blob: c2563030102cbb817dcd94f3a73a2d100b791f2f [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>
34#include <proto/log.h>
35#include <proto/proto_http.h>
36#include <proto/proxy.h>
37#include <proto/sample.h>
38#include <proto/session.h>
39#include <proto/signal.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010040#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020041#include <proto/stream.h>
42#include <proto/stream_interface.h>
43#include <proto/task.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020044#include <proto/tcp_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020045#include <proto/vars.h>
46
47#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
48#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010049#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020050#else
51#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010052#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020053#endif
54
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010055/* Reserved 4 bytes to the frame size. So a frame and its size can be written
56 * together in a buffer */
57#define MAX_FRAME_SIZE global.tune.bufsize - 4
58
59/* The minimum size for a frame */
60#define MIN_FRAME_SIZE 256
61
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010062/* Reserved for the metadata and the frame type.
63 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010064#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020065
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010066/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010067#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
68
Christopher Faulet3b386a32017-02-23 10:17:15 +010069/* SPOE filter id. Used to identify SPOE filters */
70const char *spoe_filter_id = "SPOE filter";
71
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020072/* Set if the handle on SIGUSR1 is registered */
73static int sighandler_registered = 0;
74
75/* proxy used during the parsing */
76struct proxy *curproxy = NULL;
77
78/* The name of the SPOE engine, used during the parsing */
79char *curengine = NULL;
80
81/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020082/* SPOE agent/group/message used during the parsing */
83struct spoe_agent *curagent = NULL;
84struct spoe_group *curgrp = NULL;
85struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020086
87/* list of SPOE messages and placeholders used during the parsing */
88struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020089struct list curgrps;
90struct list curmphs;
91struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010092struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020093
Christopher Faulet7250b8f2018-03-26 17:19:01 +020094/* list of log servers used during the parsing */
95struct list curlogsrvs;
96
Christopher Faulet0e0f0852018-03-26 17:20:36 +020097/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
98int curpxopts;
99int curpxopts2;
100
Christopher Faulet42bfa462017-01-04 14:14:19 +0100101/* Pools used to allocate SPOE structs */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100102static struct pool_head *pool_head_spoe_ctx = NULL;
103static struct pool_head *pool_head_spoe_appctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200104
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200105struct flt_ops spoe_ops;
106
Christopher Faulet8ef75252017-02-20 22:56:03 +0100107static int spoe_queue_context(struct spoe_context *ctx);
108static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
109static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200110
111/********************************************************************
112 * helper functions/globals
113 ********************************************************************/
114static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200115spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200116{
Christopher Faulet11610f32017-09-21 10:23:10 +0200117 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200119 free(ph->id);
120 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200121}
122
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200123static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100124spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200125{
Christopher Faulet57583e42017-09-04 15:41:09 +0200126 struct spoe_arg *arg, *argback;
127 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200128
129 if (!msg)
130 return;
131 free(msg->id);
132 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200133 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200134 release_sample_expr(arg->expr);
135 free(arg->name);
136 LIST_DEL(&arg->list);
137 free(arg);
138 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200139 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
140 LIST_DEL(&acl->list);
141 prune_acl(acl);
142 free(acl);
143 }
144 if (msg->cond) {
145 prune_acl_cond(msg->cond);
146 free(msg->cond);
147 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200148 free(msg);
149}
150
151static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200152spoe_release_group(struct spoe_group *grp)
153{
154 if (!grp)
155 return;
156 free(grp->id);
157 free(grp->conf.file);
158 free(grp);
159}
160
161static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100162spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200163{
Christopher Faulet11610f32017-09-21 10:23:10 +0200164 struct spoe_message *msg, *msgback;
165 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200166 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200167
168 if (!agent)
169 return;
170 free(agent->id);
171 free(agent->conf.file);
172 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100173 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100174 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100175 free(agent->var_t_process);
176 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200177 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
178 LIST_DEL(&msg->list);
179 spoe_release_message(msg);
180 }
181 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
182 LIST_DEL(&grp->list);
183 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200184 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200185 for (i = 0; i < global.nbthread; ++i)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100186 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +0200187 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200188 free(agent);
189}
190
191static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100192 [SPOE_FRM_ERR_NONE] = "normal",
193 [SPOE_FRM_ERR_IO] = "I/O error",
194 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
195 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
196 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
197 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
198 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
199 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
200 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
201 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
202 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
203 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100204 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100205 [SPOE_FRM_ERR_RES] = "resource allocation error",
206 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200207};
208
209static const char *spoe_event_str[SPOE_EV_EVENTS] = {
210 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
211 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
212 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
213 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
214 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
215
216 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
217 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
218 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
219};
220
221
222#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
223
224static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100225 [SPOE_CTX_ST_NONE] = "NONE",
226 [SPOE_CTX_ST_READY] = "READY",
227 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
228 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
229 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
230 [SPOE_CTX_ST_DONE] = "DONE",
231 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200232};
233
234static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100235 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
236 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
237 [SPOE_APPCTX_ST_IDLE] = "IDLE",
238 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
239 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
240 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
241 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
242 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
243 [SPOE_APPCTX_ST_EXIT] = "EXIT",
244 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200245};
246
247#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100248
Christopher Faulet8ef75252017-02-20 22:56:03 +0100249/* Used to generates a unique id for an engine. On success, it returns a
250 * allocated string. So it is the caller's reponsibility to release it. If the
251 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100252static char *
253generate_pseudo_uuid()
254{
255 static int init = 0;
256
257 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
258 const char uuid_chr[] = "0123456789ABCDEF-";
259 char *uuid;
260 int i;
261
262 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
263 return NULL;
264
265 if (!init) {
266 srand(now_ms);
267 init = 1;
268 }
269
270 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
271 int r = rand () % 16;
272
273 switch (uuid_fmt[i]) {
274 case 'x' : uuid[i] = uuid_chr[r]; break;
275 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
276 default : uuid[i] = uuid_fmt[i]; break;
277 }
278 }
279 return uuid;
280}
281
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100282
283static inline void
284spoe_update_stat_time(struct timeval *tv, long *t)
285{
286 if (*t == -1)
287 *t = tv_ms_elapsed(tv, &now);
288 else
289 *t += tv_ms_elapsed(tv, &now);
290 tv_zero(tv);
291}
292
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200293/********************************************************************
294 * Functions that encode/decode SPOE frames
295 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200296/* Helper to get static string length, excluding the terminating null byte */
297#define SLEN(str) (sizeof(str)-1)
298
299/* Predefined key used in HELLO/DISCONNECT frames */
300#define SUPPORTED_VERSIONS_KEY "supported-versions"
301#define VERSION_KEY "version"
302#define MAX_FRAME_SIZE_KEY "max-frame-size"
303#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100304#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100305#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200306#define STATUS_CODE_KEY "status-code"
307#define MSG_KEY "message"
308
309struct spoe_version {
310 char *str;
311 int min;
312 int max;
313};
314
315/* All supported versions */
316static struct spoe_version supported_versions[] = {
317 {"1.0", 1000, 1000},
318 {NULL, 0, 0}
319};
320
321/* Comma-separated list of supported versions */
322#define SUPPORTED_VERSIONS_VAL "1.0"
323
Christopher Faulet8ef75252017-02-20 22:56:03 +0100324/* Convert a string to a SPOE version value. The string must follow the format
325 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
326 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200327static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100328spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200329{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100330 const char *p, *end;
331 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200332
Christopher Faulet8ef75252017-02-20 22:56:03 +0100333 p = str;
334 end = str+len;
335 maj = min = 0;
336 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200337
Christopher Faulet8ef75252017-02-20 22:56:03 +0100338 /* skip leading spaces */
339 while (p < end && isspace(*p))
340 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200341
Christopher Faulet8ef75252017-02-20 22:56:03 +0100342 /* parse Major number, until the '.' */
343 while (*p != '.') {
344 if (p >= end || *p < '0' || *p > '9')
345 goto out;
346 maj *= 10;
347 maj += (*p - '0');
348 p++;
349 }
350
351 /* check Major version */
352 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200353 goto out;
354
Christopher Faulet8ef75252017-02-20 22:56:03 +0100355 p++; /* skip the '.' */
356 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
357 goto out;
358
359 /* Parse Minor number */
360 while (p < end) {
361 if (*p < '0' || *p > '9')
362 break;
363 min *= 10;
364 min += (*p - '0');
365 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200366 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100367
368 /* check Minor number */
369 if (min > 999)
370 goto out;
371
372 /* skip trailing spaces */
373 while (p < end && isspace(*p))
374 p++;
375 if (p != end)
376 goto out;
377
378 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200379 out:
380 return vsn;
381}
382
Christopher Faulet8ef75252017-02-20 22:56:03 +0100383/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
384 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
385 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200386static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100387spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200388{
Christopher Faulet305c6072017-02-23 16:17:53 +0100389 struct chunk *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100390 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100391 char *p, *end;
392 unsigned int flags = SPOE_FRM_FL_FIN;
393 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200394
Christopher Faulet8ef75252017-02-20 22:56:03 +0100395 p = frame;
396 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200397
Christopher Faulet8ef75252017-02-20 22:56:03 +0100398 /* Set Frame type */
399 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200400
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100401 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200402 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100403 memcpy(p, (char *)&flags, 4);
404 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200405
406 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100407 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200408
409 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
410 * and "capabilities" */
411
412 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100413 sz = SLEN(SUPPORTED_VERSIONS_KEY);
414 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
415 goto too_big;
416
417 *p++ = SPOE_DATA_T_STR;
418 sz = SLEN(SUPPORTED_VERSIONS_VAL);
419 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
420 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200421
422 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100423 sz = SLEN(MAX_FRAME_SIZE_KEY);
424 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
425 goto too_big;
426
427 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200428 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100429 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200430
431 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100432 sz = SLEN(CAPABILITIES_KEY);
433 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
434 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200435
Christopher Faulet8ef75252017-02-20 22:56:03 +0100436 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100437 chk = get_trash_chunk();
438 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
439 memcpy(chk->str, "pipelining", 10);
440 chk->len += 10;
441 }
442 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
443 if (chk->len) chk->str[chk->len++] = ',';
444 memcpy(chk->str+chk->len, "async", 5);
445 chk->len += 5;
446 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100447 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
448 if (chk->len) chk->str[chk->len++] = ',';
449 memcpy(chk->str+chk->len, "fragmentation", 13);
450 chk->len += 5;
451 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100452 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100453 goto too_big;
454
455 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100456 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100457 sz = SLEN(ENGINE_ID_KEY);
458 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
459 goto too_big;
460
461 *p++ = SPOE_DATA_T_STR;
462 sz = strlen(agent->engine_id);
463 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
464 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100465 }
466
Christopher Faulet8ef75252017-02-20 22:56:03 +0100467 return (p - frame);
468
469 too_big:
470 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
471 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200472}
473
Christopher Faulet8ef75252017-02-20 22:56:03 +0100474/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
475 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
476 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200477static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100478spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200479{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100480 const char *reason;
481 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100482 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100483 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200484
Christopher Faulet8ef75252017-02-20 22:56:03 +0100485 p = frame;
486 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200487
Christopher Faulet8ef75252017-02-20 22:56:03 +0100488 /* Set Frame type */
489 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200490
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100491 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200492 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100493 memcpy(p, (char *)&flags, 4);
494 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200495
496 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100497 *p++ = 0; *p++ = 0;
498
499 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
500 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200501
502 /* There are 2 mandatory items: "status-code" and "message" */
503
504 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100505 sz = SLEN(STATUS_CODE_KEY);
506 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
507 goto too_big;
508
509 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200510 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100511 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200512
513 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100514 sz = SLEN(MSG_KEY);
515 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
516 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200517
Christopher Faulet8ef75252017-02-20 22:56:03 +0100518 /*Get the message corresponding to the status code */
519 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
520
521 *p++ = SPOE_DATA_T_STR;
522 sz = strlen(reason);
523 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
524 goto too_big;
525
526 return (p - frame);
527
528 too_big:
529 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
530 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200531}
532
Christopher Faulet8ef75252017-02-20 22:56:03 +0100533/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
534 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
535 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200536static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100537spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100538 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200539{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100540 char *p, *end;
541 unsigned int stream_id, frame_id;
542 unsigned int flags = SPOE_FRM_FL_FIN;
543 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200544
Christopher Faulet8ef75252017-02-20 22:56:03 +0100545 p = frame;
546 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200547
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100548 stream_id = ctx->stream_id;
549 frame_id = ctx->frame_id;
550
551 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
552 /* The fragmentation is not supported by the applet */
553 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
554 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
555 return -1;
556 }
557 flags = ctx->frag_ctx.flags;
558 }
559
560 /* Set Frame type */
561 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
562
563 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200564 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100565 memcpy(p, (char *)&flags, 4);
566 p += 4;
567
568 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200569 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100570 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200571 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100572 goto too_big;
573
574 /* Copy encoded messages, if possible */
575 sz = ctx->buffer->i;
576 if (p + sz >= end)
577 goto too_big;
578 memcpy(p, ctx->buffer->p, sz);
579 p += sz;
580
581 return (p - frame);
582
583 too_big:
584 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
585 return 0;
586}
587
588/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
589 * returns the number of encoded bytes in the frame on success, 0 if an encoding
590 * error occurred and -1 if a fatal error occurred. */
591static int
592spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
593 char *frame, size_t size)
594{
595 char *p, *end;
596 unsigned int stream_id, frame_id;
597 unsigned int flags;
598 size_t sz;
599
600 p = frame;
601 end = frame+size;
602
Christopher Faulet8ef75252017-02-20 22:56:03 +0100603 /* <ctx> is null when the stream has aborted the processing of a
604 * fragmented frame. In this case, we must notify the corresponding
605 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100606 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100607 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100608 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
609 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
610 }
611 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100612 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100613 stream_id = ctx->stream_id;
614 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100615 }
616
Christopher Faulet8ef75252017-02-20 22:56:03 +0100617 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100618 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100619
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100620 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200621 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100622 memcpy(p, (char *)&flags, 4);
623 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200624
625 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200626 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100627 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200628 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100629 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200630
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100631 if (ctx == NULL)
632 goto end;
633
Christopher Faulet8ef75252017-02-20 22:56:03 +0100634 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100635 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100636 if (p + sz >= end)
637 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100638 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100639 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100640
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100641 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100642 return (p - frame);
643
644 too_big:
645 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
646 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200647}
648
Christopher Faulet8ef75252017-02-20 22:56:03 +0100649/* Decode and process the HELLO frame sent by an agent. It returns the number of
650 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
651 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200652static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100653spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200654{
Christopher Faulet305c6072017-02-23 16:17:53 +0100655 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
656 char *p, *end;
657 int vsn, max_frame_size;
658 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100659
660 p = frame;
661 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200662
663 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100664 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
665 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200666 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100667 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200668
Christopher Faulet8ef75252017-02-20 22:56:03 +0100669 if (size < 7 /* TYPE + METADATA */) {
670 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
671 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200672 }
673
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100674 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100675 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200676 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100677 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200678
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100679 /* Fragmentation is not supported for HELLO frame */
680 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100681 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100682 return -1;
683 }
684
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200685 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100686 if (*p != 0 || *(p+1) != 0) {
687 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
688 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200689 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100690 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200691
692 /* There are 3 mandatory items: "version", "max-frame-size" and
693 * "capabilities" */
694
695 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100696 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100697 while (p < end) {
698 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200699 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100700 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200701
702 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100703 ret = spoe_decode_buffer(&p, end, &str, &sz);
704 if (ret == -1 || !sz) {
705 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
706 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200707 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100708
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200709 /* Check "version" K/V item */
710 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100711 int i, type = *p++;
712
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200713 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100714 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
715 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
716 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200717 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100718 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
719 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
720 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200721 }
722
Christopher Faulet8ef75252017-02-20 22:56:03 +0100723 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200724 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100725 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200726 return -1;
727 }
728 for (i = 0; supported_versions[i].str != NULL; ++i) {
729 if (vsn >= supported_versions[i].min &&
730 vsn <= supported_versions[i].max)
731 break;
732 }
733 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100734 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200735 return -1;
736 }
737 }
738 /* Check "max-frame-size" K/V item */
739 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100740 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200741
742 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200743 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
744 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
745 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
746 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100747 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
748 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200749 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200750 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100751 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
752 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200753 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100754 if (sz < MIN_FRAME_SIZE ||
755 sz > SPOE_APPCTX(appctx)->max_frame_size) {
756 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200757 return -1;
758 }
759 max_frame_size = sz;
760 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100761 /* Check "capabilities" K/V item */
762 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100763 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100764
765 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100766 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
767 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
768 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100769 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100770 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
771 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
772 return 0;
773 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100774
Christopher Faulet8ef75252017-02-20 22:56:03 +0100775 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100776 char *delim;
777
778 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100779 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100780
Christopher Faulet8ef75252017-02-20 22:56:03 +0100781 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
782 str += 10; sz -= 10;
783 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100784 flags |= SPOE_APPCTX_FL_PIPELINING;
785 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100786 else if (sz >= 5 && !strncmp(str, "async", 5)) {
787 str += 5; sz -= 5;
788 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100789 flags |= SPOE_APPCTX_FL_ASYNC;
790 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100791 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
792 str += 13; sz -= 13;
793 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100794 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
795 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100796
Christopher Faulet8ef75252017-02-20 22:56:03 +0100797 /* Get the next comma or break */
798 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100799 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100800 delim++;
801 sz -= (delim - str);
802 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100803 }
804 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200805 else {
806 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100807 if (spoe_skip_data(&p, end) == -1) {
808 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
809 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200810 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200811 }
812 }
813
814 /* Final checks */
815 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100816 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200817 return -1;
818 }
819 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100820 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200821 return -1;
822 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100823 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
824 flags &= ~SPOE_APPCTX_FL_PIPELINING;
825 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
826 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200827
Christopher Faulet42bfa462017-01-04 14:14:19 +0100828 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
829 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
830 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100831
832 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200833}
834
835/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
836 * bytes on success, 0 if the frame can be ignored and -1 if an error
837 * occurred. */
838static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100839spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200840{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100841 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100842 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100843
844 p = frame;
845 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200846
847 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100848 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
849 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200850 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100851 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200852
Christopher Faulet8ef75252017-02-20 22:56:03 +0100853 if (size < 7 /* TYPE + METADATA */) {
854 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
855 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200856 }
857
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100858 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100859 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200860 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100861 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200862
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100863 /* Fragmentation is not supported for DISCONNECT frame */
864 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100865 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100866 return -1;
867 }
868
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200869 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100870 if (*p != 0 || *(p+1) != 0) {
871 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
872 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200873 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100874 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200875
876 /* There are 2 mandatory items: "status-code" and "message" */
877
878 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100879 while (p < end) {
880 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200881 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100882 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200883
884 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100885 ret = spoe_decode_buffer(&p, end, &str, &sz);
886 if (ret == -1 || !sz) {
887 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
888 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200889 }
890
891 /* Check "status-code" K/V item */
892 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100893 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200894
895 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200896 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
897 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
898 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
899 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100900 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
901 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200902 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200903 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100904 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
905 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200906 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100907 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 }
909
910 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100911 else if (!memcmp(str, MSG_KEY, sz)) {
912 int type = *p++;
913
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200914 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100915 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
916 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
917 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200918 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100919 ret = spoe_decode_buffer(&p, end, &str, &sz);
920 if (ret == -1 || sz > 255) {
921 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
922 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200923 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100924#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
925 SPOE_APPCTX(appctx)->reason = str;
926 SPOE_APPCTX(appctx)->rlen = sz;
927#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200928 }
929 else {
930 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100931 if (spoe_skip_data(&p, end) == -1) {
932 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
933 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200934 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200935 }
936 }
937
Christopher Faulet8ef75252017-02-20 22:56:03 +0100938 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200939}
940
941
Christopher Fauleta1cda022016-12-21 08:58:06 +0100942/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200943 * success, 0 if the frame can be ignored and -1 if an error occurred. */
944static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100945spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100946 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200947{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100948 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100949 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100950 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100951 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100952 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100953
954 p = frame;
955 end = frame + size;
956 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200957
958 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100959 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
960 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200961 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100962 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200963
Christopher Faulet8ef75252017-02-20 22:56:03 +0100964 if (size < 7 /* TYPE + METADATA */) {
965 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
966 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200967 }
968
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100969 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100970 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200971 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100972 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200973
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100974 /* Fragmentation is not supported for now */
975 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100976 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100977 return -1;
978 }
979
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200980 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200981 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100982 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100983 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100984 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200985 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100986 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200987 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100988 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100989
Christopher Faulet8ef75252017-02-20 22:56:03 +0100990 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100991 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200992 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100993 if ((*ctx)->stream_id == (unsigned int)stream_id &&
994 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100995 goto found;
996 }
997 }
998 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100999 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
1000 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +01001001 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001002 goto found;
1003 }
1004 }
1005
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001006 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
1007 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
1008 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
1009
1010 /* ABRT bit is set for an unfinished fragmented frame */
1011 if (flags & SPOE_FRM_FL_ABRT) {
1012 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001013 (*ctx)->state = SPOE_CTX_ST_ERROR;
1014 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1015 /* Ignore the payload */
1016 goto end;
1017 }
1018 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1019 /* For now, we ignore the ack */
1020 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1021 return 0;
1022 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001023
Christopher Fauleta1cda022016-12-21 08:58:06 +01001024 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001025 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1026 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001027 " - stream-id=%u - frame-id=%u\n",
1028 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1029 __FUNCTION__, appctx,
1030 (unsigned int)stream_id, (unsigned int)frame_id);
1031
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001032 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001033 return 0;
1034
1035 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001036 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1037 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001038 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001039 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001040 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001041
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001042 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001043 len = (end - p);
1044 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1045 SPOE_APPCTX(appctx)->buffer->i = len;
1046 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001047
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001048 /* Transfer the buffer ownership to the SPOE context */
1049 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1050 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001051
Christopher Faulet8ef75252017-02-20 22:56:03 +01001052 (*ctx)->state = SPOE_CTX_ST_DONE;
1053
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001054 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001055 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001056 " - ACK frame received"
1057 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001058 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001059 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1060 (*ctx)->frame_id, flags);
1061 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001062}
1063
Christopher Fauletba7bc162016-11-07 21:07:38 +01001064/* This function is used in cfgparse.c and declared in proto/checks.h. It
1065 * prepare the request to send to agents during a healthcheck. It returns 0 on
1066 * success and -1 if an error occurred. */
1067int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001068spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001069{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001070 struct appctx appctx;
1071 struct spoe_appctx spoe_appctx;
1072 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1073 size_t sz;
1074 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001075
Christopher Faulet42bfa462017-01-04 14:14:19 +01001076 memset(&appctx, 0, sizeof(appctx));
1077 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001078 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001079
1080 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001081 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001082
Christopher Faulet8ef75252017-02-20 22:56:03 +01001083 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1084 end = frame + MAX_FRAME_SIZE;
1085
1086 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1087 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001088 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001089 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001090
Christopher Faulet8ef75252017-02-20 22:56:03 +01001091 /* Add "healthcheck" K/V item */
1092 sz = SLEN(HEALTHCHECK_KEY);
1093 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1094 return -1;
1095 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001096
Christopher Faulet8ef75252017-02-20 22:56:03 +01001097 *len = frame - buf;
1098 sz = htonl(*len - 4);
1099 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001100
Christopher Faulet8ef75252017-02-20 22:56:03 +01001101 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001102 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001103 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001104 return 0;
1105}
1106
1107/* This function is used in checks.c and declared in proto/checks.h. It decode
1108 * the response received from an agent during a healthcheck. It returns 0 on
1109 * success and -1 if an error occurred. */
1110int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001111spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001112{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001113 struct appctx appctx;
1114 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001115
Christopher Faulet42bfa462017-01-04 14:14:19 +01001116 memset(&appctx, 0, sizeof(appctx));
1117 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001118
Christopher Faulet42bfa462017-01-04 14:14:19 +01001119 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001120 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001121
Christopher Faulet8ef75252017-02-20 22:56:03 +01001122 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1123 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001124 goto error;
1125 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001126 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1127 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001128
1129 return 0;
1130
1131 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001132 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1133 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1134 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001135 return -1;
1136}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001137
Christopher Fauleta1cda022016-12-21 08:58:06 +01001138/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1139 * the frame can be ignored, 1 to retry later, and the frame legnth on
1140 * success. */
1141static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001142spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001143{
1144 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001145 int ret;
1146 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001147
Christopher Faulet8ef75252017-02-20 22:56:03 +01001148 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1149 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001150 netint = htonl(framesz);
1151 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001152 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001153 if (ret <= 0) {
Christopher Fauletd5216d42018-02-01 08:45:22 +01001154 if ((ret == -3 && si_ic(si)->buf == &buf_empty) || ret == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001155 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001156 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001157 }
1158 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001159 return -1; /* error */
1160 }
1161 return framesz;
1162}
1163
1164/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1165 * when the frame can be ignored, 1 to retry later and the frame length on
1166 * success. */
1167static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001168spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001169{
1170 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001171 int ret;
1172 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001173
Willy Tarreau06d80a92017-10-19 14:32:15 +02001174 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001175 if (ret > 0) {
1176 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001177 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001178 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001179 return -1;
1180 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001181 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001182 }
1183 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001184 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001185 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001186 }
1187 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001188 return -1; /* error */
1189 }
1190 return framesz;
1191}
1192
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001193/********************************************************************
1194 * Functions that manage the SPOE applet
1195 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001196static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001197spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001198{
1199 si_applet_want_get(appctx->owner);
1200 si_applet_want_put(appctx->owner);
1201 appctx_wakeup(appctx);
1202 return 1;
1203}
1204
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001205/* Callback function that catches applet timeouts. If a timeout occurred, we set
1206 * <appctx->st1> flag and the SPOE applet is woken up. */
1207static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001208spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001209{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001210 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001211
1212 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1213 if (tick_is_expired(task->expire, now_ms)) {
1214 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001215 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001216 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001217 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001218 return task;
1219}
1220
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001221/* Callback function that releases a SPOE applet. This happens when the
1222 * connection with the agent is closed. */
1223static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001224spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001225{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001226 struct stream_interface *si = appctx->owner;
1227 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1228 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001229 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001230
1231 if (spoe_appctx == NULL)
1232 return;
1233
1234 appctx->ctx.spoe.ptr = NULL;
1235 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001236
1237 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1238 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1239 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001240
Christopher Faulet8ef75252017-02-20 22:56:03 +01001241 /* Remove applet from the list of running applets */
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001242 HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001243 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001244 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1245 LIST_DEL(&spoe_appctx->list);
1246 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001247 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001248 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001249
Christopher Faulet8ef75252017-02-20 22:56:03 +01001250 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001251 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001252 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1253 eb32_delete(&spoe_appctx->node);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001254 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001255 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001256
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001257 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001258 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1259 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001260
1261 si_shutw(si);
1262 si_shutr(si);
1263 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001264 }
1265
Christopher Faulet8ef75252017-02-20 22:56:03 +01001266 /* Destroy the task attached to this applet */
1267 if (spoe_appctx->task) {
1268 task_delete(spoe_appctx->task);
1269 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001270 }
1271
Christopher Faulet8ef75252017-02-20 22:56:03 +01001272 /* Notify all waiting streams */
1273 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001274 LIST_DEL(&ctx->list);
1275 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001276 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001277 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001278 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001279 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001280 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001281 }
1282
Christopher Faulet8ef75252017-02-20 22:56:03 +01001283 /* If the applet was processing a fragmented frame, notify the
1284 * corresponding stream. */
1285 if (spoe_appctx->frag_ctx.ctx) {
1286 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001287 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001288 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001289 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001290 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1291 }
1292
Christopher Faulet8ef75252017-02-20 22:56:03 +01001293 /* Release allocated memory */
1294 spoe_release_buffer(&spoe_appctx->buffer,
1295 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001296 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001297
Christopher Faulet24289f22017-09-25 14:48:02 +02001298 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001299 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001300
Christopher Faulet8ef75252017-02-20 22:56:03 +01001301 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001302 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001303 LIST_DEL(&ctx->list);
1304 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001305 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001306 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001307 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001308 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001309 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001310 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001311 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001312 LIST_DEL(&ctx->list);
1313 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001314 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001315 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001316 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001317 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001318 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1319 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001320
1321 end:
1322 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001323 agent->rt[tid].frame_size = agent->max_frame_size;
1324 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1325 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001326}
1327
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001328static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001329spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001330{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001331 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001332 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001333 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001334 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001335
Christopher Fauleta1cda022016-12-21 08:58:06 +01001336 if (si->state <= SI_ST_CON) {
1337 si_applet_want_put(si);
1338 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1339 goto stop;
1340 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001341 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001342 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001343 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001344 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001345
Christopher Fauleta1cda022016-12-21 08:58:06 +01001346 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001347 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1348 " - Connection timed out\n",
1349 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1350 __FUNCTION__, appctx);
1351 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001352 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001353 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001354
Christopher Faulet42bfa462017-01-04 14:14:19 +01001355 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001356 SPOE_APPCTX(appctx)->task->expire =
1357 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001358
Christopher Faulet8ef75252017-02-20 22:56:03 +01001359 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1360 * length. */
1361 buf = trash.str; frame = buf+4;
1362 ret = spoe_prepare_hahello_frame(appctx, frame,
1363 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001364 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001365 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001366
1367 switch (ret) {
1368 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001369 case 0: /* ignore => an error, cannot be ignored */
1370 goto exit;
1371
1372 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001373 goto stop;
1374
Christopher Faulet8ef75252017-02-20 22:56:03 +01001375 default:
1376 /* HELLO frame successfully sent, now wait for the
1377 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001378 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1379 goto next;
1380 }
1381
1382 next:
1383 return 0;
1384 stop:
1385 return 1;
1386 exit:
1387 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1388 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001389}
1390
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001391static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001392spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001393{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001394 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001395 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001396 char *frame;
1397 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001398
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001399
Christopher Fauletb067b062017-01-04 16:39:11 +01001400 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001401 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001402 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001403 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001404
Christopher Fauleta1cda022016-12-21 08:58:06 +01001405 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001406 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1407 " - Connection timed out\n",
1408 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1409 __FUNCTION__, appctx);
1410 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001411 goto exit;
1412 }
1413
Christopher Faulet8ef75252017-02-20 22:56:03 +01001414 frame = trash.str; trash.len = 0;
1415 ret = spoe_recv_frame(appctx, frame,
1416 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001417 if (ret > 1) {
1418 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1419 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1420 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001421 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001422 trash.len = ret + 4;
1423 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001424 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001425
Christopher Fauleta1cda022016-12-21 08:58:06 +01001426 switch (ret) {
1427 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001428 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001429 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1430 goto next;
1431
1432 case 1: /* retry later */
1433 goto stop;
1434
1435 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001436 /* HELLO handshake is finished, set the idle timeout and
1437 * add the applet in the list of running applets. */
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001438 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001439 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001440 SPOE_APPCTX(appctx)->node.key = 0;
1441 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001442
1443 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001444 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001445 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001446 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001447
Christopher Fauleta1cda022016-12-21 08:58:06 +01001448 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001449 /* Do not forget to remove processed frame from the output buffer */
1450 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001451 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001452
1453 SPOE_APPCTX(appctx)->task->expire =
1454 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001455 return 0;
1456 stop:
1457 return 1;
1458 exit:
1459 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1460 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001461}
1462
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001463
Christopher Fauleta1cda022016-12-21 08:58:06 +01001464static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001465spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001466{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001467 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1468 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001469 char *frame, *buf;
1470 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001471
Christopher Faulet8ef75252017-02-20 22:56:03 +01001472 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1473 * length. */
1474 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001475
1476 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1477 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1478 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1479 SPOE_APPCTX(appctx)->max_frame_size);
1480 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001481 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001482 *skip = 1;
1483 ret = 1;
1484 goto end;
1485 }
1486 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001487 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001488 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1489 SPOE_APPCTX(appctx)->max_frame_size);
1490
1491 }
1492
Christopher Faulet8ef75252017-02-20 22:56:03 +01001493 if (ret > 1)
1494 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001495
Christopher Faulet8ef75252017-02-20 22:56:03 +01001496 switch (ret) {
1497 case -1: /* error */
1498 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1499 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001500
Christopher Faulet8ef75252017-02-20 22:56:03 +01001501 case 0: /* ignore */
1502 if (ctx == NULL)
1503 goto abort_frag_frame;
1504
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001505 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001506 LIST_DEL(&ctx->list);
1507 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001508 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001509 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001510 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001511 ctx->state = SPOE_CTX_ST_ERROR;
1512 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1513 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001514 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001515 break;
1516
1517 case 1: /* retry */
1518 *skip = 1;
1519 break;
1520
1521 default:
1522 if (ctx == NULL)
1523 goto abort_frag_frame;
1524
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001525 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001526 LIST_DEL(&ctx->list);
1527 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001528 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001529 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001530 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001531 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1532 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1533 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001534 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001535 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001536 }
1537 goto end;
1538
1539 frag_frame_sent:
1540 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001541 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001542 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1543 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1544 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001545 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1546 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1547 goto end;
1548
1549 no_frag_frame_sent:
1550 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1551 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001552 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001553 }
1554 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1555 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1556 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1557 }
1558 else {
1559 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001560 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001561 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1562 }
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001563 HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001564 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001565 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1566 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1567 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001568 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001569
Christopher Faulet8ef75252017-02-20 22:56:03 +01001570 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1571 goto end;
1572
1573 abort_frag_frame:
1574 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1575 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1576 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1577 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1578 goto end;
1579
1580 end:
1581 return ret;
1582}
1583
1584static int
1585spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1586{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001587 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001588 struct spoe_context *ctx = NULL;
1589 char *frame;
1590 int ret;
1591
1592 frame = trash.str; trash.len = 0;
1593 ret = spoe_recv_frame(appctx, frame,
1594 SPOE_APPCTX(appctx)->max_frame_size);
1595 if (ret > 1) {
1596 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1597 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001598 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001599 goto end;
1600 }
1601 trash.len = ret + 4;
1602 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1603 }
1604 switch (ret) {
1605 case -1: /* error */
1606 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1607 break;
1608
1609 case 0: /* ignore */
1610 break;
1611
1612 case 1: /* retry */
1613 *skip = 1;
1614 break;
1615
1616 default:
1617 LIST_DEL(&ctx->list);
1618 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001619 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001620 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1621 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001622 if (ctx->spoe_appctx) {
1623 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001624 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001625 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001626 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1627 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1628 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1629 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1630 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1631 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1632 }
1633 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1634 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001635 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1636 break;
1637 }
1638
1639 /* Do not forget to remove processed frame from the output buffer */
1640 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001641 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001642 end:
1643 return ret;
1644}
1645
1646static int
1647spoe_handle_processing_appctx(struct appctx *appctx)
1648{
1649 struct stream_interface *si = appctx->owner;
1650 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001651 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001652
1653 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1654 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1655 goto exit;
1656 }
1657
1658 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1659 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1660 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1661 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1662 goto next;
1663 }
1664
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001665 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001666 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001667 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001668 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1669 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001670 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001671
Christopher Faulet8f82b202018-01-24 16:23:03 +01001672 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1673 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001674
Christopher Faulet8f82b202018-01-24 16:23:03 +01001675 /* receiving_frame loop */
1676 while (!skip_receiving) {
1677 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1678 switch (ret) {
1679 case -1: /* error */
1680 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001681
Christopher Faulet8f82b202018-01-24 16:23:03 +01001682 case 0: /* ignore */
1683 active_r = 1;
1684 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001685
Christopher Faulet8f82b202018-01-24 16:23:03 +01001686 case 1: /* retry */
1687 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001688
Christopher Faulet8f82b202018-01-24 16:23:03 +01001689 default:
1690 active_r = 1;
1691 break;
1692 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001693 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001694
Christopher Faulet8f82b202018-01-24 16:23:03 +01001695 /* send_frame loop */
1696 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1697 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1698 switch (ret) {
1699 case -1: /* error */
1700 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001701
Christopher Faulet8f82b202018-01-24 16:23:03 +01001702 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001703 if (SPOE_APPCTX(appctx)->node.key)
1704 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001705 active_s++;
1706 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001707
Christopher Faulet8f82b202018-01-24 16:23:03 +01001708 case 1: /* retry */
1709 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001710
Christopher Faulet8f82b202018-01-24 16:23:03 +01001711 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001712 if (SPOE_APPCTX(appctx)->node.key)
1713 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001714 active_s++;
1715 break;
1716 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001717 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001718
Christopher Faulet8f82b202018-01-24 16:23:03 +01001719 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001720 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1721 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1722 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001723
Christopher Faulet8f82b202018-01-24 16:23:03 +01001724 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001725 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001726 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001727 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001728 }
1729 return 1;
1730
Christopher Faulet8f82b202018-01-24 16:23:03 +01001731 next:
1732 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1733 return 0;
1734
Christopher Fauleta1cda022016-12-21 08:58:06 +01001735 exit:
1736 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1737 return 0;
1738}
1739
1740static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001741spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001742{
1743 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001744 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001745 char *frame, *buf;
1746 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001747
Christopher Fauleta1cda022016-12-21 08:58:06 +01001748 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1749 goto exit;
1750
1751 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1752 goto exit;
1753
Christopher Faulet8ef75252017-02-20 22:56:03 +01001754 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1755 * length. */
1756 buf = trash.str; frame = buf+4;
1757 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1758 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001759 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001760 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001761
1762 switch (ret) {
1763 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001764 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001765 goto exit;
1766
1767 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001768 goto stop;
1769
1770 default:
1771 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1772 " - disconnected by HAProxy (%d): %s\n",
1773 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001774 __FUNCTION__, appctx,
1775 SPOE_APPCTX(appctx)->status_code,
1776 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001777
1778 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1779 goto next;
1780 }
1781
1782 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001783 SPOE_APPCTX(appctx)->task->expire =
1784 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001785 return 0;
1786 stop:
1787 return 1;
1788 exit:
1789 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1790 return 0;
1791}
1792
1793static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001794spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001795{
1796 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001797 char *frame;
1798 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001799
Christopher Fauletb067b062017-01-04 16:39:11 +01001800 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001801 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001802 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001803 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001804
Christopher Fauletb067b062017-01-04 16:39:11 +01001805 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001806 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001807 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001808 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001809
Christopher Faulet8ef75252017-02-20 22:56:03 +01001810 frame = trash.str; trash.len = 0;
1811 ret = spoe_recv_frame(appctx, frame,
1812 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001813 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001814 trash.len = ret + 4;
1815 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001816 }
1817
1818 switch (ret) {
1819 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001820 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1821 " - error on frame (%s)\n",
1822 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001823 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001824 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001825 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001826 goto exit;
1827
1828 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001829 goto next;
1830
1831 case 1: /* retry */
1832 goto stop;
1833
1834 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001835 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001836 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001837 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001838 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001839 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1840 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001841 goto exit;
1842 }
1843
1844 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001845 /* Do not forget to remove processed frame from the output buffer */
1846 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001847 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001848
Christopher Fauleta1cda022016-12-21 08:58:06 +01001849 return 0;
1850 stop:
1851 return 1;
1852 exit:
1853 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1854 return 0;
1855}
1856
1857/* I/O Handler processing messages exchanged with the agent */
1858static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001859spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001860{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001861 struct stream_interface *si = appctx->owner;
1862 struct spoe_agent *agent;
1863
1864 if (SPOE_APPCTX(appctx) == NULL)
1865 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001866
Christopher Faulet8ef75252017-02-20 22:56:03 +01001867 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1868 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001869
Christopher Fauleta1cda022016-12-21 08:58:06 +01001870 switchstate:
1871 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1872 " - appctx-state=%s\n",
1873 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1874 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1875
1876 switch (appctx->st0) {
1877 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001878 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001879 goto out;
1880 goto switchstate;
1881
1882 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001883 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001884 goto out;
1885 goto switchstate;
1886
1887 case SPOE_APPCTX_ST_IDLE:
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001888 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001889 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001890 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001891 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001892 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001893 SPOE_APPCTX(appctx)->task->expire =
1894 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001895 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001896 goto switchstate;
1897 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001898 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1899 /* fall through */
1900
1901 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001902 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1903 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001904 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001905 goto out;
1906 goto switchstate;
1907
1908 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001909 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001910 goto out;
1911 goto switchstate;
1912
1913 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001914 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001915 goto out;
1916 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001917
1918 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001919 appctx->st0 = SPOE_APPCTX_ST_END;
1920 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1921
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001922 si_shutw(si);
1923 si_shutr(si);
1924 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001925 /* fall through */
1926
1927 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001928 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001929 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001930 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001931 if (stopping)
1932 spoe_wakeup_appctx(appctx);
1933
Christopher Faulet42bfa462017-01-04 14:14:19 +01001934 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1935 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001936 si_oc(si)->flags |= CF_READ_DONTWAIT;
1937 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001938}
1939
1940struct applet spoe_applet = {
1941 .obj_type = OBJ_TYPE_APPLET,
1942 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001943 .fct = spoe_handle_appctx,
1944 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001945};
1946
1947/* Create a SPOE applet. On success, the created applet is returned, else
1948 * NULL. */
1949static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001950spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001951{
1952 struct appctx *appctx;
1953 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001954 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001955
Emeric Brun1138fd02017-06-19 12:38:55 +02001956 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001957 goto out_error;
1958
Willy Tarreaubafbe012017-11-24 17:34:44 +01001959 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001960 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001961 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001962 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001963
Christopher Faulet42bfa462017-01-04 14:14:19 +01001964 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001965 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001966 goto out_free_spoe_appctx;
1967
1968 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001969 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001970 SPOE_APPCTX(appctx)->task->context = appctx;
1971 SPOE_APPCTX(appctx)->agent = conf->agent;
1972 SPOE_APPCTX(appctx)->version = 0;
1973 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1974 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001975 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001976 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001977 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001978
1979 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1980 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001981 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001982
1983 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1984 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001985
Willy Tarreau5820a362016-12-22 15:59:02 +01001986 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001987 if (!sess)
1988 goto out_free_spoe;
1989
Willy Tarreau87787ac2017-08-28 16:22:54 +02001990 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001991 goto out_free_sess;
1992
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001993 stream_set_backend(strm, conf->agent->b.be);
1994
1995 /* applet is waiting for data */
1996 si_applet_cant_get(&strm->si[0]);
1997 appctx_wakeup(appctx);
1998
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001999 strm->do_log = NULL;
2000 strm->res.flags |= CF_READ_DONTWAIT;
2001
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002002 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002003 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002004 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002005 HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002006
Emeric Brunc60def82017-09-27 14:59:38 +02002007 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002008 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002009 return appctx;
2010
2011 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002012 out_free_sess:
2013 session_free(sess);
2014 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01002015 task_free(SPOE_APPCTX(appctx)->task);
2016 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002017 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002018 out_free_appctx:
2019 appctx_free(appctx);
2020 out_error:
2021 return NULL;
2022}
2023
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002024static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002025spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002026{
2027 struct spoe_config *conf = FLT_CONF(ctx->filter);
2028 struct spoe_agent *agent = conf->agent;
2029 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002030 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002031
Christopher Fauleta1cda022016-12-21 08:58:06 +01002032 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002033 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002034 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002035 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002036
2037 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002038 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002039 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2040 ctx->strm);
2041
Christopher Fauleta1cda022016-12-21 08:58:06 +01002042 /* Do not try to create a new applet if there is no server up for the
2043 * agent's backend. */
2044 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2045 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2046 " - cannot create SPOE appctx: no server up\n",
2047 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2048 __FUNCTION__, ctx->strm);
2049 goto end;
2050 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002051
Christopher Fauleta1cda022016-12-21 08:58:06 +01002052 /* Do not try to create a new applet if we have reached the maximum of
2053 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002054 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002055 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002056 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2057 " - cannot create SPOE appctx: max CPS reached\n",
2058 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2059 __FUNCTION__, ctx->strm);
2060 goto end;
2061 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002062 }
2063
Christopher Faulet8ef75252017-02-20 22:56:03 +01002064 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002065 if (appctx == NULL) {
2066 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2067 " - failed to create SPOE appctx\n",
2068 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2069 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002070 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002071 "SPOE: [%s] failed to create SPOE applet\n",
2072 agent->id);
2073
Christopher Fauleta1cda022016-12-21 08:58:06 +01002074 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002075 }
2076
Christopher Fauleta1cda022016-12-21 08:58:06 +01002077 /* Increase the per-process number of cumulated connections */
2078 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002079 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002080
Christopher Fauleta1cda022016-12-21 08:58:06 +01002081 end:
2082 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002083 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002084 ctx->status_code = SPOE_CTX_ERR_RES;
2085 return -1;
2086 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002087
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002088 /* Add the SPOE context in the sending queue */
Christopher Faulet24289f22017-09-25 14:48:02 +02002089 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002090 HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002091 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2092 ctx->stats.tv_queue = now;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002093
2094 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002095 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002096 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002097 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002098 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002099 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002100
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002101 /* Finally try to wakeup an IDLE applet. */
2102 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2103 struct eb32_node *node;
2104
2105 node = eb32_first(&agent->rt[tid].idle_applets);
2106 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2107 if (node && spoe_appctx) {
2108 eb32_delete(&spoe_appctx->node);
2109 spoe_appctx->node.key++;
2110 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2111 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002112 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002113 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002114 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002115}
2116
2117/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002118 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002119 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002120/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2121 * fragmented_content. If the next message can be processed, it returns 0. If
2122 * the message is too big, it returns -1.*/
2123static int
2124spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2125 struct spoe_message *msg, int dir,
2126 char **buf, char *end)
2127{
2128 struct sample *smp;
2129 struct spoe_arg *arg;
2130 int ret;
2131
2132 if (msg->cond) {
2133 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2134 ret = acl_pass(ret);
2135 if (msg->cond->pol == ACL_COND_UNLESS)
2136 ret = !ret;
2137
2138 /* the rule does not match */
2139 if (!ret)
2140 goto next;
2141 }
2142
2143 /* Resume encoding of a SPOE argument */
2144 if (ctx->frag_ctx.curarg != NULL) {
2145 arg = ctx->frag_ctx.curarg;
2146 goto encode_argument;
2147 }
2148
2149 if (ctx->frag_ctx.curoff != UINT_MAX)
2150 goto encode_msg_payload;
2151
2152 /* Check if there is enough space for the message name and the
2153 * number of arguments. It implies <msg->id_len> is encoded on 2
2154 * bytes, at most (< 2288). */
2155 if (*buf + 2 + msg->id_len + 1 > end)
2156 goto too_big;
2157
2158 /* Encode the message name */
2159 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2160 goto too_big;
2161
2162 /* Set the number of arguments for this message */
2163 **buf = msg->nargs;
2164 (*buf)++;
2165
2166 ctx->frag_ctx.curoff = 0;
2167 encode_msg_payload:
2168
2169 /* Loop on arguments */
2170 list_for_each_entry(arg, &msg->args, list) {
2171 ctx->frag_ctx.curarg = arg;
2172 ctx->frag_ctx.curoff = UINT_MAX;
2173
2174 encode_argument:
2175 if (ctx->frag_ctx.curoff != UINT_MAX)
2176 goto encode_arg_value;
2177
2178 /* Encode the arguement name as a string. It can by NULL */
2179 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2180 goto too_big;
2181
2182 ctx->frag_ctx.curoff = 0;
2183 encode_arg_value:
2184
2185 /* Fetch the arguement value */
2186 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2187 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2188 if (ret == -1 || ctx->frag_ctx.curoff)
2189 goto too_big;
2190 }
2191
2192 next:
2193 return 0;
2194
2195 too_big:
2196 return -1;
2197}
2198
Christopher Fauletc718b822017-09-21 16:50:56 +02002199/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2200 * handle fragmented content. On success it returns 1. If an error occurred, -1
2201 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2202 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002203static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002204spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002205 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002206{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002207 struct spoe_config *conf = FLT_CONF(ctx->filter);
2208 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002209 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002210 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002211
Christopher Faulet8ef75252017-02-20 22:56:03 +01002212 p = ctx->buffer->p;
Christopher Faulet24289f22017-09-25 14:48:02 +02002213 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002214
Christopher Fauletc718b822017-09-21 16:50:56 +02002215 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2216 /* Resume encoding of a SPOE message */
2217 if (ctx->frag_ctx.curmsg != NULL) {
2218 msg = ctx->frag_ctx.curmsg;
2219 goto encode_evt_message;
2220 }
2221
2222 list_for_each_entry(msg, messages, by_evt) {
2223 ctx->frag_ctx.curmsg = msg;
2224 ctx->frag_ctx.curarg = NULL;
2225 ctx->frag_ctx.curoff = UINT_MAX;
2226
2227 encode_evt_message:
2228 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2229 goto too_big;
2230 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002231 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002232 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2233 /* Resume encoding of a SPOE message */
2234 if (ctx->frag_ctx.curmsg != NULL) {
2235 msg = ctx->frag_ctx.curmsg;
2236 goto encode_grp_message;
2237 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002238
Christopher Fauletc718b822017-09-21 16:50:56 +02002239 list_for_each_entry(msg, messages, by_grp) {
2240 ctx->frag_ctx.curmsg = msg;
2241 ctx->frag_ctx.curarg = NULL;
2242 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002243
Christopher Fauletc718b822017-09-21 16:50:56 +02002244 encode_grp_message:
2245 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2246 goto too_big;
2247 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002248 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002249 else
2250 goto skip;
2251
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002252
Christopher Faulet57583e42017-09-04 15:41:09 +02002253 /* nothing has been encoded for an unfragmented payload */
2254 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2255 goto skip;
2256
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002257 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002258 " - encode %s messages - spoe_appctx=%p"
2259 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002260 (int)now.tv_sec, (int)now.tv_usec,
2261 agent->id, __FUNCTION__, s,
2262 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002263 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002264 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002265
Christopher Faulet8ef75252017-02-20 22:56:03 +01002266 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002267 ctx->frag_ctx.curmsg = NULL;
2268 ctx->frag_ctx.curarg = NULL;
2269 ctx->frag_ctx.curoff = 0;
2270 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002271
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002272 return 1;
2273
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002274 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002275 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2276 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2277 return -1;
2278 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002279
2280 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002281 " - encode fragmented messages - spoe_appctx=%p"
2282 " - curmsg=%p - curarg=%p - curoff=%u"
2283 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002284 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002285 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002286 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet24289f22017-09-25 14:48:02 +02002287 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002288
Christopher Faulet8ef75252017-02-20 22:56:03 +01002289 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002290 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2291 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2292 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002293
2294 skip:
2295 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2296 " - skip the frame because nothing has been encoded\n",
2297 (int)now.tv_sec, (int)now.tv_usec,
2298 agent->id, __FUNCTION__, s);
2299 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002300}
2301
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002302
2303/***************************************************************************
2304 * Functions that handle SPOE actions
2305 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002306/* Helper function to set a variable */
2307static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002308spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002309 struct sample *smp)
2310{
2311 struct spoe_config *conf = FLT_CONF(ctx->filter);
2312 struct spoe_agent *agent = conf->agent;
2313 char varname[64];
2314
2315 memset(varname, 0, sizeof(varname));
2316 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2317 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002318 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2319 vars_set_by_name(varname, len, smp);
2320 else
2321 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002322}
2323
2324/* Helper function to unset a variable */
2325static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002326spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002327 struct sample *smp)
2328{
2329 struct spoe_config *conf = FLT_CONF(ctx->filter);
2330 struct spoe_agent *agent = conf->agent;
2331 char varname[64];
2332
2333 memset(varname, 0, sizeof(varname));
2334 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2335 scope, agent->var_pfx, len, name);
2336 vars_unset_by_name_ifexist(varname, len, smp);
2337}
2338
2339
Christopher Faulet8ef75252017-02-20 22:56:03 +01002340static inline int
2341spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2342 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002343{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002344 char *str, *scope, *p = *buf;
2345 struct sample smp;
2346 uint64_t sz;
2347 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002348
Christopher Faulet8ef75252017-02-20 22:56:03 +01002349 if (p + 2 >= end)
2350 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002351
Christopher Faulet8ef75252017-02-20 22:56:03 +01002352 /* SET-VAR requires 3 arguments */
2353 if (*p++ != 3)
2354 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002355
Christopher Faulet8ef75252017-02-20 22:56:03 +01002356 switch (*p++) {
2357 case SPOE_SCOPE_PROC: scope = "proc"; break;
2358 case SPOE_SCOPE_SESS: scope = "sess"; break;
2359 case SPOE_SCOPE_TXN : scope = "txn"; break;
2360 case SPOE_SCOPE_REQ : scope = "req"; break;
2361 case SPOE_SCOPE_RES : scope = "res"; break;
2362 default: goto skip;
2363 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002364
Christopher Faulet8ef75252017-02-20 22:56:03 +01002365 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2366 goto skip;
2367 memset(&smp, 0, sizeof(smp));
2368 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002369
Christopher Faulet8ef75252017-02-20 22:56:03 +01002370 if (spoe_decode_data(&p, end, &smp) == -1)
2371 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002372
Christopher Faulet8ef75252017-02-20 22:56:03 +01002373 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2374 " - set-var '%s.%s.%.*s'\n",
2375 (int)now.tv_sec, (int)now.tv_usec,
2376 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2377 __FUNCTION__, s, scope,
2378 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2379 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002380
Christopher Faulet8ef75252017-02-20 22:56:03 +01002381 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002382
Christopher Faulet8ef75252017-02-20 22:56:03 +01002383 ret = (p - *buf);
2384 *buf = p;
2385 return ret;
2386 skip:
2387 return 0;
2388}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002389
Christopher Faulet8ef75252017-02-20 22:56:03 +01002390static inline int
2391spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2392 char **buf, char *end, int dir)
2393{
2394 char *str, *scope, *p = *buf;
2395 struct sample smp;
2396 uint64_t sz;
2397 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002398
Christopher Faulet8ef75252017-02-20 22:56:03 +01002399 if (p + 2 >= end)
2400 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002401
Christopher Faulet8ef75252017-02-20 22:56:03 +01002402 /* UNSET-VAR requires 2 arguments */
2403 if (*p++ != 2)
2404 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002405
Christopher Faulet8ef75252017-02-20 22:56:03 +01002406 switch (*p++) {
2407 case SPOE_SCOPE_PROC: scope = "proc"; break;
2408 case SPOE_SCOPE_SESS: scope = "sess"; break;
2409 case SPOE_SCOPE_TXN : scope = "txn"; break;
2410 case SPOE_SCOPE_REQ : scope = "req"; break;
2411 case SPOE_SCOPE_RES : scope = "res"; break;
2412 default: goto skip;
2413 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002414
Christopher Faulet8ef75252017-02-20 22:56:03 +01002415 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2416 goto skip;
2417 memset(&smp, 0, sizeof(smp));
2418 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002419
Christopher Faulet8ef75252017-02-20 22:56:03 +01002420 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2421 " - unset-var '%s.%s.%.*s'\n",
2422 (int)now.tv_sec, (int)now.tv_usec,
2423 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2424 __FUNCTION__, s, scope,
2425 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2426 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002427
Christopher Faulet8ef75252017-02-20 22:56:03 +01002428 spoe_unset_var(ctx, scope, str, sz, &smp);
2429
2430 ret = (p - *buf);
2431 *buf = p;
2432 return ret;
2433 skip:
2434 return 0;
2435}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002436
Christopher Faulet8ef75252017-02-20 22:56:03 +01002437/* Process SPOE actions for a specific event. It returns 1 on success. If an
2438 * error occurred, 0 is returned. */
2439static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002440spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002441{
2442 char *p, *end;
2443 int ret;
2444
2445 p = ctx->buffer->p;
2446 end = p + ctx->buffer->i;
2447
2448 while (p < end) {
2449 enum spoe_action_type type;
2450
2451 type = *p++;
2452 switch (type) {
2453 case SPOE_ACT_T_SET_VAR:
2454 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2455 if (!ret)
2456 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002457 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002458
Christopher Faulet8ef75252017-02-20 22:56:03 +01002459 case SPOE_ACT_T_UNSET_VAR:
2460 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2461 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002462 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002463 break;
2464
2465 default:
2466 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002467 }
2468 }
2469
2470 return 1;
2471 skip:
2472 return 0;
2473}
2474
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002475/***************************************************************************
2476 * Functions that process SPOE events
2477 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002478static void
2479spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2480 struct spoe_context *ctx, int dir)
2481{
2482 if (!tv_iszero(&ctx->stats.tv_start)) {
2483 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2484 ctx->stats.t_total += ctx->stats.t_process;
2485 tv_zero(&ctx->stats.tv_request);
2486 tv_zero(&ctx->stats.tv_queue);
2487 tv_zero(&ctx->stats.tv_wait);
2488 tv_zero(&ctx->stats.tv_response);
2489 }
2490
2491 if (agent->var_t_process) {
2492 struct sample smp;
2493
2494 memset(&smp, 0, sizeof(smp));
2495 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2496 smp.data.u.sint = ctx->stats.t_process;
2497 smp.data.type = SMP_T_SINT;
2498
2499 spoe_set_var(ctx, "txn", agent->var_t_process,
2500 strlen(agent->var_t_process), &smp);
2501 }
2502
2503 if (agent->var_t_total) {
2504 struct sample smp;
2505
2506 memset(&smp, 0, sizeof(smp));
2507 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2508 smp.data.u.sint = ctx->stats.t_total;
2509 smp.data.type = SMP_T_SINT;
2510
2511 spoe_set_var(ctx, "txn", agent->var_t_total,
2512 strlen(agent->var_t_total), &smp);
2513 }
2514}
2515
2516static void
2517spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2518 struct spoe_context *ctx, int dir)
2519{
2520 if (agent->eps_max > 0)
2521 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2522
2523 if (agent->var_on_error) {
2524 struct sample smp;
2525
2526 memset(&smp, 0, sizeof(smp));
2527 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2528 smp.data.u.sint = ctx->status_code;
2529 smp.data.type = SMP_T_BOOL;
2530
2531 spoe_set_var(ctx, "txn", agent->var_on_error,
2532 strlen(agent->var_on_error), &smp);
2533 }
2534
2535 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2536 ? SPOE_CTX_ST_READY
2537 : SPOE_CTX_ST_NONE);
2538}
2539
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002540static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002541spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002542{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002543 /* If a process is already started for this SPOE context, retry
2544 * later. */
2545 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002546 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002547
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002548 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002549 ctx->stats.tv_start = now;
2550 ctx->stats.tv_request = now;
2551 ctx->stats.t_request = -1;
2552 ctx->stats.t_queue = -1;
2553 ctx->stats.t_waiting = -1;
2554 ctx->stats.t_response = -1;
2555 ctx->stats.t_process = -1;
2556
2557 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002558
Christopher Fauleta1cda022016-12-21 08:58:06 +01002559 /* Set the right flag to prevent request and response processing
2560 * in same time. */
2561 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2562 ? SPOE_CTX_FL_REQ_PROCESS
2563 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002564 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002565}
2566
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002567static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002568spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002569{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002570 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002571
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002572 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2573 return;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002574 HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002575 if (sa) {
2576 if (sa->frag_ctx.ctx == ctx) {
2577 sa->frag_ctx.ctx = NULL;
2578 spoe_wakeup_appctx(sa->owner);
2579 }
2580 else
2581 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002582 }
2583
Christopher Fauleta1cda022016-12-21 08:58:06 +01002584 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002585 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002586 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002587
2588 /* Reset processing timer */
2589 ctx->process_exp = TICK_ETERNITY;
2590
Christopher Faulet8ef75252017-02-20 22:56:03 +01002591 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002592
Christopher Fauletfce747b2018-01-24 15:59:32 +01002593 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002594 ctx->frag_ctx.curmsg = NULL;
2595 ctx->frag_ctx.curarg = NULL;
2596 ctx->frag_ctx.curoff = 0;
2597 ctx->frag_ctx.flags = 0;
2598
Christopher Fauleta1cda022016-12-21 08:58:06 +01002599 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002600 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Christopher Fauletebe13992018-04-26 11:33:44 +02002601 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002602 else
2603 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
2604
Christopher Fauleta1cda022016-12-21 08:58:06 +01002605 LIST_DEL(&ctx->list);
2606 LIST_INIT(&ctx->list);
2607 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002608}
2609
Christopher Faulet58d03682017-09-21 16:57:24 +02002610/* Process a list of SPOE messages. First, this functions will process messages
2611 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2612 * to process corresponding actions. During all the processing, it returns 0
2613 * and it returns 1 when the processing is finished. If an error occurred, -1
2614 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002615static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002616spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2617 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002618{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002619 struct spoe_config *conf = FLT_CONF(ctx->filter);
2620 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002621 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002622
2623 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002624 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002625
2626 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2627 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002628 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002629 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002630 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002631 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002632 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002633 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002634
2635 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002636 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002637 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002638 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002639 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002640 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002641 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002642 goto skip;
2643 }
2644 }
2645
Christopher Fauletf7a30922016-11-10 15:04:51 +01002646 if (!tick_isset(ctx->process_exp)) {
2647 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2648 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2649 ctx->process_exp);
2650 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002651 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002652 if (!ret)
2653 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002654
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002655 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002656 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002657 }
2658
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002659 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002660 if (!tv_iszero(&ctx->stats.tv_request))
2661 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002662 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002663 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002664 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002665 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002666 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002667 if (!ret)
2668 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002669 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002670 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002671 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2672 }
2673
2674 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002675 if (ctx->spoe_appctx)
2676 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002677 ret = 0;
2678 goto out;
2679 }
2680
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002681 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2682 ret = 0;
2683 goto out;
2684 }
2685
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002686 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002687 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002688 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002689 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002690 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002691 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002692 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002693 }
2694
2695 out:
2696 return ret;
2697
Christopher Fauleta1cda022016-12-21 08:58:06 +01002698 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002699 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002700 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002701 spoe_stop_processing(agent, ctx);
2702 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002703
Christopher Fauleta1cda022016-12-21 08:58:06 +01002704 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002705 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002706 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002707 if (ctx->status_code) {
2708 HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
2709 spoe_handle_processing_error(s, agent, ctx, dir);
2710 ret = 1;
2711 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002712 return ret;
2713}
2714
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002715/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2716 * See spoe_process_message for details. */
2717static int
2718spoe_process_group(struct stream *s, struct spoe_context *ctx,
2719 struct spoe_group *group, int dir)
2720{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002721 struct spoe_config *conf = FLT_CONF(ctx->filter);
2722 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002723 int ret;
2724
2725 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2726 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002727 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002728 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2729 group->id);
2730
2731 if (LIST_ISEMPTY(&group->messages))
2732 return 1;
2733
2734 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002735 if (ret && ctx->stats.t_process != -1) {
2736 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002737 " - <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 +01002738 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002739 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002740 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002741 ctx->stats.t_response, ctx->stats.t_process,
2742 agent->counters.idles, agent->counters.applets,
2743 agent->counters.nb_sending, agent->counters.nb_waiting,
2744 agent->counters.nb_errors, agent->counters.nb_processed,
2745 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2746 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2747 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2748 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2749 agent->id, group->id, s->uniq_id, ctx->status_code,
2750 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2751 ctx->stats.t_response, ctx->stats.t_process,
2752 agent->counters.idles, agent->counters.applets,
2753 agent->counters.nb_sending, agent->counters.nb_waiting,
2754 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002755 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002756 return ret;
2757}
2758
Christopher Faulet58d03682017-09-21 16:57:24 +02002759/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2760 * See spoe_process_message for details. */
2761static int
2762spoe_process_event(struct stream *s, struct spoe_context *ctx,
2763 enum spoe_event ev)
2764{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002765 struct spoe_config *conf = FLT_CONF(ctx->filter);
2766 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002767 int dir, ret;
2768
2769 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002770 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002771 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002772 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2773 spoe_event_str[ev]);
2774
2775 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2776
2777 if (LIST_ISEMPTY(&(ctx->events[ev])))
2778 return 1;
2779
2780 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002781 if (ret && ctx->stats.t_process != -1) {
2782 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002783 " - <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 +01002784 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2785 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2786 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002787 ctx->stats.t_response, ctx->stats.t_process,
2788 agent->counters.idles, agent->counters.applets,
2789 agent->counters.nb_sending, agent->counters.nb_waiting,
2790 agent->counters.nb_errors, agent->counters.nb_processed,
2791 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2792 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2793 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2794 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2795 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2796 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2797 ctx->stats.t_response, ctx->stats.t_process,
2798 agent->counters.idles, agent->counters.applets,
2799 agent->counters.nb_sending, agent->counters.nb_waiting,
2800 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002801 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002802 return ret;
2803}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002804
2805/***************************************************************************
2806 * Functions that create/destroy SPOE contexts
2807 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002808static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002809spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002810{
Christopher Faulet600d37e2017-11-10 11:54:58 +01002811 if ((*buf)->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002812 return 1;
2813
Christopher Faulet4596fb72017-01-11 14:05:19 +01002814 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002815 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002816 LIST_DEL(&buffer_wait->list);
2817 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002818 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002819 }
2820
Christopher Faulet4596fb72017-01-11 14:05:19 +01002821 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002822 return 1;
2823
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002824 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002825 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002826 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002827 return 0;
2828}
2829
2830static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002831spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002832{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002833 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002834 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002835 LIST_DEL(&buffer_wait->list);
2836 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002837 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002838 }
2839
2840 /* Release the buffer if needed */
Christopher Faulet600d37e2017-11-10 11:54:58 +01002841 if ((*buf)->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002842 b_free(buf);
Olivier Houchard673867c2018-05-25 16:58:52 +02002843 offer_buffers(buffer_wait->target, tasks_run_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002844 }
2845}
2846
Christopher Faulet4596fb72017-01-11 14:05:19 +01002847static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002848spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002849{
2850 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2851 return 1;
2852}
2853
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002854static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002855spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002856{
2857 struct spoe_config *conf = FLT_CONF(filter);
2858 struct spoe_context *ctx;
2859
Willy Tarreaubafbe012017-11-24 17:34:44 +01002860 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002861 if (ctx == NULL) {
2862 return NULL;
2863 }
2864 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002865 ctx->filter = filter;
2866 ctx->state = SPOE_CTX_ST_NONE;
2867 ctx->status_code = SPOE_CTX_ERR_NONE;
2868 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002869 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002870 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002871 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002872 LIST_INIT(&ctx->buffer_wait.list);
2873 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002874 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002875 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002876
Christopher Fauletf7a30922016-11-10 15:04:51 +01002877 ctx->stream_id = 0;
2878 ctx->frame_id = 1;
2879 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002880
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002881 tv_zero(&ctx->stats.tv_start);
2882 tv_zero(&ctx->stats.tv_request);
2883 tv_zero(&ctx->stats.tv_queue);
2884 tv_zero(&ctx->stats.tv_wait);
2885 tv_zero(&ctx->stats.tv_response);
2886 ctx->stats.t_request = -1;
2887 ctx->stats.t_queue = -1;
2888 ctx->stats.t_waiting = -1;
2889 ctx->stats.t_response = -1;
2890 ctx->stats.t_process = -1;
2891 ctx->stats.t_total = 0;
2892
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002893 ctx->strm = s;
2894 ctx->state = SPOE_CTX_ST_READY;
2895 filter->ctx = ctx;
2896
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002897 return ctx;
2898}
2899
2900static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002901spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002902{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002903 struct spoe_config *conf = FLT_CONF(filter);
2904 struct spoe_context *ctx = filter->ctx;
2905
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002906 if (!ctx)
2907 return;
2908
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002909 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002910 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002911 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002912}
2913
2914static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002915spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002916{
2917 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002918 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002919
2920 tv_zero(&ctx->stats.tv_start);
2921 tv_zero(&ctx->stats.tv_request);
2922 tv_zero(&ctx->stats.tv_queue);
2923 tv_zero(&ctx->stats.tv_wait);
2924 tv_zero(&ctx->stats.tv_response);
2925 ctx->stats.t_request = -1;
2926 ctx->stats.t_queue = -1;
2927 ctx->stats.t_waiting = -1;
2928 ctx->stats.t_response = -1;
2929 ctx->stats.t_process = -1;
2930 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002931}
2932
2933
2934/***************************************************************************
2935 * Hooks that manage the filter lifecycle (init/check/deinit)
2936 **************************************************************************/
2937/* Signal handler: Do a soft stop, wakeup SPOE applet */
2938static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002939spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002940{
2941 struct proxy *p;
2942
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002943 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002944 while (p) {
2945 struct flt_conf *fconf;
2946
2947 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002948 struct spoe_config *conf;
2949 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002950 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002951 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002952
Christopher Faulet3b386a32017-02-23 10:17:15 +01002953 if (fconf->id != spoe_filter_id)
2954 continue;
2955
2956 conf = fconf->conf;
2957 agent = conf->agent;
2958
Christopher Faulet24289f22017-09-25 14:48:02 +02002959 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002960 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002961 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2962 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002963 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002964 }
2965 }
2966 p = p->next;
2967 }
2968}
2969
2970
2971/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2972static int
2973spoe_init(struct proxy *px, struct flt_conf *fconf)
2974{
2975 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002976
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002977 /* conf->agent_fe was already initialized during the config
2978 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002979 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002980 conf->agent_fe.cap = PR_CAP_FE;
2981 conf->agent_fe.mode = PR_MODE_TCP;
2982 conf->agent_fe.maxconn = 0;
2983 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2984 conf->agent_fe.conn_retries = CONN_RETRIES;
2985 conf->agent_fe.accept = frontend_accept;
2986 conf->agent_fe.srv = NULL;
2987 conf->agent_fe.timeout.client = TICK_ETERNITY;
2988 conf->agent_fe.default_target = &spoe_applet.obj_type;
2989 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2990
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002991 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002992 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002993 sighandler_registered = 1;
2994 }
2995
2996 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002997}
2998
2999/* Free ressources allocated by the SPOE filter. */
3000static void
3001spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3002{
3003 struct spoe_config *conf = fconf->conf;
3004
3005 if (conf) {
3006 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003007
Christopher Faulet8ef75252017-02-20 22:56:03 +01003008 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003009 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003010 free(conf);
3011 }
3012 fconf->conf = NULL;
3013}
3014
3015/* Check configuration of a SPOE filter for a specified proxy.
3016 * Return 1 on error, else 0. */
3017static int
3018spoe_check(struct proxy *px, struct flt_conf *fconf)
3019{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003020 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003021 struct spoe_config *conf = fconf->conf;
3022 struct proxy *target;
3023
Christopher Faulet7ee86672017-09-19 11:08:28 +02003024 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3025 * are uniq */
3026 list_for_each_entry(f, &px->filter_configs, list) {
3027 struct spoe_config *c = f->conf;
3028
3029 /* This is not an SPOE filter */
3030 if (f->id != spoe_filter_id)
3031 continue;
3032 /* This is the current SPOE filter */
3033 if (f == fconf)
3034 continue;
3035
3036 /* Check engine Id. It should be uniq */
3037 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003038 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3039 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003040 return 1;
3041 }
3042 }
3043
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003044 target = proxy_be_by_name(conf->agent->b.name);
3045 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003046 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3047 " declared at %s:%d.\n",
3048 px->id, conf->agent->b.name, conf->agent->id,
3049 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003050 return 1;
3051 }
3052 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003053 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3054 " at %s:%d does not support HTTP mode.\n",
3055 px->id, target->id, conf->agent->id,
3056 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003057 return 1;
3058 }
3059
3060 free(conf->agent->b.name);
3061 conf->agent->b.name = NULL;
3062 conf->agent->b.be = target;
3063 return 0;
3064}
3065
3066/**************************************************************************
3067 * Hooks attached to a stream
3068 *************************************************************************/
3069/* Called when a filter instance is created and attach to a stream. It creates
3070 * the context that will be used to process this stream. */
3071static int
3072spoe_start(struct stream *s, struct filter *filter)
3073{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003074 struct spoe_config *conf = FLT_CONF(filter);
3075 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003076 struct spoe_context *ctx;
3077
3078 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003079 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003080 __FUNCTION__, s);
3081
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003082 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003083 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3084 " - failed to create SPOE context\n",
3085 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003086 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003087 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003088 "SPOE: [%s] failed to create SPOE context\n",
3089 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003090 return 0;
3091 }
3092
Christopher Faulet11610f32017-09-21 10:23:10 +02003093 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003094 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3095
Christopher Faulet11610f32017-09-21 10:23:10 +02003096 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003097 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3098
Christopher Faulet11610f32017-09-21 10:23:10 +02003099 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003100 filter->pre_analyzers |= AN_RES_INSPECT;
3101
Christopher Faulet11610f32017-09-21 10:23:10 +02003102 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003103 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3104
Christopher Faulet11610f32017-09-21 10:23:10 +02003105 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003106 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3107
Christopher Faulet11610f32017-09-21 10:23:10 +02003108 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003109 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3110
3111 return 1;
3112}
3113
3114/* Called when a filter instance is detached from a stream. It release the
3115 * attached SPOE context. */
3116static void
3117spoe_stop(struct stream *s, struct filter *filter)
3118{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003119 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3120 (int)now.tv_sec, (int)now.tv_usec,
3121 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3122 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003123 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003124}
3125
Christopher Fauletf7a30922016-11-10 15:04:51 +01003126
3127/*
3128 * Called when the stream is woken up because of expired timer.
3129 */
3130static void
3131spoe_check_timeouts(struct stream *s, struct filter *filter)
3132{
3133 struct spoe_context *ctx = filter->ctx;
3134
Christopher Fauletac580602018-03-20 16:09:20 +01003135 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003136 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003137}
3138
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003139/* Called when we are ready to filter data on a channel */
3140static int
3141spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3142{
3143 struct spoe_context *ctx = filter->ctx;
3144 int ret = 1;
3145
3146 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3147 " - ctx-flags=0x%08x\n",
3148 (int)now.tv_sec, (int)now.tv_usec,
3149 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3150 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3151
Christopher Fauletb067b062017-01-04 16:39:11 +01003152 if (ctx->state == SPOE_CTX_ST_NONE)
3153 goto out;
3154
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003155 if (!(chn->flags & CF_ISRESP)) {
3156 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3157 chn->analysers |= AN_REQ_INSPECT_FE;
3158 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3159 chn->analysers |= AN_REQ_INSPECT_BE;
3160
3161 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3162 goto out;
3163
3164 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003165 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003166 if (!ret)
3167 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003168 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3169 }
3170 else {
3171 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3172 chn->analysers |= AN_RES_INSPECT;
3173
3174 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3175 goto out;
3176
Christopher Faulet8ef75252017-02-20 22:56:03 +01003177 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003178 if (!ret) {
3179 channel_dont_read(chn);
3180 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003181 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003182 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003183 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003184 }
3185
3186 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003187 return ret;
3188}
3189
3190/* Called before a processing happens on a given channel */
3191static int
3192spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3193 struct channel *chn, unsigned an_bit)
3194{
3195 struct spoe_context *ctx = filter->ctx;
3196 int ret = 1;
3197
3198 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3199 " - ctx-flags=0x%08x - ana=0x%08x\n",
3200 (int)now.tv_sec, (int)now.tv_usec,
3201 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3202 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3203 ctx->flags, an_bit);
3204
Christopher Fauletb067b062017-01-04 16:39:11 +01003205 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003206 goto out;
3207
3208 switch (an_bit) {
3209 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003210 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003211 break;
3212 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003213 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003214 break;
3215 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003216 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003217 break;
3218 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003219 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003220 break;
3221 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003222 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003223 break;
3224 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003225 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003226 break;
3227 }
3228
3229 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003230 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003231 channel_dont_read(chn);
3232 channel_dont_close(chn);
3233 }
3234 return ret;
3235}
3236
3237/* Called when the filtering on the channel ends. */
3238static int
3239spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3240{
3241 struct spoe_context *ctx = filter->ctx;
3242
3243 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3244 " - ctx-flags=0x%08x\n",
3245 (int)now.tv_sec, (int)now.tv_usec,
3246 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3247 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3248
3249 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003250 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003251 }
3252
3253 return 1;
3254}
3255
3256/********************************************************************
3257 * Functions that manage the filter initialization
3258 ********************************************************************/
3259struct flt_ops spoe_ops = {
3260 /* Manage SPOE filter, called for each filter declaration */
3261 .init = spoe_init,
3262 .deinit = spoe_deinit,
3263 .check = spoe_check,
3264
3265 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003266 .attach = spoe_start,
3267 .detach = spoe_stop,
3268 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003269
3270 /* Handle channels activity */
3271 .channel_start_analyze = spoe_start_analyze,
3272 .channel_pre_analyze = spoe_chn_pre_analyze,
3273 .channel_end_analyze = spoe_end_analyze,
3274};
3275
3276
3277static int
3278cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3279{
3280 const char *err;
3281 int i, err_code = 0;
3282
3283 if ((cfg_scope == NULL && curengine != NULL) ||
3284 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003285 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003286 goto out;
3287
3288 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3289 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003290 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3291 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003292 err_code |= ERR_ALERT | ERR_ABORT;
3293 goto out;
3294 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003295 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3296 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003297 goto out;
3298 }
3299
3300 err = invalid_char(args[1]);
3301 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003302 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3303 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003304 err_code |= ERR_ALERT | ERR_ABORT;
3305 goto out;
3306 }
3307
3308 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003309 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3310 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003311 err_code |= ERR_ALERT | ERR_ABORT;
3312 goto out;
3313 }
3314 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003315 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003316 err_code |= ERR_ALERT | ERR_ABORT;
3317 goto out;
3318 }
3319
3320 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003321
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003322 curagent->conf.file = strdup(file);
3323 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003324
3325 curagent->timeout.hello = TICK_ETERNITY;
3326 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003327 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003328
3329 curagent->engine_id = NULL;
3330 curagent->var_pfx = NULL;
3331 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003332 curagent->var_t_process = NULL;
3333 curagent->var_t_total = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003334 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3335 if (global.nbthread == 1)
3336 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003337 curagent->cps_max = 0;
3338 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003339 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003340 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003341
3342 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003343 LIST_INIT(&curagent->events[i]);
3344 LIST_INIT(&curagent->groups);
3345 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003346
Christopher Faulet24289f22017-09-25 14:48:02 +02003347 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003348 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003349 err_code |= ERR_ALERT | ERR_ABORT;
3350 goto out;
3351 }
3352 for (i = 0; i < global.nbthread; ++i) {
3353 curagent->rt[i].frame_size = curagent->max_frame_size;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003354 curagent->rt[i].processing = 0;
Christopher Faulet24289f22017-09-25 14:48:02 +02003355 LIST_INIT(&curagent->rt[i].applets);
3356 LIST_INIT(&curagent->rt[i].sending_queue);
3357 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003358 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003359 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003360 }
3361 else if (!strcmp(args[0], "use-backend")) {
3362 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003363 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3364 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003365 err_code |= ERR_ALERT | ERR_FATAL;
3366 goto out;
3367 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003368 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003369 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003370 free(curagent->b.name);
3371 curagent->b.name = strdup(args[1]);
3372 }
3373 else if (!strcmp(args[0], "messages")) {
3374 int cur_arg = 1;
3375 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003376 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003377
Christopher Faulet11610f32017-09-21 10:23:10 +02003378 list_for_each_entry(ph, &curmphs, list) {
3379 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003380 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3381 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003382 err_code |= ERR_ALERT | ERR_FATAL;
3383 goto out;
3384 }
3385 }
3386
Christopher Faulet11610f32017-09-21 10:23:10 +02003387 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003388 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003389 err_code |= ERR_ALERT | ERR_ABORT;
3390 goto out;
3391 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003392 ph->id = strdup(args[cur_arg]);
3393 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003394 cur_arg++;
3395 }
3396 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003397 else if (!strcmp(args[0], "groups")) {
3398 int cur_arg = 1;
3399 while (*args[cur_arg]) {
3400 struct spoe_placeholder *ph = NULL;
3401
3402 list_for_each_entry(ph, &curgphs, list) {
3403 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003404 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3405 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003406 err_code |= ERR_ALERT | ERR_FATAL;
3407 goto out;
3408 }
3409 }
3410
3411 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003412 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003413 err_code |= ERR_ALERT | ERR_ABORT;
3414 goto out;
3415 }
3416 ph->id = strdup(args[cur_arg]);
3417 LIST_ADDQ(&curgphs, &ph->list);
3418 cur_arg++;
3419 }
3420 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003421 else if (!strcmp(args[0], "timeout")) {
3422 unsigned int *tv = NULL;
3423 const char *res;
3424 unsigned timeout;
3425
3426 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003427 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3428 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003429 err_code |= ERR_ALERT | ERR_FATAL;
3430 goto out;
3431 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003432 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3433 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003434 if (!strcmp(args[1], "hello"))
3435 tv = &curagent->timeout.hello;
3436 else if (!strcmp(args[1], "idle"))
3437 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003438 else if (!strcmp(args[1], "processing"))
3439 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003440 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003441 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3442 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003443 err_code |= ERR_ALERT | ERR_FATAL;
3444 goto out;
3445 }
3446 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003447 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3448 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003449 err_code |= ERR_ALERT | ERR_FATAL;
3450 goto out;
3451 }
3452 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3453 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003454 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3455 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003456 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003457 goto out;
3458 }
3459 *tv = MS_TO_TICKS(timeout);
3460 }
3461 else if (!strcmp(args[0], "option")) {
3462 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003463 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3464 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003465 err_code |= ERR_ALERT | ERR_FATAL;
3466 goto out;
3467 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003468
Christopher Faulet305c6072017-02-23 16:17:53 +01003469 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003470 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003471 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003472 if (kwm == 1)
3473 curagent->flags &= ~SPOE_FL_PIPELINING;
3474 else
3475 curagent->flags |= SPOE_FL_PIPELINING;
3476 goto out;
3477 }
3478 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003479 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003480 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003481 if (kwm == 1)
3482 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003483 else {
3484 if (global.nbthread == 1)
3485 curagent->flags |= SPOE_FL_ASYNC;
3486 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003487 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3488 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003489 err_code |= ERR_WARN;
3490 }
3491 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003492 goto out;
3493 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003494 else if (!strcmp(args[1], "send-frag-payload")) {
3495 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3496 goto out;
3497 if (kwm == 1)
3498 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3499 else
3500 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3501 goto out;
3502 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003503 else if (!strcmp(args[1], "dontlog-normal")) {
3504 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3505 goto out;
3506 if (kwm == 1)
3507 curpxopts2 &= ~PR_O2_NOLOGNORM;
3508 else
3509 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003510 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003511 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003512
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003513 /* Following options does not support negation */
3514 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003515 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3516 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003517 err_code |= ERR_ALERT | ERR_FATAL;
3518 goto out;
3519 }
3520
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003521 if (!strcmp(args[1], "var-prefix")) {
3522 char *tmp;
3523
3524 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003525 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3526 file, linenum, args[0],
3527 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003528 err_code |= ERR_ALERT | ERR_FATAL;
3529 goto out;
3530 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003531 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3532 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003533 tmp = args[2];
3534 while (*tmp) {
3535 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003536 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003537 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003538 err_code |= ERR_ALERT | ERR_FATAL;
3539 goto out;
3540 }
3541 tmp++;
3542 }
3543 curagent->var_pfx = strdup(args[2]);
3544 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003545 else if (!strcmp(args[1], "force-set-var")) {
3546 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3547 goto out;
3548 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3549 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003550 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003551 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003552 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003553 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3554 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003555 else if (!strcmp(args[1], "set-on-error")) {
3556 char *tmp;
3557
3558 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003559 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3560 file, linenum, args[0],
3561 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003562 err_code |= ERR_ALERT | ERR_FATAL;
3563 goto out;
3564 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003565 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3566 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003567 tmp = args[2];
3568 while (*tmp) {
3569 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003570 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003571 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003572 err_code |= ERR_ALERT | ERR_FATAL;
3573 goto out;
3574 }
3575 tmp++;
3576 }
3577 curagent->var_on_error = strdup(args[2]);
3578 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003579 else if (!strcmp(args[1], "set-process-time")) {
3580 char *tmp;
3581
3582 if (!*args[2]) {
3583 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3584 file, linenum, args[0],
3585 args[1]);
3586 err_code |= ERR_ALERT | ERR_FATAL;
3587 goto out;
3588 }
3589 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3590 goto out;
3591 tmp = args[2];
3592 while (*tmp) {
3593 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003594 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003595 file, linenum, args[0], args[1]);
3596 err_code |= ERR_ALERT | ERR_FATAL;
3597 goto out;
3598 }
3599 tmp++;
3600 }
3601 curagent->var_t_process = strdup(args[2]);
3602 }
3603 else if (!strcmp(args[1], "set-total-time")) {
3604 char *tmp;
3605
3606 if (!*args[2]) {
3607 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3608 file, linenum, args[0],
3609 args[1]);
3610 err_code |= ERR_ALERT | ERR_FATAL;
3611 goto out;
3612 }
3613 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3614 goto out;
3615 tmp = args[2];
3616 while (*tmp) {
3617 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003618 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003619 file, linenum, args[0], args[1]);
3620 err_code |= ERR_ALERT | ERR_FATAL;
3621 goto out;
3622 }
3623 tmp++;
3624 }
3625 curagent->var_t_total = strdup(args[2]);
3626 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003627 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003628 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3629 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003630 err_code |= ERR_ALERT | ERR_FATAL;
3631 goto out;
3632 }
Christopher Faulet48026722016-11-16 15:01:12 +01003633 }
3634 else if (!strcmp(args[0], "maxconnrate")) {
3635 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003636 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3637 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003638 err_code |= ERR_ALERT | ERR_FATAL;
3639 goto out;
3640 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003641 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003642 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003643 curagent->cps_max = atol(args[1]);
3644 }
3645 else if (!strcmp(args[0], "maxerrrate")) {
3646 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003647 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3648 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003649 err_code |= ERR_ALERT | ERR_FATAL;
3650 goto out;
3651 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003652 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003653 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003654 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003655 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003656 else if (!strcmp(args[0], "max-frame-size")) {
3657 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003658 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3659 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003660 err_code |= ERR_ALERT | ERR_FATAL;
3661 goto out;
3662 }
3663 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3664 goto out;
3665 curagent->max_frame_size = atol(args[1]);
3666 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3667 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003668 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3669 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003670 err_code |= ERR_ALERT | ERR_FATAL;
3671 goto out;
3672 }
3673 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003674 else if (!strcmp(args[0], "max-waiting-frames")) {
3675 if (!*args[1]) {
3676 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3677 file, linenum, args[0]);
3678 err_code |= ERR_ALERT | ERR_FATAL;
3679 goto out;
3680 }
3681 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3682 goto out;
3683 curagent->max_fpa = atol(args[1]);
3684 if (curagent->max_fpa < 1) {
3685 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3686 file, linenum, args[0]);
3687 err_code |= ERR_ALERT | ERR_FATAL;
3688 goto out;
3689 }
3690 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003691 else if (!strcmp(args[0], "register-var-names")) {
3692 int cur_arg;
3693
3694 if (!*args[1]) {
3695 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3696 file, linenum, args[0]);
3697 err_code |= ERR_ALERT | ERR_FATAL;
3698 goto out;
3699 }
3700 cur_arg = 1;
3701 while (*args[cur_arg]) {
3702 struct spoe_var_placeholder *vph;
3703
3704 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3705 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3706 err_code |= ERR_ALERT | ERR_ABORT;
3707 goto out;
3708 }
3709 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3710 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3711 err_code |= ERR_ALERT | ERR_ABORT;
3712 goto out;
3713 }
3714 LIST_ADDQ(&curvars, &vph->list);
3715 cur_arg++;
3716 }
3717 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003718 else if (!strcmp(args[0], "log")) {
3719 char *errmsg = NULL;
3720
3721 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3722 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3723 err_code |= ERR_ALERT | ERR_FATAL;
3724 goto out;
3725 }
3726 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003727 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003728 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3729 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003730 err_code |= ERR_ALERT | ERR_FATAL;
3731 goto out;
3732 }
3733 out:
3734 return err_code;
3735}
Christopher Faulet11610f32017-09-21 10:23:10 +02003736static int
3737cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3738{
3739 struct spoe_group *grp;
3740 const char *err;
3741 int err_code = 0;
3742
3743 if ((cfg_scope == NULL && curengine != NULL) ||
3744 (cfg_scope != NULL && curengine == NULL) ||
3745 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3746 goto out;
3747
3748 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3749 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003750 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3751 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003752 err_code |= ERR_ALERT | ERR_ABORT;
3753 goto out;
3754 }
3755 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3756 err_code |= ERR_ABORT;
3757 goto out;
3758 }
3759
3760 err = invalid_char(args[1]);
3761 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003762 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3763 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003764 err_code |= ERR_ALERT | ERR_ABORT;
3765 goto out;
3766 }
3767
3768 list_for_each_entry(grp, &curgrps, list) {
3769 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003770 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3771 " name as another one declared at %s:%d.\n",
3772 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003773 err_code |= ERR_ALERT | ERR_FATAL;
3774 goto out;
3775 }
3776 }
3777
3778 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003779 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003780 err_code |= ERR_ALERT | ERR_ABORT;
3781 goto out;
3782 }
3783
3784 curgrp->id = strdup(args[1]);
3785 curgrp->conf.file = strdup(file);
3786 curgrp->conf.line = linenum;
3787 LIST_INIT(&curgrp->phs);
3788 LIST_INIT(&curgrp->messages);
3789 LIST_ADDQ(&curgrps, &curgrp->list);
3790 }
3791 else if (!strcmp(args[0], "messages")) {
3792 int cur_arg = 1;
3793 while (*args[cur_arg]) {
3794 struct spoe_placeholder *ph = NULL;
3795
3796 list_for_each_entry(ph, &curgrp->phs, list) {
3797 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003798 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3799 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003800 err_code |= ERR_ALERT | ERR_FATAL;
3801 goto out;
3802 }
3803 }
3804
3805 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003806 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003807 err_code |= ERR_ALERT | ERR_ABORT;
3808 goto out;
3809 }
3810 ph->id = strdup(args[cur_arg]);
3811 LIST_ADDQ(&curgrp->phs, &ph->list);
3812 cur_arg++;
3813 }
3814 }
3815 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003816 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3817 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003818 err_code |= ERR_ALERT | ERR_FATAL;
3819 goto out;
3820 }
3821 out:
3822 return err_code;
3823}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003824
3825static int
3826cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3827{
3828 struct spoe_message *msg;
3829 struct spoe_arg *arg;
3830 const char *err;
3831 char *errmsg = NULL;
3832 int err_code = 0;
3833
3834 if ((cfg_scope == NULL && curengine != NULL) ||
3835 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003836 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003837 goto out;
3838
3839 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3840 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003841 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3842 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003843 err_code |= ERR_ALERT | ERR_ABORT;
3844 goto out;
3845 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003846 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3847 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003848 goto out;
3849 }
3850
3851 err = invalid_char(args[1]);
3852 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003853 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3854 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003855 err_code |= ERR_ALERT | ERR_ABORT;
3856 goto out;
3857 }
3858
3859 list_for_each_entry(msg, &curmsgs, list) {
3860 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003861 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3862 " name as another one declared at %s:%d.\n",
3863 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003864 err_code |= ERR_ALERT | ERR_FATAL;
3865 goto out;
3866 }
3867 }
3868
3869 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003870 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003871 err_code |= ERR_ALERT | ERR_ABORT;
3872 goto out;
3873 }
3874
3875 curmsg->id = strdup(args[1]);
3876 curmsg->id_len = strlen(curmsg->id);
3877 curmsg->event = SPOE_EV_NONE;
3878 curmsg->conf.file = strdup(file);
3879 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003880 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003881 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003882 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003883 LIST_INIT(&curmsg->by_evt);
3884 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003885 LIST_ADDQ(&curmsgs, &curmsg->list);
3886 }
3887 else if (!strcmp(args[0], "args")) {
3888 int cur_arg = 1;
3889
3890 curproxy->conf.args.ctx = ARGC_SPOE;
3891 curproxy->conf.args.file = file;
3892 curproxy->conf.args.line = linenum;
3893 while (*args[cur_arg]) {
3894 char *delim = strchr(args[cur_arg], '=');
3895 int idx = 0;
3896
3897 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003898 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003899 err_code |= ERR_ALERT | ERR_ABORT;
3900 goto out;
3901 }
3902
3903 if (!delim) {
3904 arg->name = NULL;
3905 arg->name_len = 0;
3906 delim = args[cur_arg];
3907 }
3908 else {
3909 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3910 arg->name_len = delim - args[cur_arg];
3911 delim++;
3912 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003913 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3914 &idx, file, linenum, &errmsg,
3915 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003916 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003917 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003918 err_code |= ERR_ALERT | ERR_FATAL;
3919 free(arg->name);
3920 free(arg);
3921 goto out;
3922 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003923 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003924 LIST_ADDQ(&curmsg->args, &arg->list);
3925 cur_arg++;
3926 }
3927 curproxy->conf.args.file = NULL;
3928 curproxy->conf.args.line = 0;
3929 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003930 else if (!strcmp(args[0], "acl")) {
3931 err = invalid_char(args[1]);
3932 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003933 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3934 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003935 err_code |= ERR_ALERT | ERR_FATAL;
3936 goto out;
3937 }
3938 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003939 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3940 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003941 err_code |= ERR_ALERT | ERR_FATAL;
3942 goto out;
3943 }
3944 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003945 else if (!strcmp(args[0], "event")) {
3946 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003947 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003948 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003949 goto out;
3950 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003951 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3952 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003953
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003954 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3955 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3956 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3957 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3958
3959 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3960 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3961 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3962 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3963 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3964 curmsg->event = SPOE_EV_ON_TCP_RSP;
3965
3966 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3967 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3968 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3969 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3970 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3971 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3972 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003973 ha_alert("parsing [%s:%d] : unkown event '%s'.\n",
3974 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003975 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003976 goto out;
3977 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003978
3979 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3980 struct acl_cond *cond;
3981
3982 cond = build_acl_cond(file, linenum, &curmsg->acls,
3983 curproxy, (const char **)args+2,
3984 &errmsg);
3985 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003986 ha_alert("parsing [%s:%d] : error detected while "
3987 "parsing an 'event %s' condition : %s.\n",
3988 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003989 err_code |= ERR_ALERT | ERR_FATAL;
3990 goto out;
3991 }
3992 curmsg->cond = cond;
3993 }
3994 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003995 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3996 "or 'unless' followed by a condition but found '%s'.\n",
3997 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003998 err_code |= ERR_ALERT | ERR_FATAL;
3999 goto out;
4000 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004001 }
4002 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004003 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4004 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004005 err_code |= ERR_ALERT | ERR_FATAL;
4006 goto out;
4007 }
4008 out:
4009 free(errmsg);
4010 return err_code;
4011}
4012
4013/* Return -1 on error, else 0 */
4014static int
4015parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4016 struct flt_conf *fconf, char **err, void *private)
4017{
4018 struct list backup_sections;
4019 struct spoe_config *conf;
4020 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004021 struct spoe_group *grp, *grpback;
4022 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004023 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004024 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004025 char *file = NULL, *engine = NULL;
4026 int ret, pos = *cur_arg + 1;
4027
Christopher Faulet84c844e2018-03-23 14:37:14 +01004028 LIST_INIT(&curmsgs);
4029 LIST_INIT(&curgrps);
4030 LIST_INIT(&curmphs);
4031 LIST_INIT(&curgphs);
4032 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004033 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004034 curpxopts = 0;
4035 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004036
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004037 conf = calloc(1, sizeof(*conf));
4038 if (conf == NULL) {
4039 memprintf(err, "%s: out of memory", args[*cur_arg]);
4040 goto error;
4041 }
4042 conf->proxy = px;
4043
4044 while (*args[pos]) {
4045 if (!strcmp(args[pos], "config")) {
4046 if (!*args[pos+1]) {
4047 memprintf(err, "'%s' : '%s' option without value",
4048 args[*cur_arg], args[pos]);
4049 goto error;
4050 }
4051 file = args[pos+1];
4052 pos += 2;
4053 }
4054 else if (!strcmp(args[pos], "engine")) {
4055 if (!*args[pos+1]) {
4056 memprintf(err, "'%s' : '%s' option without value",
4057 args[*cur_arg], args[pos]);
4058 goto error;
4059 }
4060 engine = args[pos+1];
4061 pos += 2;
4062 }
4063 else {
4064 memprintf(err, "unknown keyword '%s'", args[pos]);
4065 goto error;
4066 }
4067 }
4068 if (file == NULL) {
4069 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4070 goto error;
4071 }
4072
4073 /* backup sections and register SPOE sections */
4074 LIST_INIT(&backup_sections);
4075 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004076 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4077 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004078 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004079
4080 /* Parse SPOE filter configuration file */
4081 curengine = engine;
4082 curproxy = px;
4083 curagent = NULL;
4084 curmsg = NULL;
4085 ret = readcfgfile(file);
4086 curproxy = NULL;
4087
4088 /* unregister SPOE sections and restore previous sections */
4089 cfg_unregister_sections();
4090 cfg_restore_sections(&backup_sections);
4091
4092 if (ret == -1) {
4093 memprintf(err, "Could not open configuration file %s : %s",
4094 file, strerror(errno));
4095 goto error;
4096 }
4097 if (ret & (ERR_ABORT|ERR_FATAL)) {
4098 memprintf(err, "Error(s) found in configuration file %s", file);
4099 goto error;
4100 }
4101
4102 /* Check SPOE agent */
4103 if (curagent == NULL) {
4104 memprintf(err, "No SPOE agent found in file %s", file);
4105 goto error;
4106 }
4107 if (curagent->b.name == NULL) {
4108 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4109 curagent->id, curagent->conf.file, curagent->conf.line);
4110 goto error;
4111 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004112 if (curagent->timeout.hello == TICK_ETERNITY ||
4113 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004114 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004115 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4116 " | While not properly invalid, you will certainly encounter various problems\n"
4117 " | with such a configuration. To fix this, please ensure that all following\n"
4118 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4119 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004120 }
4121 if (curagent->var_pfx == NULL) {
4122 char *tmp = curagent->id;
4123
4124 while (*tmp) {
4125 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
4126 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4127 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4128 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4129 goto error;
4130 }
4131 tmp++;
4132 }
4133 curagent->var_pfx = strdup(curagent->id);
4134 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01004135 if (curagent->engine_id == NULL)
4136 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004137
Christopher Fauletb7426d12018-03-21 14:12:17 +01004138 if (curagent->var_on_error) {
4139 struct arg arg;
4140
4141 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4142 curagent->var_pfx, curagent->var_on_error);
4143
4144 arg.type = ARGT_STR;
4145 arg.data.str.str = trash.str;
4146 arg.data.str.len = trash.len;
4147 if (!vars_check_arg(&arg, err)) {
4148 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4149 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4150 goto error;
4151 }
4152 }
4153
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004154 if (curagent->var_t_process) {
4155 struct arg arg;
4156
4157 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4158 curagent->var_pfx, curagent->var_t_process);
4159
4160 arg.type = ARGT_STR;
4161 arg.data.str.str = trash.str;
4162 arg.data.str.len = trash.len;
4163 if (!vars_check_arg(&arg, err)) {
4164 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4165 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4166 goto error;
4167 }
4168 }
4169
4170 if (curagent->var_t_total) {
4171 struct arg arg;
4172
4173 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4174 curagent->var_pfx, curagent->var_t_total);
4175
4176 arg.type = ARGT_STR;
4177 arg.data.str.str = trash.str;
4178 arg.data.str.len = trash.len;
4179 if (!vars_check_arg(&arg, err)) {
4180 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4181 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4182 goto error;
4183 }
4184 }
4185
Christopher Faulet11610f32017-09-21 10:23:10 +02004186 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004187 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4188 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004189 goto finish;
4190 }
4191
Christopher Faulet11610f32017-09-21 10:23:10 +02004192 /* Replace placeholders by the corresponding messages for the SPOE
4193 * agent */
4194 list_for_each_entry(ph, &curmphs, list) {
4195 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004196 struct spoe_arg *arg;
4197 unsigned int where;
4198
Christopher Faulet11610f32017-09-21 10:23:10 +02004199 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004200 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4201 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4202 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4203 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4204 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4205 }
4206 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4207 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4208 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004209 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4210 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004211 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004212 }
4213 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004214 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4215 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004216 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004217 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004218
4219 where = 0;
4220 switch (msg->event) {
4221 case SPOE_EV_ON_CLIENT_SESS:
4222 where |= SMP_VAL_FE_CON_ACC;
4223 break;
4224
4225 case SPOE_EV_ON_TCP_REQ_FE:
4226 where |= SMP_VAL_FE_REQ_CNT;
4227 break;
4228
4229 case SPOE_EV_ON_HTTP_REQ_FE:
4230 where |= SMP_VAL_FE_HRQ_HDR;
4231 break;
4232
4233 case SPOE_EV_ON_TCP_REQ_BE:
4234 if (px->cap & PR_CAP_FE)
4235 where |= SMP_VAL_FE_REQ_CNT;
4236 if (px->cap & PR_CAP_BE)
4237 where |= SMP_VAL_BE_REQ_CNT;
4238 break;
4239
4240 case SPOE_EV_ON_HTTP_REQ_BE:
4241 if (px->cap & PR_CAP_FE)
4242 where |= SMP_VAL_FE_HRQ_HDR;
4243 if (px->cap & PR_CAP_BE)
4244 where |= SMP_VAL_BE_HRQ_HDR;
4245 break;
4246
4247 case SPOE_EV_ON_SERVER_SESS:
4248 where |= SMP_VAL_BE_SRV_CON;
4249 break;
4250
4251 case SPOE_EV_ON_TCP_RSP:
4252 if (px->cap & PR_CAP_FE)
4253 where |= SMP_VAL_FE_RES_CNT;
4254 if (px->cap & PR_CAP_BE)
4255 where |= SMP_VAL_BE_RES_CNT;
4256 break;
4257
4258 case SPOE_EV_ON_HTTP_RSP:
4259 if (px->cap & PR_CAP_FE)
4260 where |= SMP_VAL_FE_HRS_HDR;
4261 if (px->cap & PR_CAP_BE)
4262 where |= SMP_VAL_BE_HRS_HDR;
4263 break;
4264
4265 default:
4266 break;
4267 }
4268
4269 list_for_each_entry(arg, &msg->args, list) {
4270 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004271 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004272 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004273 "none of which is available here ('%s')",
4274 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004275 sample_ckp_names(arg->expr->fetch->use),
4276 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004277 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004278 }
4279 }
4280
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004281 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004282 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4283 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004284 }
4285 }
4286 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004287 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004288 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004289 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004290 continue;
4291 }
4292
Christopher Faulet11610f32017-09-21 10:23:10 +02004293 /* Replace placeholders by the corresponding groups for the SPOE
4294 * agent */
4295 list_for_each_entry(ph, &curgphs, list) {
4296 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4297 if (!strcmp(grp->id, ph->id)) {
4298 grp->agent = curagent;
4299 LIST_DEL(&grp->list);
4300 LIST_ADDQ(&curagent->groups, &grp->list);
4301 goto next_aph;
4302 }
4303 }
4304 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4305 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4306 goto error;
4307 next_aph:
4308 continue;
4309 }
4310
4311 /* Replace placeholders by the corresponding message for each SPOE
4312 * group of the SPOE agent */
4313 list_for_each_entry(grp, &curagent->groups, list) {
4314 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4315 list_for_each_entry(msg, &curmsgs, list) {
4316 if (!strcmp(msg->id, ph->id)) {
4317 if (msg->group != NULL) {
4318 memprintf(err, "SPOE message '%s' already belongs to "
4319 "the SPOE group '%s' declare at %s:%d",
4320 msg->id, msg->group->id,
4321 msg->group->conf.file,
4322 msg->group->conf.line);
4323 goto error;
4324 }
4325
4326 /* Scope for arguments are not checked for now. We will check
4327 * them only if a rule use the corresponding SPOE group. */
4328 msg->agent = curagent;
4329 msg->group = grp;
4330 LIST_DEL(&ph->list);
4331 LIST_ADDQ(&grp->messages, &msg->by_grp);
4332 goto next_mph_grp;
4333 }
4334 }
4335 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4336 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4337 goto error;
4338 next_mph_grp:
4339 continue;
4340 }
4341 }
4342
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004343 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004344 /* move curmsgs to the agent message list */
4345 curmsgs.n->p = &curagent->messages;
4346 curmsgs.p->n = &curagent->messages;
4347 curagent->messages = curmsgs;
4348 LIST_INIT(&curmsgs);
4349
Christopher Faulet7ee86672017-09-19 11:08:28 +02004350 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004351 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004352
4353 /* Start agent's proxy initialization here. It will be finished during
4354 * the filter init. */
4355 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4356 init_new_proxy(&conf->agent_fe);
4357 conf->agent_fe.id = conf->agent->id;
4358 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004359 conf->agent_fe.options |= curpxopts;
4360 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004361
4362 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4363 LIST_DEL(&logsrv->list);
4364 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4365 }
4366
Christopher Faulet11610f32017-09-21 10:23:10 +02004367 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4368 LIST_DEL(&ph->list);
4369 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004370 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004371 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4372 LIST_DEL(&ph->list);
4373 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004374 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004375 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4376 struct arg arg;
4377
4378 trash.len = snprintf(trash.str, trash.size, "proc.%s.%s",
4379 curagent->var_pfx, vph->name);
4380
4381 arg.type = ARGT_STR;
4382 arg.data.str.str = trash.str;
4383 arg.data.str.len = trash.len;
4384 if (!vars_check_arg(&arg, err)) {
4385 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4386 curagent->id, curagent->var_pfx, vph->name, *err);
4387 goto error;
4388 }
4389
4390 LIST_DEL(&vph->list);
4391 free(vph->name);
4392 free(vph);
4393 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004394 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4395 LIST_DEL(&grp->list);
4396 spoe_release_group(grp);
4397 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004398 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004399 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004400 fconf->ops = &spoe_ops;
4401 fconf->conf = conf;
4402 return 0;
4403
4404 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004405 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004406 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4407 LIST_DEL(&ph->list);
4408 spoe_release_placeholder(ph);
4409 }
4410 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4411 LIST_DEL(&ph->list);
4412 spoe_release_placeholder(ph);
4413 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004414 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4415 LIST_DEL(&vph->list);
4416 free(vph->name);
4417 free(vph);
4418 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004419 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4420 LIST_DEL(&grp->list);
4421 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004422 }
4423 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4424 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004425 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004426 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004427 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4428 LIST_DEL(&logsrv->list);
4429 free(logsrv);
4430 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004431 free(conf);
4432 return -1;
4433}
4434
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004435/* Send message of a SPOE group. This is the action_ptr callback of a rule
4436 * associated to a "send-spoe-group" action.
4437 *
4438 * It returns ACT_RET_CONT is processing is finished without error, it returns
4439 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4440 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004441static enum act_return
4442spoe_send_group(struct act_rule *rule, struct proxy *px,
4443 struct session *sess, struct stream *s, int flags)
4444{
4445 struct filter *filter;
4446 struct spoe_agent *agent = NULL;
4447 struct spoe_group *group = NULL;
4448 struct spoe_context *ctx = NULL;
4449 int ret, dir;
4450
4451 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4452 if (filter->config == rule->arg.act.p[0]) {
4453 agent = rule->arg.act.p[2];
4454 group = rule->arg.act.p[3];
4455 ctx = filter->ctx;
4456 break;
4457 }
4458 }
4459 if (agent == NULL || group == NULL || ctx == NULL)
4460 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004461 if (ctx->state == SPOE_CTX_ST_NONE)
4462 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004463
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004464 switch (rule->from) {
4465 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4466 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4467 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4468 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4469 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4470 default:
4471 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4472 " - internal error while execute spoe-send-group\n",
4473 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4474 __FUNCTION__, s);
4475 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4476 agent->id);
4477 return ACT_RET_CONT;
4478 }
4479
4480 ret = spoe_process_group(s, ctx, group, dir);
4481 if (ret == 1)
4482 return ACT_RET_CONT;
4483 else if (ret == 0) {
4484 if (flags & ACT_FLAG_FINAL) {
4485 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4486 " - failed to process group '%s': interrupted by caller\n",
4487 (int)now.tv_sec, (int)now.tv_usec,
4488 agent->id, __FUNCTION__, s, group->id);
4489 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004490 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004491 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004492 return ACT_RET_CONT;
4493 }
4494 return ACT_RET_YIELD;
4495 }
4496 else
4497 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004498}
4499
4500/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4501 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4502 * action should be:
4503 *
4504 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4505 *
4506 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4507 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4508 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4509 * group.
4510 *
4511 * The function returns 1 in success case, otherwise, it returns 0 and err is
4512 * filled.
4513 */
4514static int
4515check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4516{
4517 struct flt_conf *fconf;
4518 struct spoe_config *conf;
4519 struct spoe_agent *agent = NULL;
4520 struct spoe_group *group;
4521 struct spoe_message *msg;
4522 char *engine_id = rule->arg.act.p[0];
4523 char *group_id = rule->arg.act.p[1];
4524 unsigned int where = 0;
4525
4526 switch (rule->from) {
4527 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4528 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4529 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4530 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4531 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4532 default:
4533 memprintf(err,
4534 "internal error, unexpected rule->from=%d, please report this bug!",
4535 rule->from);
4536 goto error;
4537 }
4538
4539 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4540 * <px> */
4541 list_for_each_entry(fconf, &px->filter_configs, list) {
4542 conf = fconf->conf;
4543
4544 /* This is not an SPOE filter */
4545 if (fconf->id != spoe_filter_id)
4546 continue;
4547
4548 /* This is the good engine */
4549 if (!strcmp(conf->id, engine_id)) {
4550 agent = conf->agent;
4551 break;
4552 }
4553 }
4554 if (agent == NULL) {
4555 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4556 engine_id, group_id);
4557 goto error;
4558 }
4559
4560 /* Try to find the right group */
4561 list_for_each_entry(group, &agent->groups, list) {
4562 /* This is the good group */
4563 if (!strcmp(group->id, group_id))
4564 break;
4565 }
4566 if (&group->list == &agent->groups) {
4567 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4568 group_id, engine_id);
4569 goto error;
4570 }
4571
4572 /* Ok, we found the group, we need to check messages and their
4573 * arguments */
4574 list_for_each_entry(msg, &group->messages, by_grp) {
4575 struct spoe_arg *arg;
4576
4577 list_for_each_entry(arg, &msg->args, list) {
4578 if (!(arg->expr->fetch->val & where)) {
4579 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4580 "some args extract information from '%s',"
4581 "none of which is available here ('%s')",
4582 msg->id, group->id, msg->conf.file, msg->conf.line,
4583 sample_ckp_names(arg->expr->fetch->use),
4584 sample_ckp_names(where));
4585 goto error;
4586 }
4587 }
4588 }
4589
4590 free(engine_id);
4591 free(group_id);
4592 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4593 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4594 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4595 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4596 return 1;
4597
4598 error:
4599 free(engine_id);
4600 free(group_id);
4601 return 0;
4602}
4603
4604/* Parse 'send-spoe-group' action following the format:
4605 *
4606 * ... send-spoe-group <engine-id> <group-id>
4607 *
4608 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4609 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4610 * ids are saved and used later, when the rule will be checked.
4611 */
4612static enum act_parse_ret
4613parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4614 struct act_rule *rule, char **err)
4615{
4616 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4617 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4618 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4619 return ACT_RET_PRS_ERR;
4620 }
4621 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4622 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4623
4624 (*orig_arg) += 2;
4625
4626 rule->action = ACT_CUSTOM;
4627 rule->action_ptr = spoe_send_group;
4628 rule->check_ptr = check_send_spoe_group;
4629 return ACT_RET_PRS_OK;
4630}
4631
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004632
4633/* Declare the filter parser for "spoe" keyword */
4634static struct flt_kw_list flt_kws = { "SPOE", { }, {
4635 { "spoe", parse_spoe_flt, NULL },
4636 { NULL, NULL, NULL },
4637 }
4638};
4639
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004640/* Delcate the action parser for "spoe-action" keyword */
4641static struct action_kw_list tcp_req_action_kws = { { }, {
4642 { "send-spoe-group", parse_send_spoe_group },
4643 { /* END */ },
4644 }
4645};
4646static struct action_kw_list tcp_res_action_kws = { { }, {
4647 { "send-spoe-group", parse_send_spoe_group },
4648 { /* END */ },
4649 }
4650};
4651static struct action_kw_list http_req_action_kws = { { }, {
4652 { "send-spoe-group", parse_send_spoe_group },
4653 { /* END */ },
4654 }
4655};
4656static struct action_kw_list http_res_action_kws = { { }, {
4657 { "send-spoe-group", parse_send_spoe_group },
4658 { /* END */ },
4659 }
4660};
4661
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004662__attribute__((constructor))
4663static void __spoe_init(void)
4664{
4665 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004666 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4667 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4668 http_req_keywords_register(&http_req_action_kws);
4669 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004670
Willy Tarreaubafbe012017-11-24 17:34:44 +01004671 pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
4672 pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004673}
4674
4675__attribute__((destructor))
4676static void
4677__spoe_deinit(void)
4678{
Willy Tarreaubafbe012017-11-24 17:34:44 +01004679 pool_destroy(pool_head_spoe_ctx);
4680 pool_destroy(pool_head_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004681}