blob: 7a19a18a7ae7fdc9f25125339848008534b0f081 [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 Faulet3a47e5e2018-05-25 10:42:37 +02001033 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1034 return -1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001035 return 0;
1036
1037 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001038 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1039 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001040 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001041 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001042 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001043
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001044 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001045 len = (end - p);
1046 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1047 SPOE_APPCTX(appctx)->buffer->i = len;
1048 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001049
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001050 /* Transfer the buffer ownership to the SPOE context */
1051 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1052 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001053
Christopher Faulet8ef75252017-02-20 22:56:03 +01001054 (*ctx)->state = SPOE_CTX_ST_DONE;
1055
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001056 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001057 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001058 " - ACK frame received"
1059 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001060 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001061 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1062 (*ctx)->frame_id, flags);
1063 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001064}
1065
Christopher Fauletba7bc162016-11-07 21:07:38 +01001066/* This function is used in cfgparse.c and declared in proto/checks.h. It
1067 * prepare the request to send to agents during a healthcheck. It returns 0 on
1068 * success and -1 if an error occurred. */
1069int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001070spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001071{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001072 struct appctx appctx;
1073 struct spoe_appctx spoe_appctx;
1074 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1075 size_t sz;
1076 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001077
Christopher Faulet42bfa462017-01-04 14:14:19 +01001078 memset(&appctx, 0, sizeof(appctx));
1079 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001080 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001081
1082 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001083 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001084
Christopher Faulet8ef75252017-02-20 22:56:03 +01001085 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1086 end = frame + MAX_FRAME_SIZE;
1087
1088 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1089 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001090 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001091 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001092
Christopher Faulet8ef75252017-02-20 22:56:03 +01001093 /* Add "healthcheck" K/V item */
1094 sz = SLEN(HEALTHCHECK_KEY);
1095 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1096 return -1;
1097 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001098
Christopher Faulet8ef75252017-02-20 22:56:03 +01001099 *len = frame - buf;
1100 sz = htonl(*len - 4);
1101 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001102
Christopher Faulet8ef75252017-02-20 22:56:03 +01001103 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001104 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001105 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001106 return 0;
1107}
1108
1109/* This function is used in checks.c and declared in proto/checks.h. It decode
1110 * the response received from an agent during a healthcheck. It returns 0 on
1111 * success and -1 if an error occurred. */
1112int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001113spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001114{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001115 struct appctx appctx;
1116 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001117
Christopher Faulet42bfa462017-01-04 14:14:19 +01001118 memset(&appctx, 0, sizeof(appctx));
1119 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001120
Christopher Faulet42bfa462017-01-04 14:14:19 +01001121 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001122 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001123
Christopher Faulet8ef75252017-02-20 22:56:03 +01001124 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1125 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001126 goto error;
1127 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001128 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1129 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001130
1131 return 0;
1132
1133 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001134 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1135 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1136 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001137 return -1;
1138}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001139
Christopher Fauleta1cda022016-12-21 08:58:06 +01001140/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1141 * the frame can be ignored, 1 to retry later, and the frame legnth on
1142 * success. */
1143static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001144spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001145{
1146 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001147 int ret;
1148 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001149
Christopher Faulet8ef75252017-02-20 22:56:03 +01001150 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1151 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001152 netint = htonl(framesz);
1153 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001154 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001155 if (ret <= 0) {
Christopher Fauletd5216d42018-02-01 08:45:22 +01001156 if ((ret == -3 && si_ic(si)->buf == &buf_empty) || ret == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001157 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001158 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001159 }
1160 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001161 return -1; /* error */
1162 }
1163 return framesz;
1164}
1165
1166/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1167 * when the frame can be ignored, 1 to retry later and the frame length on
1168 * success. */
1169static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001170spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001171{
1172 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001173 int ret;
1174 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001175
Willy Tarreau06d80a92017-10-19 14:32:15 +02001176 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001177 if (ret > 0) {
1178 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001179 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001180 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001181 return -1;
1182 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001183 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001184 }
1185 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001186 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001187 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001188 }
1189 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001190 return -1; /* error */
1191 }
1192 return framesz;
1193}
1194
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001195/********************************************************************
1196 * Functions that manage the SPOE applet
1197 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001198static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001199spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001200{
1201 si_applet_want_get(appctx->owner);
1202 si_applet_want_put(appctx->owner);
1203 appctx_wakeup(appctx);
1204 return 1;
1205}
1206
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001207/* Callback function that catches applet timeouts. If a timeout occurred, we set
1208 * <appctx->st1> flag and the SPOE applet is woken up. */
1209static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001210spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001211{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001212 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001213
1214 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1215 if (tick_is_expired(task->expire, now_ms)) {
1216 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001217 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001218 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001219 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001220 return task;
1221}
1222
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001223/* Callback function that releases a SPOE applet. This happens when the
1224 * connection with the agent is closed. */
1225static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001226spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001227{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001228 struct stream_interface *si = appctx->owner;
1229 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1230 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001231 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001232
1233 if (spoe_appctx == NULL)
1234 return;
1235
1236 appctx->ctx.spoe.ptr = NULL;
1237 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001238
1239 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1240 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1241 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001242
Christopher Faulet8ef75252017-02-20 22:56:03 +01001243 /* Remove applet from the list of running applets */
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001244 HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001245 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001246 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1247 LIST_DEL(&spoe_appctx->list);
1248 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001249 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001250 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001251
Christopher Faulet8ef75252017-02-20 22:56:03 +01001252 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001253 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001254 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1255 eb32_delete(&spoe_appctx->node);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001256 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001257 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001258
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001259 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001260 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1261 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001262
1263 si_shutw(si);
1264 si_shutr(si);
1265 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001266 }
1267
Christopher Faulet8ef75252017-02-20 22:56:03 +01001268 /* Destroy the task attached to this applet */
1269 if (spoe_appctx->task) {
1270 task_delete(spoe_appctx->task);
1271 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001272 }
1273
Christopher Faulet8ef75252017-02-20 22:56:03 +01001274 /* Notify all waiting streams */
1275 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001276 LIST_DEL(&ctx->list);
1277 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001278 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001279 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001280 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001281 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001282 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001283 }
1284
Christopher Faulet8ef75252017-02-20 22:56:03 +01001285 /* If the applet was processing a fragmented frame, notify the
1286 * corresponding stream. */
1287 if (spoe_appctx->frag_ctx.ctx) {
1288 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001289 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001290 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001291 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001292 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1293 }
1294
Christopher Faulet8ef75252017-02-20 22:56:03 +01001295 /* Release allocated memory */
1296 spoe_release_buffer(&spoe_appctx->buffer,
1297 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001298 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001299
Christopher Faulet24289f22017-09-25 14:48:02 +02001300 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001301 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001302
Christopher Faulet8ef75252017-02-20 22:56:03 +01001303 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001304 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001305 LIST_DEL(&ctx->list);
1306 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001307 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001308 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001309 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001310 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001311 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001312 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001313 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001314 LIST_DEL(&ctx->list);
1315 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001316 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001317 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001318 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001319 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001320 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1321 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001322
1323 end:
1324 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001325 agent->rt[tid].frame_size = agent->max_frame_size;
1326 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1327 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001328}
1329
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001330static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001331spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001332{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001333 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001334 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001335 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001336 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001337
Christopher Fauleta1cda022016-12-21 08:58:06 +01001338 if (si->state <= SI_ST_CON) {
1339 si_applet_want_put(si);
1340 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1341 goto stop;
1342 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001343 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001344 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001345 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001346 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001347
Christopher Fauleta1cda022016-12-21 08:58:06 +01001348 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001349 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1350 " - Connection timed out\n",
1351 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1352 __FUNCTION__, appctx);
1353 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001354 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001355 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001356
Christopher Faulet42bfa462017-01-04 14:14:19 +01001357 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001358 SPOE_APPCTX(appctx)->task->expire =
1359 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001360
Christopher Faulet8ef75252017-02-20 22:56:03 +01001361 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1362 * length. */
1363 buf = trash.str; frame = buf+4;
1364 ret = spoe_prepare_hahello_frame(appctx, frame,
1365 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001366 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001367 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001368
1369 switch (ret) {
1370 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001371 case 0: /* ignore => an error, cannot be ignored */
1372 goto exit;
1373
1374 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001375 goto stop;
1376
Christopher Faulet8ef75252017-02-20 22:56:03 +01001377 default:
1378 /* HELLO frame successfully sent, now wait for the
1379 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001380 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1381 goto next;
1382 }
1383
1384 next:
1385 return 0;
1386 stop:
1387 return 1;
1388 exit:
1389 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1390 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001391}
1392
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001393static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001394spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001395{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001396 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001397 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001398 char *frame;
1399 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001400
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001401
Christopher Fauletb067b062017-01-04 16:39:11 +01001402 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001403 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001404 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001405 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001406
Christopher Fauleta1cda022016-12-21 08:58:06 +01001407 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001408 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1409 " - Connection timed out\n",
1410 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1411 __FUNCTION__, appctx);
1412 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001413 goto exit;
1414 }
1415
Christopher Faulet8ef75252017-02-20 22:56:03 +01001416 frame = trash.str; trash.len = 0;
1417 ret = spoe_recv_frame(appctx, frame,
1418 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001419 if (ret > 1) {
1420 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1421 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1422 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001423 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001424 trash.len = ret + 4;
1425 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001426 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001427
Christopher Fauleta1cda022016-12-21 08:58:06 +01001428 switch (ret) {
1429 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001430 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001431 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1432 goto next;
1433
1434 case 1: /* retry later */
1435 goto stop;
1436
1437 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001438 /* HELLO handshake is finished, set the idle timeout and
1439 * add the applet in the list of running applets. */
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001440 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001441 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001442 SPOE_APPCTX(appctx)->node.key = 0;
1443 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001444
1445 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001446 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001447 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001448 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001449
Christopher Fauleta1cda022016-12-21 08:58:06 +01001450 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001451 /* Do not forget to remove processed frame from the output buffer */
1452 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001453 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001454
1455 SPOE_APPCTX(appctx)->task->expire =
1456 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001457 return 0;
1458 stop:
1459 return 1;
1460 exit:
1461 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1462 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001463}
1464
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001465
Christopher Fauleta1cda022016-12-21 08:58:06 +01001466static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001467spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001468{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001469 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1470 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001471 char *frame, *buf;
1472 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001473
Christopher Faulet8ef75252017-02-20 22:56:03 +01001474 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1475 * length. */
1476 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001477
1478 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1479 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1480 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1481 SPOE_APPCTX(appctx)->max_frame_size);
1482 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001483 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001484 *skip = 1;
1485 ret = 1;
1486 goto end;
1487 }
1488 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001489 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001490 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1491 SPOE_APPCTX(appctx)->max_frame_size);
1492
1493 }
1494
Christopher Faulet8ef75252017-02-20 22:56:03 +01001495 if (ret > 1)
1496 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001497
Christopher Faulet8ef75252017-02-20 22:56:03 +01001498 switch (ret) {
1499 case -1: /* error */
1500 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1501 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001502
Christopher Faulet8ef75252017-02-20 22:56:03 +01001503 case 0: /* ignore */
1504 if (ctx == NULL)
1505 goto abort_frag_frame;
1506
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001507 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001508 LIST_DEL(&ctx->list);
1509 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001510 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001511 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001512 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001513 ctx->state = SPOE_CTX_ST_ERROR;
1514 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1515 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001516 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001517 break;
1518
1519 case 1: /* retry */
1520 *skip = 1;
1521 break;
1522
1523 default:
1524 if (ctx == NULL)
1525 goto abort_frag_frame;
1526
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001527 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001528 LIST_DEL(&ctx->list);
1529 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001530 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001531 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001532 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001533 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1534 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1535 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001536 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001537 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001538 }
1539 goto end;
1540
1541 frag_frame_sent:
1542 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001543 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001544 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1545 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1546 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001547 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1548 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1549 goto end;
1550
1551 no_frag_frame_sent:
1552 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1553 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001554 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001555 }
1556 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1557 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1558 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1559 }
1560 else {
1561 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001562 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001563 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1564 }
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001565 HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001566 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001567 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1568 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1569 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001570 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001571
Christopher Faulet8ef75252017-02-20 22:56:03 +01001572 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1573 goto end;
1574
1575 abort_frag_frame:
1576 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1577 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1578 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1579 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1580 goto end;
1581
1582 end:
1583 return ret;
1584}
1585
1586static int
1587spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1588{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001589 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001590 struct spoe_context *ctx = NULL;
1591 char *frame;
1592 int ret;
1593
1594 frame = trash.str; trash.len = 0;
1595 ret = spoe_recv_frame(appctx, frame,
1596 SPOE_APPCTX(appctx)->max_frame_size);
1597 if (ret > 1) {
1598 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1599 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001600 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001601 goto end;
1602 }
1603 trash.len = ret + 4;
1604 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1605 }
1606 switch (ret) {
1607 case -1: /* error */
1608 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1609 break;
1610
1611 case 0: /* ignore */
1612 break;
1613
1614 case 1: /* retry */
1615 *skip = 1;
1616 break;
1617
1618 default:
1619 LIST_DEL(&ctx->list);
1620 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001621 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001622 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1623 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001624 if (ctx->spoe_appctx) {
1625 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001626 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001627 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001628 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1629 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1630 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1631 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1632 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1633 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1634 }
1635 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1636 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001637 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1638 break;
1639 }
1640
1641 /* Do not forget to remove processed frame from the output buffer */
1642 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001643 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001644 end:
1645 return ret;
1646}
1647
1648static int
1649spoe_handle_processing_appctx(struct appctx *appctx)
1650{
1651 struct stream_interface *si = appctx->owner;
1652 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001653 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001654
1655 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1656 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1657 goto exit;
1658 }
1659
1660 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1661 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1662 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1663 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1664 goto next;
1665 }
1666
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001667 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001668 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001669 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001670 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1671 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001672 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001673
Christopher Faulet8f82b202018-01-24 16:23:03 +01001674 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1675 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001676
Christopher Faulet8f82b202018-01-24 16:23:03 +01001677 /* receiving_frame loop */
1678 while (!skip_receiving) {
1679 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1680 switch (ret) {
1681 case -1: /* error */
1682 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001683
Christopher Faulet8f82b202018-01-24 16:23:03 +01001684 case 0: /* ignore */
1685 active_r = 1;
1686 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001687
Christopher Faulet8f82b202018-01-24 16:23:03 +01001688 case 1: /* retry */
1689 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001690
Christopher Faulet8f82b202018-01-24 16:23:03 +01001691 default:
1692 active_r = 1;
1693 break;
1694 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001695 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001696
Christopher Faulet8f82b202018-01-24 16:23:03 +01001697 /* send_frame loop */
1698 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1699 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1700 switch (ret) {
1701 case -1: /* error */
1702 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001703
Christopher Faulet8f82b202018-01-24 16:23:03 +01001704 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001705 if (SPOE_APPCTX(appctx)->node.key)
1706 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001707 active_s++;
1708 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001709
Christopher Faulet8f82b202018-01-24 16:23:03 +01001710 case 1: /* retry */
1711 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001712
Christopher Faulet8f82b202018-01-24 16:23:03 +01001713 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001714 if (SPOE_APPCTX(appctx)->node.key)
1715 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001716 active_s++;
1717 break;
1718 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001719 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001720
Christopher Faulet8f82b202018-01-24 16:23:03 +01001721 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001722 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1723 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1724 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001725
Christopher Faulet8f82b202018-01-24 16:23:03 +01001726 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001727 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001728 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001729 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001730 }
1731 return 1;
1732
Christopher Faulet8f82b202018-01-24 16:23:03 +01001733 next:
1734 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1735 return 0;
1736
Christopher Fauleta1cda022016-12-21 08:58:06 +01001737 exit:
1738 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1739 return 0;
1740}
1741
1742static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001743spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001744{
1745 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001746 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001747 char *frame, *buf;
1748 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001749
Christopher Fauleta1cda022016-12-21 08:58:06 +01001750 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1751 goto exit;
1752
1753 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1754 goto exit;
1755
Christopher Faulet8ef75252017-02-20 22:56:03 +01001756 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1757 * length. */
1758 buf = trash.str; frame = buf+4;
1759 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1760 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001761 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001762 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001763
1764 switch (ret) {
1765 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001766 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001767 goto exit;
1768
1769 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001770 goto stop;
1771
1772 default:
1773 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1774 " - disconnected by HAProxy (%d): %s\n",
1775 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001776 __FUNCTION__, appctx,
1777 SPOE_APPCTX(appctx)->status_code,
1778 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001779
1780 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1781 goto next;
1782 }
1783
1784 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001785 SPOE_APPCTX(appctx)->task->expire =
1786 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001787 return 0;
1788 stop:
1789 return 1;
1790 exit:
1791 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1792 return 0;
1793}
1794
1795static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001796spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001797{
1798 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001799 char *frame;
1800 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001801
Christopher Fauletb067b062017-01-04 16:39:11 +01001802 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001803 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001804 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001805 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001806
Christopher Fauletb067b062017-01-04 16:39:11 +01001807 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001808 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001809 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001810 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001811
Christopher Faulet8ef75252017-02-20 22:56:03 +01001812 frame = trash.str; trash.len = 0;
1813 ret = spoe_recv_frame(appctx, frame,
1814 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001815 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001816 trash.len = ret + 4;
1817 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001818 }
1819
1820 switch (ret) {
1821 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001822 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1823 " - error on frame (%s)\n",
1824 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001825 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001826 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001827 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001828 goto exit;
1829
1830 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001831 goto next;
1832
1833 case 1: /* retry */
1834 goto stop;
1835
1836 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001837 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001838 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001839 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001840 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001841 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1842 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001843 goto exit;
1844 }
1845
1846 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001847 /* Do not forget to remove processed frame from the output buffer */
1848 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001849 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001850
Christopher Fauleta1cda022016-12-21 08:58:06 +01001851 return 0;
1852 stop:
1853 return 1;
1854 exit:
1855 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1856 return 0;
1857}
1858
1859/* I/O Handler processing messages exchanged with the agent */
1860static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001861spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001862{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001863 struct stream_interface *si = appctx->owner;
1864 struct spoe_agent *agent;
1865
1866 if (SPOE_APPCTX(appctx) == NULL)
1867 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001868
Christopher Faulet8ef75252017-02-20 22:56:03 +01001869 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1870 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001871
Christopher Fauleta1cda022016-12-21 08:58:06 +01001872 switchstate:
1873 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1874 " - appctx-state=%s\n",
1875 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1876 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1877
1878 switch (appctx->st0) {
1879 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001880 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001881 goto out;
1882 goto switchstate;
1883
1884 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001885 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001886 goto out;
1887 goto switchstate;
1888
1889 case SPOE_APPCTX_ST_IDLE:
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001890 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001891 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001892 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001893 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001894 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001895 SPOE_APPCTX(appctx)->task->expire =
1896 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001897 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001898 goto switchstate;
1899 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001900 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1901 /* fall through */
1902
1903 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001904 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1905 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001906 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001907 goto out;
1908 goto switchstate;
1909
1910 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001911 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001912 goto out;
1913 goto switchstate;
1914
1915 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001916 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001917 goto out;
1918 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001919
1920 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001921 appctx->st0 = SPOE_APPCTX_ST_END;
1922 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1923
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001924 si_shutw(si);
1925 si_shutr(si);
1926 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001927 /* fall through */
1928
1929 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001930 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001931 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001932 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001933 if (stopping)
1934 spoe_wakeup_appctx(appctx);
1935
Christopher Faulet42bfa462017-01-04 14:14:19 +01001936 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1937 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001938 si_oc(si)->flags |= CF_READ_DONTWAIT;
1939 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001940}
1941
1942struct applet spoe_applet = {
1943 .obj_type = OBJ_TYPE_APPLET,
1944 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001945 .fct = spoe_handle_appctx,
1946 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001947};
1948
1949/* Create a SPOE applet. On success, the created applet is returned, else
1950 * NULL. */
1951static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001952spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001953{
1954 struct appctx *appctx;
1955 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001956 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001957
Emeric Brun1138fd02017-06-19 12:38:55 +02001958 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001959 goto out_error;
1960
Willy Tarreaubafbe012017-11-24 17:34:44 +01001961 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001962 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001963 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001964 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001965
Christopher Faulet42bfa462017-01-04 14:14:19 +01001966 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001967 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001968 goto out_free_spoe_appctx;
1969
1970 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001971 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001972 SPOE_APPCTX(appctx)->task->context = appctx;
1973 SPOE_APPCTX(appctx)->agent = conf->agent;
1974 SPOE_APPCTX(appctx)->version = 0;
1975 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1976 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001977 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001978 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001979 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001980
1981 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1982 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001983 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001984
1985 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1986 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001987
Willy Tarreau5820a362016-12-22 15:59:02 +01001988 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001989 if (!sess)
1990 goto out_free_spoe;
1991
Willy Tarreau87787ac2017-08-28 16:22:54 +02001992 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001993 goto out_free_sess;
1994
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001995 stream_set_backend(strm, conf->agent->b.be);
1996
1997 /* applet is waiting for data */
1998 si_applet_cant_get(&strm->si[0]);
1999 appctx_wakeup(appctx);
2000
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002001 strm->do_log = NULL;
2002 strm->res.flags |= CF_READ_DONTWAIT;
2003
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002004 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002005 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002006 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002007 HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002008
Emeric Brunc60def82017-09-27 14:59:38 +02002009 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002010 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002011 return appctx;
2012
2013 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002014 out_free_sess:
2015 session_free(sess);
2016 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01002017 task_free(SPOE_APPCTX(appctx)->task);
2018 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002019 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002020 out_free_appctx:
2021 appctx_free(appctx);
2022 out_error:
2023 return NULL;
2024}
2025
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002026static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002027spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002028{
2029 struct spoe_config *conf = FLT_CONF(ctx->filter);
2030 struct spoe_agent *agent = conf->agent;
2031 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002032 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002033
Christopher Fauleta1cda022016-12-21 08:58:06 +01002034 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002035 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002036 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002037 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002038
2039 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002040 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002041 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2042 ctx->strm);
2043
Christopher Fauleta1cda022016-12-21 08:58:06 +01002044 /* Do not try to create a new applet if there is no server up for the
2045 * agent's backend. */
2046 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2047 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2048 " - cannot create SPOE appctx: no server up\n",
2049 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2050 __FUNCTION__, ctx->strm);
2051 goto end;
2052 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002053
Christopher Fauleta1cda022016-12-21 08:58:06 +01002054 /* Do not try to create a new applet if we have reached the maximum of
2055 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002056 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002057 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002058 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2059 " - cannot create SPOE appctx: max CPS reached\n",
2060 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2061 __FUNCTION__, ctx->strm);
2062 goto end;
2063 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002064 }
2065
Christopher Faulet8ef75252017-02-20 22:56:03 +01002066 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002067 if (appctx == NULL) {
2068 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2069 " - failed to create SPOE appctx\n",
2070 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2071 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002072 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002073 "SPOE: [%s] failed to create SPOE applet\n",
2074 agent->id);
2075
Christopher Fauleta1cda022016-12-21 08:58:06 +01002076 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002077 }
2078
Christopher Fauleta1cda022016-12-21 08:58:06 +01002079 /* Increase the per-process number of cumulated connections */
2080 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002081 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002082
Christopher Fauleta1cda022016-12-21 08:58:06 +01002083 end:
2084 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002085 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002086 ctx->status_code = SPOE_CTX_ERR_RES;
2087 return -1;
2088 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002089
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002090 /* Add the SPOE context in the sending queue */
Christopher Faulet24289f22017-09-25 14:48:02 +02002091 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002092 HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002093 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2094 ctx->stats.tv_queue = now;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002095
2096 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002097 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002098 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002099 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002100 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002101 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002102
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002103 /* Finally try to wakeup an IDLE applet. */
2104 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2105 struct eb32_node *node;
2106
2107 node = eb32_first(&agent->rt[tid].idle_applets);
2108 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2109 if (node && spoe_appctx) {
2110 eb32_delete(&spoe_appctx->node);
2111 spoe_appctx->node.key++;
2112 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2113 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002114 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002115 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002116 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002117}
2118
2119/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002120 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002121 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002122/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2123 * fragmented_content. If the next message can be processed, it returns 0. If
2124 * the message is too big, it returns -1.*/
2125static int
2126spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2127 struct spoe_message *msg, int dir,
2128 char **buf, char *end)
2129{
2130 struct sample *smp;
2131 struct spoe_arg *arg;
2132 int ret;
2133
2134 if (msg->cond) {
2135 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2136 ret = acl_pass(ret);
2137 if (msg->cond->pol == ACL_COND_UNLESS)
2138 ret = !ret;
2139
2140 /* the rule does not match */
2141 if (!ret)
2142 goto next;
2143 }
2144
2145 /* Resume encoding of a SPOE argument */
2146 if (ctx->frag_ctx.curarg != NULL) {
2147 arg = ctx->frag_ctx.curarg;
2148 goto encode_argument;
2149 }
2150
2151 if (ctx->frag_ctx.curoff != UINT_MAX)
2152 goto encode_msg_payload;
2153
2154 /* Check if there is enough space for the message name and the
2155 * number of arguments. It implies <msg->id_len> is encoded on 2
2156 * bytes, at most (< 2288). */
2157 if (*buf + 2 + msg->id_len + 1 > end)
2158 goto too_big;
2159
2160 /* Encode the message name */
2161 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2162 goto too_big;
2163
2164 /* Set the number of arguments for this message */
2165 **buf = msg->nargs;
2166 (*buf)++;
2167
2168 ctx->frag_ctx.curoff = 0;
2169 encode_msg_payload:
2170
2171 /* Loop on arguments */
2172 list_for_each_entry(arg, &msg->args, list) {
2173 ctx->frag_ctx.curarg = arg;
2174 ctx->frag_ctx.curoff = UINT_MAX;
2175
2176 encode_argument:
2177 if (ctx->frag_ctx.curoff != UINT_MAX)
2178 goto encode_arg_value;
2179
2180 /* Encode the arguement name as a string. It can by NULL */
2181 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2182 goto too_big;
2183
2184 ctx->frag_ctx.curoff = 0;
2185 encode_arg_value:
2186
2187 /* Fetch the arguement value */
2188 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2189 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2190 if (ret == -1 || ctx->frag_ctx.curoff)
2191 goto too_big;
2192 }
2193
2194 next:
2195 return 0;
2196
2197 too_big:
2198 return -1;
2199}
2200
Christopher Fauletc718b822017-09-21 16:50:56 +02002201/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2202 * handle fragmented content. On success it returns 1. If an error occurred, -1
2203 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2204 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002205static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002206spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002207 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002208{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002209 struct spoe_config *conf = FLT_CONF(ctx->filter);
2210 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002211 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002212 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002213
Christopher Faulet8ef75252017-02-20 22:56:03 +01002214 p = ctx->buffer->p;
Christopher Faulet24289f22017-09-25 14:48:02 +02002215 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002216
Christopher Fauletc718b822017-09-21 16:50:56 +02002217 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2218 /* Resume encoding of a SPOE message */
2219 if (ctx->frag_ctx.curmsg != NULL) {
2220 msg = ctx->frag_ctx.curmsg;
2221 goto encode_evt_message;
2222 }
2223
2224 list_for_each_entry(msg, messages, by_evt) {
2225 ctx->frag_ctx.curmsg = msg;
2226 ctx->frag_ctx.curarg = NULL;
2227 ctx->frag_ctx.curoff = UINT_MAX;
2228
2229 encode_evt_message:
2230 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2231 goto too_big;
2232 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002233 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002234 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2235 /* Resume encoding of a SPOE message */
2236 if (ctx->frag_ctx.curmsg != NULL) {
2237 msg = ctx->frag_ctx.curmsg;
2238 goto encode_grp_message;
2239 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002240
Christopher Fauletc718b822017-09-21 16:50:56 +02002241 list_for_each_entry(msg, messages, by_grp) {
2242 ctx->frag_ctx.curmsg = msg;
2243 ctx->frag_ctx.curarg = NULL;
2244 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002245
Christopher Fauletc718b822017-09-21 16:50:56 +02002246 encode_grp_message:
2247 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2248 goto too_big;
2249 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002250 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002251 else
2252 goto skip;
2253
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002254
Christopher Faulet57583e42017-09-04 15:41:09 +02002255 /* nothing has been encoded for an unfragmented payload */
2256 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2257 goto skip;
2258
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002259 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002260 " - encode %s messages - spoe_appctx=%p"
2261 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002262 (int)now.tv_sec, (int)now.tv_usec,
2263 agent->id, __FUNCTION__, s,
2264 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002265 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002266 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002267
Christopher Faulet8ef75252017-02-20 22:56:03 +01002268 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002269 ctx->frag_ctx.curmsg = NULL;
2270 ctx->frag_ctx.curarg = NULL;
2271 ctx->frag_ctx.curoff = 0;
2272 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002273
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002274 return 1;
2275
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002276 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002277 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2278 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2279 return -1;
2280 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002281
2282 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002283 " - encode fragmented messages - spoe_appctx=%p"
2284 " - curmsg=%p - curarg=%p - curoff=%u"
2285 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002286 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002287 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002288 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet24289f22017-09-25 14:48:02 +02002289 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002290
Christopher Faulet8ef75252017-02-20 22:56:03 +01002291 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002292 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2293 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2294 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002295
2296 skip:
2297 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2298 " - skip the frame because nothing has been encoded\n",
2299 (int)now.tv_sec, (int)now.tv_usec,
2300 agent->id, __FUNCTION__, s);
2301 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002302}
2303
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002304
2305/***************************************************************************
2306 * Functions that handle SPOE actions
2307 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002308/* Helper function to set a variable */
2309static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002310spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002311 struct sample *smp)
2312{
2313 struct spoe_config *conf = FLT_CONF(ctx->filter);
2314 struct spoe_agent *agent = conf->agent;
2315 char varname[64];
2316
2317 memset(varname, 0, sizeof(varname));
2318 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2319 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002320 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2321 vars_set_by_name(varname, len, smp);
2322 else
2323 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002324}
2325
2326/* Helper function to unset a variable */
2327static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002328spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002329 struct sample *smp)
2330{
2331 struct spoe_config *conf = FLT_CONF(ctx->filter);
2332 struct spoe_agent *agent = conf->agent;
2333 char varname[64];
2334
2335 memset(varname, 0, sizeof(varname));
2336 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2337 scope, agent->var_pfx, len, name);
2338 vars_unset_by_name_ifexist(varname, len, smp);
2339}
2340
2341
Christopher Faulet8ef75252017-02-20 22:56:03 +01002342static inline int
2343spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2344 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002345{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002346 char *str, *scope, *p = *buf;
2347 struct sample smp;
2348 uint64_t sz;
2349 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002350
Christopher Faulet8ef75252017-02-20 22:56:03 +01002351 if (p + 2 >= end)
2352 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002353
Christopher Faulet8ef75252017-02-20 22:56:03 +01002354 /* SET-VAR requires 3 arguments */
2355 if (*p++ != 3)
2356 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002357
Christopher Faulet8ef75252017-02-20 22:56:03 +01002358 switch (*p++) {
2359 case SPOE_SCOPE_PROC: scope = "proc"; break;
2360 case SPOE_SCOPE_SESS: scope = "sess"; break;
2361 case SPOE_SCOPE_TXN : scope = "txn"; break;
2362 case SPOE_SCOPE_REQ : scope = "req"; break;
2363 case SPOE_SCOPE_RES : scope = "res"; break;
2364 default: goto skip;
2365 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002366
Christopher Faulet8ef75252017-02-20 22:56:03 +01002367 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2368 goto skip;
2369 memset(&smp, 0, sizeof(smp));
2370 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002371
Christopher Faulet8ef75252017-02-20 22:56:03 +01002372 if (spoe_decode_data(&p, end, &smp) == -1)
2373 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002374
Christopher Faulet8ef75252017-02-20 22:56:03 +01002375 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2376 " - set-var '%s.%s.%.*s'\n",
2377 (int)now.tv_sec, (int)now.tv_usec,
2378 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2379 __FUNCTION__, s, scope,
2380 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2381 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002382
Christopher Faulet8ef75252017-02-20 22:56:03 +01002383 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002384
Christopher Faulet8ef75252017-02-20 22:56:03 +01002385 ret = (p - *buf);
2386 *buf = p;
2387 return ret;
2388 skip:
2389 return 0;
2390}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002391
Christopher Faulet8ef75252017-02-20 22:56:03 +01002392static inline int
2393spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2394 char **buf, char *end, int dir)
2395{
2396 char *str, *scope, *p = *buf;
2397 struct sample smp;
2398 uint64_t sz;
2399 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002400
Christopher Faulet8ef75252017-02-20 22:56:03 +01002401 if (p + 2 >= end)
2402 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002403
Christopher Faulet8ef75252017-02-20 22:56:03 +01002404 /* UNSET-VAR requires 2 arguments */
2405 if (*p++ != 2)
2406 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002407
Christopher Faulet8ef75252017-02-20 22:56:03 +01002408 switch (*p++) {
2409 case SPOE_SCOPE_PROC: scope = "proc"; break;
2410 case SPOE_SCOPE_SESS: scope = "sess"; break;
2411 case SPOE_SCOPE_TXN : scope = "txn"; break;
2412 case SPOE_SCOPE_REQ : scope = "req"; break;
2413 case SPOE_SCOPE_RES : scope = "res"; break;
2414 default: goto skip;
2415 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002416
Christopher Faulet8ef75252017-02-20 22:56:03 +01002417 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2418 goto skip;
2419 memset(&smp, 0, sizeof(smp));
2420 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002421
Christopher Faulet8ef75252017-02-20 22:56:03 +01002422 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2423 " - unset-var '%s.%s.%.*s'\n",
2424 (int)now.tv_sec, (int)now.tv_usec,
2425 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2426 __FUNCTION__, s, scope,
2427 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2428 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002429
Christopher Faulet8ef75252017-02-20 22:56:03 +01002430 spoe_unset_var(ctx, scope, str, sz, &smp);
2431
2432 ret = (p - *buf);
2433 *buf = p;
2434 return ret;
2435 skip:
2436 return 0;
2437}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002438
Christopher Faulet8ef75252017-02-20 22:56:03 +01002439/* Process SPOE actions for a specific event. It returns 1 on success. If an
2440 * error occurred, 0 is returned. */
2441static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002442spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002443{
2444 char *p, *end;
2445 int ret;
2446
2447 p = ctx->buffer->p;
2448 end = p + ctx->buffer->i;
2449
2450 while (p < end) {
2451 enum spoe_action_type type;
2452
2453 type = *p++;
2454 switch (type) {
2455 case SPOE_ACT_T_SET_VAR:
2456 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2457 if (!ret)
2458 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002459 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002460
Christopher Faulet8ef75252017-02-20 22:56:03 +01002461 case SPOE_ACT_T_UNSET_VAR:
2462 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2463 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002464 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002465 break;
2466
2467 default:
2468 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002469 }
2470 }
2471
2472 return 1;
2473 skip:
2474 return 0;
2475}
2476
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002477/***************************************************************************
2478 * Functions that process SPOE events
2479 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002480static void
2481spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2482 struct spoe_context *ctx, int dir)
2483{
2484 if (!tv_iszero(&ctx->stats.tv_start)) {
2485 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2486 ctx->stats.t_total += ctx->stats.t_process;
2487 tv_zero(&ctx->stats.tv_request);
2488 tv_zero(&ctx->stats.tv_queue);
2489 tv_zero(&ctx->stats.tv_wait);
2490 tv_zero(&ctx->stats.tv_response);
2491 }
2492
2493 if (agent->var_t_process) {
2494 struct sample smp;
2495
2496 memset(&smp, 0, sizeof(smp));
2497 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2498 smp.data.u.sint = ctx->stats.t_process;
2499 smp.data.type = SMP_T_SINT;
2500
2501 spoe_set_var(ctx, "txn", agent->var_t_process,
2502 strlen(agent->var_t_process), &smp);
2503 }
2504
2505 if (agent->var_t_total) {
2506 struct sample smp;
2507
2508 memset(&smp, 0, sizeof(smp));
2509 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2510 smp.data.u.sint = ctx->stats.t_total;
2511 smp.data.type = SMP_T_SINT;
2512
2513 spoe_set_var(ctx, "txn", agent->var_t_total,
2514 strlen(agent->var_t_total), &smp);
2515 }
2516}
2517
2518static void
2519spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2520 struct spoe_context *ctx, int dir)
2521{
2522 if (agent->eps_max > 0)
2523 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2524
2525 if (agent->var_on_error) {
2526 struct sample smp;
2527
2528 memset(&smp, 0, sizeof(smp));
2529 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2530 smp.data.u.sint = ctx->status_code;
2531 smp.data.type = SMP_T_BOOL;
2532
2533 spoe_set_var(ctx, "txn", agent->var_on_error,
2534 strlen(agent->var_on_error), &smp);
2535 }
2536
2537 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2538 ? SPOE_CTX_ST_READY
2539 : SPOE_CTX_ST_NONE);
2540}
2541
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002542static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002543spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002544{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002545 /* If a process is already started for this SPOE context, retry
2546 * later. */
2547 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002548 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002549
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002550 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002551 ctx->stats.tv_start = now;
2552 ctx->stats.tv_request = now;
2553 ctx->stats.t_request = -1;
2554 ctx->stats.t_queue = -1;
2555 ctx->stats.t_waiting = -1;
2556 ctx->stats.t_response = -1;
2557 ctx->stats.t_process = -1;
2558
2559 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002560
Christopher Fauleta1cda022016-12-21 08:58:06 +01002561 /* Set the right flag to prevent request and response processing
2562 * in same time. */
2563 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2564 ? SPOE_CTX_FL_REQ_PROCESS
2565 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002566 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002567}
2568
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002569static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002570spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002571{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002572 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002573
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002574 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2575 return;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002576 HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002577 if (sa) {
2578 if (sa->frag_ctx.ctx == ctx) {
2579 sa->frag_ctx.ctx = NULL;
2580 spoe_wakeup_appctx(sa->owner);
2581 }
2582 else
2583 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002584 }
2585
Christopher Fauleta1cda022016-12-21 08:58:06 +01002586 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002587 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002588 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002589
2590 /* Reset processing timer */
2591 ctx->process_exp = TICK_ETERNITY;
2592
Christopher Faulet8ef75252017-02-20 22:56:03 +01002593 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002594
Christopher Fauletfce747b2018-01-24 15:59:32 +01002595 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002596 ctx->frag_ctx.curmsg = NULL;
2597 ctx->frag_ctx.curarg = NULL;
2598 ctx->frag_ctx.curoff = 0;
2599 ctx->frag_ctx.flags = 0;
2600
Christopher Fauleta1cda022016-12-21 08:58:06 +01002601 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002602 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Christopher Fauletebe13992018-04-26 11:33:44 +02002603 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002604 else
2605 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
2606
Christopher Fauleta1cda022016-12-21 08:58:06 +01002607 LIST_DEL(&ctx->list);
2608 LIST_INIT(&ctx->list);
2609 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002610}
2611
Christopher Faulet58d03682017-09-21 16:57:24 +02002612/* Process a list of SPOE messages. First, this functions will process messages
2613 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2614 * to process corresponding actions. During all the processing, it returns 0
2615 * and it returns 1 when the processing is finished. If an error occurred, -1
2616 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002617static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002618spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2619 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002620{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002621 struct spoe_config *conf = FLT_CONF(ctx->filter);
2622 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002623 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002624
2625 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002626 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002627
2628 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2629 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002630 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002631 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002632 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002633 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002634 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002635 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002636
2637 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002638 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002639 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002640 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002641 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002642 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002643 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002644 goto skip;
2645 }
2646 }
2647
Christopher Fauletf7a30922016-11-10 15:04:51 +01002648 if (!tick_isset(ctx->process_exp)) {
2649 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2650 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2651 ctx->process_exp);
2652 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002653 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002654 if (!ret)
2655 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002656
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002657 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002658 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002659 }
2660
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002661 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002662 if (!tv_iszero(&ctx->stats.tv_request))
2663 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002664 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002665 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002666 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002667 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002668 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002669 if (!ret)
2670 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002671 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002672 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002673 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2674 }
2675
2676 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002677 if (ctx->spoe_appctx)
2678 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002679 ret = 0;
2680 goto out;
2681 }
2682
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002683 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2684 ret = 0;
2685 goto out;
2686 }
2687
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002688 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002689 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002690 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002691 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002692 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002693 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002694 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002695 }
2696
2697 out:
2698 return ret;
2699
Christopher Fauleta1cda022016-12-21 08:58:06 +01002700 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002701 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002702 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002703 spoe_stop_processing(agent, ctx);
2704 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002705
Christopher Fauleta1cda022016-12-21 08:58:06 +01002706 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002707 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002708 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002709 if (ctx->status_code) {
2710 HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
2711 spoe_handle_processing_error(s, agent, ctx, dir);
2712 ret = 1;
2713 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002714 return ret;
2715}
2716
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002717/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2718 * See spoe_process_message for details. */
2719static int
2720spoe_process_group(struct stream *s, struct spoe_context *ctx,
2721 struct spoe_group *group, int dir)
2722{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002723 struct spoe_config *conf = FLT_CONF(ctx->filter);
2724 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002725 int ret;
2726
2727 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2728 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002729 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002730 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2731 group->id);
2732
2733 if (LIST_ISEMPTY(&group->messages))
2734 return 1;
2735
2736 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002737 if (ret && ctx->stats.t_process != -1) {
2738 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002739 " - <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 +01002740 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002741 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002742 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002743 ctx->stats.t_response, ctx->stats.t_process,
2744 agent->counters.idles, agent->counters.applets,
2745 agent->counters.nb_sending, agent->counters.nb_waiting,
2746 agent->counters.nb_errors, agent->counters.nb_processed,
2747 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2748 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2749 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2750 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2751 agent->id, group->id, s->uniq_id, ctx->status_code,
2752 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2753 ctx->stats.t_response, ctx->stats.t_process,
2754 agent->counters.idles, agent->counters.applets,
2755 agent->counters.nb_sending, agent->counters.nb_waiting,
2756 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002757 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002758 return ret;
2759}
2760
Christopher Faulet58d03682017-09-21 16:57:24 +02002761/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2762 * See spoe_process_message for details. */
2763static int
2764spoe_process_event(struct stream *s, struct spoe_context *ctx,
2765 enum spoe_event ev)
2766{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002767 struct spoe_config *conf = FLT_CONF(ctx->filter);
2768 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002769 int dir, ret;
2770
2771 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002772 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002773 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002774 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2775 spoe_event_str[ev]);
2776
2777 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2778
2779 if (LIST_ISEMPTY(&(ctx->events[ev])))
2780 return 1;
2781
2782 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002783 if (ret && ctx->stats.t_process != -1) {
2784 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002785 " - <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 +01002786 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2787 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2788 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002789 ctx->stats.t_response, ctx->stats.t_process,
2790 agent->counters.idles, agent->counters.applets,
2791 agent->counters.nb_sending, agent->counters.nb_waiting,
2792 agent->counters.nb_errors, agent->counters.nb_processed,
2793 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2794 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2795 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2796 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2797 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2798 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2799 ctx->stats.t_response, ctx->stats.t_process,
2800 agent->counters.idles, agent->counters.applets,
2801 agent->counters.nb_sending, agent->counters.nb_waiting,
2802 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002803 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002804 return ret;
2805}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002806
2807/***************************************************************************
2808 * Functions that create/destroy SPOE contexts
2809 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002810static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002811spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002812{
Christopher Faulet600d37e2017-11-10 11:54:58 +01002813 if ((*buf)->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002814 return 1;
2815
Christopher Faulet4596fb72017-01-11 14:05:19 +01002816 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002817 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002818 LIST_DEL(&buffer_wait->list);
2819 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002820 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002821 }
2822
Christopher Faulet4596fb72017-01-11 14:05:19 +01002823 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002824 return 1;
2825
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002826 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002827 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002828 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002829 return 0;
2830}
2831
2832static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002833spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002834{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002835 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002836 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002837 LIST_DEL(&buffer_wait->list);
2838 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002839 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002840 }
2841
2842 /* Release the buffer if needed */
Christopher Faulet600d37e2017-11-10 11:54:58 +01002843 if ((*buf)->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002844 b_free(buf);
Olivier Houchard673867c2018-05-25 16:58:52 +02002845 offer_buffers(buffer_wait->target, tasks_run_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002846 }
2847}
2848
Christopher Faulet4596fb72017-01-11 14:05:19 +01002849static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002850spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002851{
2852 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2853 return 1;
2854}
2855
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002856static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002857spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002858{
2859 struct spoe_config *conf = FLT_CONF(filter);
2860 struct spoe_context *ctx;
2861
Willy Tarreaubafbe012017-11-24 17:34:44 +01002862 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002863 if (ctx == NULL) {
2864 return NULL;
2865 }
2866 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002867 ctx->filter = filter;
2868 ctx->state = SPOE_CTX_ST_NONE;
2869 ctx->status_code = SPOE_CTX_ERR_NONE;
2870 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002871 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002872 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002873 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002874 LIST_INIT(&ctx->buffer_wait.list);
2875 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002876 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002877 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002878
Christopher Fauletf7a30922016-11-10 15:04:51 +01002879 ctx->stream_id = 0;
2880 ctx->frame_id = 1;
2881 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002882
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002883 tv_zero(&ctx->stats.tv_start);
2884 tv_zero(&ctx->stats.tv_request);
2885 tv_zero(&ctx->stats.tv_queue);
2886 tv_zero(&ctx->stats.tv_wait);
2887 tv_zero(&ctx->stats.tv_response);
2888 ctx->stats.t_request = -1;
2889 ctx->stats.t_queue = -1;
2890 ctx->stats.t_waiting = -1;
2891 ctx->stats.t_response = -1;
2892 ctx->stats.t_process = -1;
2893 ctx->stats.t_total = 0;
2894
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002895 ctx->strm = s;
2896 ctx->state = SPOE_CTX_ST_READY;
2897 filter->ctx = ctx;
2898
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002899 return ctx;
2900}
2901
2902static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002903spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002904{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002905 struct spoe_config *conf = FLT_CONF(filter);
2906 struct spoe_context *ctx = filter->ctx;
2907
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002908 if (!ctx)
2909 return;
2910
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002911 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002912 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002913 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002914}
2915
2916static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002917spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002918{
2919 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002920 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002921
2922 tv_zero(&ctx->stats.tv_start);
2923 tv_zero(&ctx->stats.tv_request);
2924 tv_zero(&ctx->stats.tv_queue);
2925 tv_zero(&ctx->stats.tv_wait);
2926 tv_zero(&ctx->stats.tv_response);
2927 ctx->stats.t_request = -1;
2928 ctx->stats.t_queue = -1;
2929 ctx->stats.t_waiting = -1;
2930 ctx->stats.t_response = -1;
2931 ctx->stats.t_process = -1;
2932 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002933}
2934
2935
2936/***************************************************************************
2937 * Hooks that manage the filter lifecycle (init/check/deinit)
2938 **************************************************************************/
2939/* Signal handler: Do a soft stop, wakeup SPOE applet */
2940static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002941spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002942{
2943 struct proxy *p;
2944
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002945 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002946 while (p) {
2947 struct flt_conf *fconf;
2948
2949 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002950 struct spoe_config *conf;
2951 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002952 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002953 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002954
Christopher Faulet3b386a32017-02-23 10:17:15 +01002955 if (fconf->id != spoe_filter_id)
2956 continue;
2957
2958 conf = fconf->conf;
2959 agent = conf->agent;
2960
Christopher Faulet24289f22017-09-25 14:48:02 +02002961 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002962 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002963 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2964 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002965 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002966 }
2967 }
2968 p = p->next;
2969 }
2970}
2971
2972
2973/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2974static int
2975spoe_init(struct proxy *px, struct flt_conf *fconf)
2976{
2977 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002978
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002979 /* conf->agent_fe was already initialized during the config
2980 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002981 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002982 conf->agent_fe.cap = PR_CAP_FE;
2983 conf->agent_fe.mode = PR_MODE_TCP;
2984 conf->agent_fe.maxconn = 0;
2985 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2986 conf->agent_fe.conn_retries = CONN_RETRIES;
2987 conf->agent_fe.accept = frontend_accept;
2988 conf->agent_fe.srv = NULL;
2989 conf->agent_fe.timeout.client = TICK_ETERNITY;
2990 conf->agent_fe.default_target = &spoe_applet.obj_type;
2991 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2992
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002993 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002994 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002995 sighandler_registered = 1;
2996 }
2997
2998 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002999}
3000
3001/* Free ressources allocated by the SPOE filter. */
3002static void
3003spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3004{
3005 struct spoe_config *conf = fconf->conf;
3006
3007 if (conf) {
3008 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003009
Christopher Faulet8ef75252017-02-20 22:56:03 +01003010 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003011 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003012 free(conf);
3013 }
3014 fconf->conf = NULL;
3015}
3016
3017/* Check configuration of a SPOE filter for a specified proxy.
3018 * Return 1 on error, else 0. */
3019static int
3020spoe_check(struct proxy *px, struct flt_conf *fconf)
3021{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003022 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003023 struct spoe_config *conf = fconf->conf;
3024 struct proxy *target;
3025
Christopher Faulet7ee86672017-09-19 11:08:28 +02003026 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3027 * are uniq */
3028 list_for_each_entry(f, &px->filter_configs, list) {
3029 struct spoe_config *c = f->conf;
3030
3031 /* This is not an SPOE filter */
3032 if (f->id != spoe_filter_id)
3033 continue;
3034 /* This is the current SPOE filter */
3035 if (f == fconf)
3036 continue;
3037
3038 /* Check engine Id. It should be uniq */
3039 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003040 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3041 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003042 return 1;
3043 }
3044 }
3045
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003046 target = proxy_be_by_name(conf->agent->b.name);
3047 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003048 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3049 " declared at %s:%d.\n",
3050 px->id, conf->agent->b.name, conf->agent->id,
3051 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003052 return 1;
3053 }
3054 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003055 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3056 " at %s:%d does not support HTTP mode.\n",
3057 px->id, target->id, conf->agent->id,
3058 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003059 return 1;
3060 }
3061
3062 free(conf->agent->b.name);
3063 conf->agent->b.name = NULL;
3064 conf->agent->b.be = target;
3065 return 0;
3066}
3067
3068/**************************************************************************
3069 * Hooks attached to a stream
3070 *************************************************************************/
3071/* Called when a filter instance is created and attach to a stream. It creates
3072 * the context that will be used to process this stream. */
3073static int
3074spoe_start(struct stream *s, struct filter *filter)
3075{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003076 struct spoe_config *conf = FLT_CONF(filter);
3077 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003078 struct spoe_context *ctx;
3079
3080 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003081 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003082 __FUNCTION__, s);
3083
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003084 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003085 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3086 " - failed to create SPOE context\n",
3087 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003088 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003089 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003090 "SPOE: [%s] failed to create SPOE context\n",
3091 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003092 return 0;
3093 }
3094
Christopher Faulet11610f32017-09-21 10:23:10 +02003095 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003096 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3097
Christopher Faulet11610f32017-09-21 10:23:10 +02003098 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003099 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3100
Christopher Faulet11610f32017-09-21 10:23:10 +02003101 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003102 filter->pre_analyzers |= AN_RES_INSPECT;
3103
Christopher Faulet11610f32017-09-21 10:23:10 +02003104 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003105 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3106
Christopher Faulet11610f32017-09-21 10:23:10 +02003107 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003108 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3109
Christopher Faulet11610f32017-09-21 10:23:10 +02003110 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003111 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3112
3113 return 1;
3114}
3115
3116/* Called when a filter instance is detached from a stream. It release the
3117 * attached SPOE context. */
3118static void
3119spoe_stop(struct stream *s, struct filter *filter)
3120{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003121 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3122 (int)now.tv_sec, (int)now.tv_usec,
3123 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3124 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003125 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003126}
3127
Christopher Fauletf7a30922016-11-10 15:04:51 +01003128
3129/*
3130 * Called when the stream is woken up because of expired timer.
3131 */
3132static void
3133spoe_check_timeouts(struct stream *s, struct filter *filter)
3134{
3135 struct spoe_context *ctx = filter->ctx;
3136
Christopher Fauletac580602018-03-20 16:09:20 +01003137 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003138 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003139}
3140
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003141/* Called when we are ready to filter data on a channel */
3142static int
3143spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3144{
3145 struct spoe_context *ctx = filter->ctx;
3146 int ret = 1;
3147
3148 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3149 " - ctx-flags=0x%08x\n",
3150 (int)now.tv_sec, (int)now.tv_usec,
3151 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3152 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3153
Christopher Fauletb067b062017-01-04 16:39:11 +01003154 if (ctx->state == SPOE_CTX_ST_NONE)
3155 goto out;
3156
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003157 if (!(chn->flags & CF_ISRESP)) {
3158 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3159 chn->analysers |= AN_REQ_INSPECT_FE;
3160 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3161 chn->analysers |= AN_REQ_INSPECT_BE;
3162
3163 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3164 goto out;
3165
3166 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003167 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003168 if (!ret)
3169 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003170 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3171 }
3172 else {
3173 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3174 chn->analysers |= AN_RES_INSPECT;
3175
3176 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3177 goto out;
3178
Christopher Faulet8ef75252017-02-20 22:56:03 +01003179 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003180 if (!ret) {
3181 channel_dont_read(chn);
3182 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003183 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003184 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003185 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003186 }
3187
3188 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003189 return ret;
3190}
3191
3192/* Called before a processing happens on a given channel */
3193static int
3194spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3195 struct channel *chn, unsigned an_bit)
3196{
3197 struct spoe_context *ctx = filter->ctx;
3198 int ret = 1;
3199
3200 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3201 " - ctx-flags=0x%08x - ana=0x%08x\n",
3202 (int)now.tv_sec, (int)now.tv_usec,
3203 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3204 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3205 ctx->flags, an_bit);
3206
Christopher Fauletb067b062017-01-04 16:39:11 +01003207 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003208 goto out;
3209
3210 switch (an_bit) {
3211 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003212 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003213 break;
3214 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003215 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003216 break;
3217 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003218 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003219 break;
3220 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003221 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003222 break;
3223 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003224 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003225 break;
3226 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003227 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003228 break;
3229 }
3230
3231 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003232 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003233 channel_dont_read(chn);
3234 channel_dont_close(chn);
3235 }
3236 return ret;
3237}
3238
3239/* Called when the filtering on the channel ends. */
3240static int
3241spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3242{
3243 struct spoe_context *ctx = filter->ctx;
3244
3245 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3246 " - ctx-flags=0x%08x\n",
3247 (int)now.tv_sec, (int)now.tv_usec,
3248 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3249 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3250
3251 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003252 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003253 }
3254
3255 return 1;
3256}
3257
3258/********************************************************************
3259 * Functions that manage the filter initialization
3260 ********************************************************************/
3261struct flt_ops spoe_ops = {
3262 /* Manage SPOE filter, called for each filter declaration */
3263 .init = spoe_init,
3264 .deinit = spoe_deinit,
3265 .check = spoe_check,
3266
3267 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003268 .attach = spoe_start,
3269 .detach = spoe_stop,
3270 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003271
3272 /* Handle channels activity */
3273 .channel_start_analyze = spoe_start_analyze,
3274 .channel_pre_analyze = spoe_chn_pre_analyze,
3275 .channel_end_analyze = spoe_end_analyze,
3276};
3277
3278
3279static int
3280cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3281{
3282 const char *err;
3283 int i, err_code = 0;
3284
3285 if ((cfg_scope == NULL && curengine != NULL) ||
3286 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003287 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003288 goto out;
3289
3290 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3291 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003292 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3293 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003294 err_code |= ERR_ALERT | ERR_ABORT;
3295 goto out;
3296 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003297 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3298 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003299 goto out;
3300 }
3301
3302 err = invalid_char(args[1]);
3303 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003304 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3305 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003306 err_code |= ERR_ALERT | ERR_ABORT;
3307 goto out;
3308 }
3309
3310 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003311 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3312 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003313 err_code |= ERR_ALERT | ERR_ABORT;
3314 goto out;
3315 }
3316 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003317 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003318 err_code |= ERR_ALERT | ERR_ABORT;
3319 goto out;
3320 }
3321
3322 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003323
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003324 curagent->conf.file = strdup(file);
3325 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003326
3327 curagent->timeout.hello = TICK_ETERNITY;
3328 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003329 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003330
3331 curagent->engine_id = NULL;
3332 curagent->var_pfx = NULL;
3333 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003334 curagent->var_t_process = NULL;
3335 curagent->var_t_total = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003336 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3337 if (global.nbthread == 1)
3338 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003339 curagent->cps_max = 0;
3340 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003341 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003342 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003343
3344 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003345 LIST_INIT(&curagent->events[i]);
3346 LIST_INIT(&curagent->groups);
3347 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003348
Christopher Faulet24289f22017-09-25 14:48:02 +02003349 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003350 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003351 err_code |= ERR_ALERT | ERR_ABORT;
3352 goto out;
3353 }
3354 for (i = 0; i < global.nbthread; ++i) {
3355 curagent->rt[i].frame_size = curagent->max_frame_size;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003356 curagent->rt[i].processing = 0;
Christopher Faulet24289f22017-09-25 14:48:02 +02003357 LIST_INIT(&curagent->rt[i].applets);
3358 LIST_INIT(&curagent->rt[i].sending_queue);
3359 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003360 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003361 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003362 }
3363 else if (!strcmp(args[0], "use-backend")) {
3364 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003365 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3366 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003367 err_code |= ERR_ALERT | ERR_FATAL;
3368 goto out;
3369 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003370 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003371 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003372 free(curagent->b.name);
3373 curagent->b.name = strdup(args[1]);
3374 }
3375 else if (!strcmp(args[0], "messages")) {
3376 int cur_arg = 1;
3377 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003378 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003379
Christopher Faulet11610f32017-09-21 10:23:10 +02003380 list_for_each_entry(ph, &curmphs, list) {
3381 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003382 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3383 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003384 err_code |= ERR_ALERT | ERR_FATAL;
3385 goto out;
3386 }
3387 }
3388
Christopher Faulet11610f32017-09-21 10:23:10 +02003389 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003390 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003391 err_code |= ERR_ALERT | ERR_ABORT;
3392 goto out;
3393 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003394 ph->id = strdup(args[cur_arg]);
3395 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003396 cur_arg++;
3397 }
3398 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003399 else if (!strcmp(args[0], "groups")) {
3400 int cur_arg = 1;
3401 while (*args[cur_arg]) {
3402 struct spoe_placeholder *ph = NULL;
3403
3404 list_for_each_entry(ph, &curgphs, list) {
3405 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003406 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3407 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003408 err_code |= ERR_ALERT | ERR_FATAL;
3409 goto out;
3410 }
3411 }
3412
3413 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003414 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003415 err_code |= ERR_ALERT | ERR_ABORT;
3416 goto out;
3417 }
3418 ph->id = strdup(args[cur_arg]);
3419 LIST_ADDQ(&curgphs, &ph->list);
3420 cur_arg++;
3421 }
3422 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003423 else if (!strcmp(args[0], "timeout")) {
3424 unsigned int *tv = NULL;
3425 const char *res;
3426 unsigned timeout;
3427
3428 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003429 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3430 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003431 err_code |= ERR_ALERT | ERR_FATAL;
3432 goto out;
3433 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003434 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3435 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003436 if (!strcmp(args[1], "hello"))
3437 tv = &curagent->timeout.hello;
3438 else if (!strcmp(args[1], "idle"))
3439 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003440 else if (!strcmp(args[1], "processing"))
3441 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003442 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003443 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3444 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003445 err_code |= ERR_ALERT | ERR_FATAL;
3446 goto out;
3447 }
3448 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003449 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3450 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003451 err_code |= ERR_ALERT | ERR_FATAL;
3452 goto out;
3453 }
3454 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3455 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003456 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3457 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003458 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003459 goto out;
3460 }
3461 *tv = MS_TO_TICKS(timeout);
3462 }
3463 else if (!strcmp(args[0], "option")) {
3464 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003465 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3466 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003467 err_code |= ERR_ALERT | ERR_FATAL;
3468 goto out;
3469 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003470
Christopher Faulet305c6072017-02-23 16:17:53 +01003471 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003472 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003473 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003474 if (kwm == 1)
3475 curagent->flags &= ~SPOE_FL_PIPELINING;
3476 else
3477 curagent->flags |= SPOE_FL_PIPELINING;
3478 goto out;
3479 }
3480 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003481 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003482 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003483 if (kwm == 1)
3484 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003485 else {
3486 if (global.nbthread == 1)
3487 curagent->flags |= SPOE_FL_ASYNC;
3488 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003489 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3490 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003491 err_code |= ERR_WARN;
3492 }
3493 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003494 goto out;
3495 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003496 else if (!strcmp(args[1], "send-frag-payload")) {
3497 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3498 goto out;
3499 if (kwm == 1)
3500 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3501 else
3502 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3503 goto out;
3504 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003505 else if (!strcmp(args[1], "dontlog-normal")) {
3506 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3507 goto out;
3508 if (kwm == 1)
3509 curpxopts2 &= ~PR_O2_NOLOGNORM;
3510 else
3511 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003512 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003513 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003514
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003515 /* Following options does not support negation */
3516 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003517 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3518 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003519 err_code |= ERR_ALERT | ERR_FATAL;
3520 goto out;
3521 }
3522
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003523 if (!strcmp(args[1], "var-prefix")) {
3524 char *tmp;
3525
3526 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003527 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3528 file, linenum, args[0],
3529 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003530 err_code |= ERR_ALERT | ERR_FATAL;
3531 goto out;
3532 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003533 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3534 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003535 tmp = args[2];
3536 while (*tmp) {
3537 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003538 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003539 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003540 err_code |= ERR_ALERT | ERR_FATAL;
3541 goto out;
3542 }
3543 tmp++;
3544 }
3545 curagent->var_pfx = strdup(args[2]);
3546 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003547 else if (!strcmp(args[1], "force-set-var")) {
3548 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3549 goto out;
3550 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3551 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003552 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003553 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003554 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003555 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3556 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003557 else if (!strcmp(args[1], "set-on-error")) {
3558 char *tmp;
3559
3560 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003561 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3562 file, linenum, args[0],
3563 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003564 err_code |= ERR_ALERT | ERR_FATAL;
3565 goto out;
3566 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003567 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3568 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003569 tmp = args[2];
3570 while (*tmp) {
3571 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003572 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003573 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003574 err_code |= ERR_ALERT | ERR_FATAL;
3575 goto out;
3576 }
3577 tmp++;
3578 }
3579 curagent->var_on_error = strdup(args[2]);
3580 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003581 else if (!strcmp(args[1], "set-process-time")) {
3582 char *tmp;
3583
3584 if (!*args[2]) {
3585 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3586 file, linenum, args[0],
3587 args[1]);
3588 err_code |= ERR_ALERT | ERR_FATAL;
3589 goto out;
3590 }
3591 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3592 goto out;
3593 tmp = args[2];
3594 while (*tmp) {
3595 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003596 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003597 file, linenum, args[0], args[1]);
3598 err_code |= ERR_ALERT | ERR_FATAL;
3599 goto out;
3600 }
3601 tmp++;
3602 }
3603 curagent->var_t_process = strdup(args[2]);
3604 }
3605 else if (!strcmp(args[1], "set-total-time")) {
3606 char *tmp;
3607
3608 if (!*args[2]) {
3609 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3610 file, linenum, args[0],
3611 args[1]);
3612 err_code |= ERR_ALERT | ERR_FATAL;
3613 goto out;
3614 }
3615 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3616 goto out;
3617 tmp = args[2];
3618 while (*tmp) {
3619 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003620 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003621 file, linenum, args[0], args[1]);
3622 err_code |= ERR_ALERT | ERR_FATAL;
3623 goto out;
3624 }
3625 tmp++;
3626 }
3627 curagent->var_t_total = strdup(args[2]);
3628 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003629 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003630 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3631 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003632 err_code |= ERR_ALERT | ERR_FATAL;
3633 goto out;
3634 }
Christopher Faulet48026722016-11-16 15:01:12 +01003635 }
3636 else if (!strcmp(args[0], "maxconnrate")) {
3637 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003638 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3639 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003640 err_code |= ERR_ALERT | ERR_FATAL;
3641 goto out;
3642 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003643 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003644 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003645 curagent->cps_max = atol(args[1]);
3646 }
3647 else if (!strcmp(args[0], "maxerrrate")) {
3648 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003649 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3650 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003651 err_code |= ERR_ALERT | ERR_FATAL;
3652 goto out;
3653 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003654 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003655 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003656 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003657 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003658 else if (!strcmp(args[0], "max-frame-size")) {
3659 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003660 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3661 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003662 err_code |= ERR_ALERT | ERR_FATAL;
3663 goto out;
3664 }
3665 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3666 goto out;
3667 curagent->max_frame_size = atol(args[1]);
3668 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3669 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003670 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3671 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003672 err_code |= ERR_ALERT | ERR_FATAL;
3673 goto out;
3674 }
3675 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003676 else if (!strcmp(args[0], "max-waiting-frames")) {
3677 if (!*args[1]) {
3678 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3679 file, linenum, args[0]);
3680 err_code |= ERR_ALERT | ERR_FATAL;
3681 goto out;
3682 }
3683 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3684 goto out;
3685 curagent->max_fpa = atol(args[1]);
3686 if (curagent->max_fpa < 1) {
3687 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3688 file, linenum, args[0]);
3689 err_code |= ERR_ALERT | ERR_FATAL;
3690 goto out;
3691 }
3692 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003693 else if (!strcmp(args[0], "register-var-names")) {
3694 int cur_arg;
3695
3696 if (!*args[1]) {
3697 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3698 file, linenum, args[0]);
3699 err_code |= ERR_ALERT | ERR_FATAL;
3700 goto out;
3701 }
3702 cur_arg = 1;
3703 while (*args[cur_arg]) {
3704 struct spoe_var_placeholder *vph;
3705
3706 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3707 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3708 err_code |= ERR_ALERT | ERR_ABORT;
3709 goto out;
3710 }
3711 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3712 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3713 err_code |= ERR_ALERT | ERR_ABORT;
3714 goto out;
3715 }
3716 LIST_ADDQ(&curvars, &vph->list);
3717 cur_arg++;
3718 }
3719 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003720 else if (!strcmp(args[0], "log")) {
3721 char *errmsg = NULL;
3722
3723 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3724 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3725 err_code |= ERR_ALERT | ERR_FATAL;
3726 goto out;
3727 }
3728 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003729 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003730 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3731 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003732 err_code |= ERR_ALERT | ERR_FATAL;
3733 goto out;
3734 }
3735 out:
3736 return err_code;
3737}
Christopher Faulet11610f32017-09-21 10:23:10 +02003738static int
3739cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3740{
3741 struct spoe_group *grp;
3742 const char *err;
3743 int err_code = 0;
3744
3745 if ((cfg_scope == NULL && curengine != NULL) ||
3746 (cfg_scope != NULL && curengine == NULL) ||
3747 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3748 goto out;
3749
3750 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3751 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003752 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3753 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003754 err_code |= ERR_ALERT | ERR_ABORT;
3755 goto out;
3756 }
3757 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3758 err_code |= ERR_ABORT;
3759 goto out;
3760 }
3761
3762 err = invalid_char(args[1]);
3763 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003764 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3765 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003766 err_code |= ERR_ALERT | ERR_ABORT;
3767 goto out;
3768 }
3769
3770 list_for_each_entry(grp, &curgrps, list) {
3771 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003772 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3773 " name as another one declared at %s:%d.\n",
3774 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003775 err_code |= ERR_ALERT | ERR_FATAL;
3776 goto out;
3777 }
3778 }
3779
3780 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003781 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003782 err_code |= ERR_ALERT | ERR_ABORT;
3783 goto out;
3784 }
3785
3786 curgrp->id = strdup(args[1]);
3787 curgrp->conf.file = strdup(file);
3788 curgrp->conf.line = linenum;
3789 LIST_INIT(&curgrp->phs);
3790 LIST_INIT(&curgrp->messages);
3791 LIST_ADDQ(&curgrps, &curgrp->list);
3792 }
3793 else if (!strcmp(args[0], "messages")) {
3794 int cur_arg = 1;
3795 while (*args[cur_arg]) {
3796 struct spoe_placeholder *ph = NULL;
3797
3798 list_for_each_entry(ph, &curgrp->phs, list) {
3799 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003800 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3801 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003802 err_code |= ERR_ALERT | ERR_FATAL;
3803 goto out;
3804 }
3805 }
3806
3807 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003808 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003809 err_code |= ERR_ALERT | ERR_ABORT;
3810 goto out;
3811 }
3812 ph->id = strdup(args[cur_arg]);
3813 LIST_ADDQ(&curgrp->phs, &ph->list);
3814 cur_arg++;
3815 }
3816 }
3817 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003818 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3819 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003820 err_code |= ERR_ALERT | ERR_FATAL;
3821 goto out;
3822 }
3823 out:
3824 return err_code;
3825}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003826
3827static int
3828cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3829{
3830 struct spoe_message *msg;
3831 struct spoe_arg *arg;
3832 const char *err;
3833 char *errmsg = NULL;
3834 int err_code = 0;
3835
3836 if ((cfg_scope == NULL && curengine != NULL) ||
3837 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003838 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003839 goto out;
3840
3841 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3842 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003843 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3844 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003845 err_code |= ERR_ALERT | ERR_ABORT;
3846 goto out;
3847 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003848 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3849 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003850 goto out;
3851 }
3852
3853 err = invalid_char(args[1]);
3854 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003855 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3856 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003857 err_code |= ERR_ALERT | ERR_ABORT;
3858 goto out;
3859 }
3860
3861 list_for_each_entry(msg, &curmsgs, list) {
3862 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003863 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3864 " name as another one declared at %s:%d.\n",
3865 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003866 err_code |= ERR_ALERT | ERR_FATAL;
3867 goto out;
3868 }
3869 }
3870
3871 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003872 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003873 err_code |= ERR_ALERT | ERR_ABORT;
3874 goto out;
3875 }
3876
3877 curmsg->id = strdup(args[1]);
3878 curmsg->id_len = strlen(curmsg->id);
3879 curmsg->event = SPOE_EV_NONE;
3880 curmsg->conf.file = strdup(file);
3881 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003882 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003883 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003884 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003885 LIST_INIT(&curmsg->by_evt);
3886 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003887 LIST_ADDQ(&curmsgs, &curmsg->list);
3888 }
3889 else if (!strcmp(args[0], "args")) {
3890 int cur_arg = 1;
3891
3892 curproxy->conf.args.ctx = ARGC_SPOE;
3893 curproxy->conf.args.file = file;
3894 curproxy->conf.args.line = linenum;
3895 while (*args[cur_arg]) {
3896 char *delim = strchr(args[cur_arg], '=');
3897 int idx = 0;
3898
3899 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003900 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003901 err_code |= ERR_ALERT | ERR_ABORT;
3902 goto out;
3903 }
3904
3905 if (!delim) {
3906 arg->name = NULL;
3907 arg->name_len = 0;
3908 delim = args[cur_arg];
3909 }
3910 else {
3911 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3912 arg->name_len = delim - args[cur_arg];
3913 delim++;
3914 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003915 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3916 &idx, file, linenum, &errmsg,
3917 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003918 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003919 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003920 err_code |= ERR_ALERT | ERR_FATAL;
3921 free(arg->name);
3922 free(arg);
3923 goto out;
3924 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003925 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003926 LIST_ADDQ(&curmsg->args, &arg->list);
3927 cur_arg++;
3928 }
3929 curproxy->conf.args.file = NULL;
3930 curproxy->conf.args.line = 0;
3931 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003932 else if (!strcmp(args[0], "acl")) {
3933 err = invalid_char(args[1]);
3934 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003935 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3936 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003937 err_code |= ERR_ALERT | ERR_FATAL;
3938 goto out;
3939 }
3940 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003941 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3942 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003943 err_code |= ERR_ALERT | ERR_FATAL;
3944 goto out;
3945 }
3946 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003947 else if (!strcmp(args[0], "event")) {
3948 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003949 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003950 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003951 goto out;
3952 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003953 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3954 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003955
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003956 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3957 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3958 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3959 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3960
3961 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3962 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3963 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3964 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3965 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3966 curmsg->event = SPOE_EV_ON_TCP_RSP;
3967
3968 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3969 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3970 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3971 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3972 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3973 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3974 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003975 ha_alert("parsing [%s:%d] : unkown event '%s'.\n",
3976 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003977 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003978 goto out;
3979 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003980
3981 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3982 struct acl_cond *cond;
3983
3984 cond = build_acl_cond(file, linenum, &curmsg->acls,
3985 curproxy, (const char **)args+2,
3986 &errmsg);
3987 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003988 ha_alert("parsing [%s:%d] : error detected while "
3989 "parsing an 'event %s' condition : %s.\n",
3990 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003991 err_code |= ERR_ALERT | ERR_FATAL;
3992 goto out;
3993 }
3994 curmsg->cond = cond;
3995 }
3996 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003997 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3998 "or 'unless' followed by a condition but found '%s'.\n",
3999 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004000 err_code |= ERR_ALERT | ERR_FATAL;
4001 goto out;
4002 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004003 }
4004 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004005 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4006 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004007 err_code |= ERR_ALERT | ERR_FATAL;
4008 goto out;
4009 }
4010 out:
4011 free(errmsg);
4012 return err_code;
4013}
4014
4015/* Return -1 on error, else 0 */
4016static int
4017parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4018 struct flt_conf *fconf, char **err, void *private)
4019{
4020 struct list backup_sections;
4021 struct spoe_config *conf;
4022 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004023 struct spoe_group *grp, *grpback;
4024 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004025 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004026 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004027 char *file = NULL, *engine = NULL;
4028 int ret, pos = *cur_arg + 1;
4029
Christopher Faulet84c844e2018-03-23 14:37:14 +01004030 LIST_INIT(&curmsgs);
4031 LIST_INIT(&curgrps);
4032 LIST_INIT(&curmphs);
4033 LIST_INIT(&curgphs);
4034 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004035 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004036 curpxopts = 0;
4037 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004038
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004039 conf = calloc(1, sizeof(*conf));
4040 if (conf == NULL) {
4041 memprintf(err, "%s: out of memory", args[*cur_arg]);
4042 goto error;
4043 }
4044 conf->proxy = px;
4045
4046 while (*args[pos]) {
4047 if (!strcmp(args[pos], "config")) {
4048 if (!*args[pos+1]) {
4049 memprintf(err, "'%s' : '%s' option without value",
4050 args[*cur_arg], args[pos]);
4051 goto error;
4052 }
4053 file = args[pos+1];
4054 pos += 2;
4055 }
4056 else if (!strcmp(args[pos], "engine")) {
4057 if (!*args[pos+1]) {
4058 memprintf(err, "'%s' : '%s' option without value",
4059 args[*cur_arg], args[pos]);
4060 goto error;
4061 }
4062 engine = args[pos+1];
4063 pos += 2;
4064 }
4065 else {
4066 memprintf(err, "unknown keyword '%s'", args[pos]);
4067 goto error;
4068 }
4069 }
4070 if (file == NULL) {
4071 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4072 goto error;
4073 }
4074
4075 /* backup sections and register SPOE sections */
4076 LIST_INIT(&backup_sections);
4077 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004078 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4079 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004080 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004081
4082 /* Parse SPOE filter configuration file */
4083 curengine = engine;
4084 curproxy = px;
4085 curagent = NULL;
4086 curmsg = NULL;
4087 ret = readcfgfile(file);
4088 curproxy = NULL;
4089
4090 /* unregister SPOE sections and restore previous sections */
4091 cfg_unregister_sections();
4092 cfg_restore_sections(&backup_sections);
4093
4094 if (ret == -1) {
4095 memprintf(err, "Could not open configuration file %s : %s",
4096 file, strerror(errno));
4097 goto error;
4098 }
4099 if (ret & (ERR_ABORT|ERR_FATAL)) {
4100 memprintf(err, "Error(s) found in configuration file %s", file);
4101 goto error;
4102 }
4103
4104 /* Check SPOE agent */
4105 if (curagent == NULL) {
4106 memprintf(err, "No SPOE agent found in file %s", file);
4107 goto error;
4108 }
4109 if (curagent->b.name == NULL) {
4110 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4111 curagent->id, curagent->conf.file, curagent->conf.line);
4112 goto error;
4113 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004114 if (curagent->timeout.hello == TICK_ETERNITY ||
4115 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004116 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004117 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4118 " | While not properly invalid, you will certainly encounter various problems\n"
4119 " | with such a configuration. To fix this, please ensure that all following\n"
4120 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4121 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004122 }
4123 if (curagent->var_pfx == NULL) {
4124 char *tmp = curagent->id;
4125
4126 while (*tmp) {
4127 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
4128 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4129 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4130 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4131 goto error;
4132 }
4133 tmp++;
4134 }
4135 curagent->var_pfx = strdup(curagent->id);
4136 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01004137 if (curagent->engine_id == NULL)
4138 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004139
Christopher Fauletb7426d12018-03-21 14:12:17 +01004140 if (curagent->var_on_error) {
4141 struct arg arg;
4142
4143 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4144 curagent->var_pfx, curagent->var_on_error);
4145
4146 arg.type = ARGT_STR;
4147 arg.data.str.str = trash.str;
4148 arg.data.str.len = trash.len;
4149 if (!vars_check_arg(&arg, err)) {
4150 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4151 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4152 goto error;
4153 }
4154 }
4155
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004156 if (curagent->var_t_process) {
4157 struct arg arg;
4158
4159 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4160 curagent->var_pfx, curagent->var_t_process);
4161
4162 arg.type = ARGT_STR;
4163 arg.data.str.str = trash.str;
4164 arg.data.str.len = trash.len;
4165 if (!vars_check_arg(&arg, err)) {
4166 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4167 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4168 goto error;
4169 }
4170 }
4171
4172 if (curagent->var_t_total) {
4173 struct arg arg;
4174
4175 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4176 curagent->var_pfx, curagent->var_t_total);
4177
4178 arg.type = ARGT_STR;
4179 arg.data.str.str = trash.str;
4180 arg.data.str.len = trash.len;
4181 if (!vars_check_arg(&arg, err)) {
4182 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4183 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4184 goto error;
4185 }
4186 }
4187
Christopher Faulet11610f32017-09-21 10:23:10 +02004188 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004189 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4190 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004191 goto finish;
4192 }
4193
Christopher Faulet11610f32017-09-21 10:23:10 +02004194 /* Replace placeholders by the corresponding messages for the SPOE
4195 * agent */
4196 list_for_each_entry(ph, &curmphs, list) {
4197 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004198 struct spoe_arg *arg;
4199 unsigned int where;
4200
Christopher Faulet11610f32017-09-21 10:23:10 +02004201 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004202 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4203 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4204 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4205 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4206 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4207 }
4208 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4209 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4210 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004211 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4212 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004213 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004214 }
4215 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004216 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4217 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004218 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004219 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004220
4221 where = 0;
4222 switch (msg->event) {
4223 case SPOE_EV_ON_CLIENT_SESS:
4224 where |= SMP_VAL_FE_CON_ACC;
4225 break;
4226
4227 case SPOE_EV_ON_TCP_REQ_FE:
4228 where |= SMP_VAL_FE_REQ_CNT;
4229 break;
4230
4231 case SPOE_EV_ON_HTTP_REQ_FE:
4232 where |= SMP_VAL_FE_HRQ_HDR;
4233 break;
4234
4235 case SPOE_EV_ON_TCP_REQ_BE:
4236 if (px->cap & PR_CAP_FE)
4237 where |= SMP_VAL_FE_REQ_CNT;
4238 if (px->cap & PR_CAP_BE)
4239 where |= SMP_VAL_BE_REQ_CNT;
4240 break;
4241
4242 case SPOE_EV_ON_HTTP_REQ_BE:
4243 if (px->cap & PR_CAP_FE)
4244 where |= SMP_VAL_FE_HRQ_HDR;
4245 if (px->cap & PR_CAP_BE)
4246 where |= SMP_VAL_BE_HRQ_HDR;
4247 break;
4248
4249 case SPOE_EV_ON_SERVER_SESS:
4250 where |= SMP_VAL_BE_SRV_CON;
4251 break;
4252
4253 case SPOE_EV_ON_TCP_RSP:
4254 if (px->cap & PR_CAP_FE)
4255 where |= SMP_VAL_FE_RES_CNT;
4256 if (px->cap & PR_CAP_BE)
4257 where |= SMP_VAL_BE_RES_CNT;
4258 break;
4259
4260 case SPOE_EV_ON_HTTP_RSP:
4261 if (px->cap & PR_CAP_FE)
4262 where |= SMP_VAL_FE_HRS_HDR;
4263 if (px->cap & PR_CAP_BE)
4264 where |= SMP_VAL_BE_HRS_HDR;
4265 break;
4266
4267 default:
4268 break;
4269 }
4270
4271 list_for_each_entry(arg, &msg->args, list) {
4272 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004273 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004274 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004275 "none of which is available here ('%s')",
4276 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004277 sample_ckp_names(arg->expr->fetch->use),
4278 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004279 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004280 }
4281 }
4282
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004283 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004284 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4285 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004286 }
4287 }
4288 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004289 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004290 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004291 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004292 continue;
4293 }
4294
Christopher Faulet11610f32017-09-21 10:23:10 +02004295 /* Replace placeholders by the corresponding groups for the SPOE
4296 * agent */
4297 list_for_each_entry(ph, &curgphs, list) {
4298 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4299 if (!strcmp(grp->id, ph->id)) {
4300 grp->agent = curagent;
4301 LIST_DEL(&grp->list);
4302 LIST_ADDQ(&curagent->groups, &grp->list);
4303 goto next_aph;
4304 }
4305 }
4306 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4307 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4308 goto error;
4309 next_aph:
4310 continue;
4311 }
4312
4313 /* Replace placeholders by the corresponding message for each SPOE
4314 * group of the SPOE agent */
4315 list_for_each_entry(grp, &curagent->groups, list) {
4316 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4317 list_for_each_entry(msg, &curmsgs, list) {
4318 if (!strcmp(msg->id, ph->id)) {
4319 if (msg->group != NULL) {
4320 memprintf(err, "SPOE message '%s' already belongs to "
4321 "the SPOE group '%s' declare at %s:%d",
4322 msg->id, msg->group->id,
4323 msg->group->conf.file,
4324 msg->group->conf.line);
4325 goto error;
4326 }
4327
4328 /* Scope for arguments are not checked for now. We will check
4329 * them only if a rule use the corresponding SPOE group. */
4330 msg->agent = curagent;
4331 msg->group = grp;
4332 LIST_DEL(&ph->list);
4333 LIST_ADDQ(&grp->messages, &msg->by_grp);
4334 goto next_mph_grp;
4335 }
4336 }
4337 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4338 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4339 goto error;
4340 next_mph_grp:
4341 continue;
4342 }
4343 }
4344
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004345 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004346 /* move curmsgs to the agent message list */
4347 curmsgs.n->p = &curagent->messages;
4348 curmsgs.p->n = &curagent->messages;
4349 curagent->messages = curmsgs;
4350 LIST_INIT(&curmsgs);
4351
Christopher Faulet7ee86672017-09-19 11:08:28 +02004352 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004353 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004354
4355 /* Start agent's proxy initialization here. It will be finished during
4356 * the filter init. */
4357 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4358 init_new_proxy(&conf->agent_fe);
4359 conf->agent_fe.id = conf->agent->id;
4360 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004361 conf->agent_fe.options |= curpxopts;
4362 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004363
4364 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4365 LIST_DEL(&logsrv->list);
4366 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4367 }
4368
Christopher Faulet11610f32017-09-21 10:23:10 +02004369 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4370 LIST_DEL(&ph->list);
4371 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004372 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004373 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4374 LIST_DEL(&ph->list);
4375 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004376 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004377 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4378 struct arg arg;
4379
4380 trash.len = snprintf(trash.str, trash.size, "proc.%s.%s",
4381 curagent->var_pfx, vph->name);
4382
4383 arg.type = ARGT_STR;
4384 arg.data.str.str = trash.str;
4385 arg.data.str.len = trash.len;
4386 if (!vars_check_arg(&arg, err)) {
4387 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4388 curagent->id, curagent->var_pfx, vph->name, *err);
4389 goto error;
4390 }
4391
4392 LIST_DEL(&vph->list);
4393 free(vph->name);
4394 free(vph);
4395 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004396 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4397 LIST_DEL(&grp->list);
4398 spoe_release_group(grp);
4399 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004400 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004401 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004402 fconf->ops = &spoe_ops;
4403 fconf->conf = conf;
4404 return 0;
4405
4406 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004407 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004408 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4409 LIST_DEL(&ph->list);
4410 spoe_release_placeholder(ph);
4411 }
4412 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4413 LIST_DEL(&ph->list);
4414 spoe_release_placeholder(ph);
4415 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004416 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4417 LIST_DEL(&vph->list);
4418 free(vph->name);
4419 free(vph);
4420 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004421 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4422 LIST_DEL(&grp->list);
4423 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004424 }
4425 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4426 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004427 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004428 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004429 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4430 LIST_DEL(&logsrv->list);
4431 free(logsrv);
4432 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004433 free(conf);
4434 return -1;
4435}
4436
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004437/* Send message of a SPOE group. This is the action_ptr callback of a rule
4438 * associated to a "send-spoe-group" action.
4439 *
4440 * It returns ACT_RET_CONT is processing is finished without error, it returns
4441 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4442 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004443static enum act_return
4444spoe_send_group(struct act_rule *rule, struct proxy *px,
4445 struct session *sess, struct stream *s, int flags)
4446{
4447 struct filter *filter;
4448 struct spoe_agent *agent = NULL;
4449 struct spoe_group *group = NULL;
4450 struct spoe_context *ctx = NULL;
4451 int ret, dir;
4452
4453 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4454 if (filter->config == rule->arg.act.p[0]) {
4455 agent = rule->arg.act.p[2];
4456 group = rule->arg.act.p[3];
4457 ctx = filter->ctx;
4458 break;
4459 }
4460 }
4461 if (agent == NULL || group == NULL || ctx == NULL)
4462 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004463 if (ctx->state == SPOE_CTX_ST_NONE)
4464 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004465
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004466 switch (rule->from) {
4467 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4468 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4469 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4470 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4471 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4472 default:
4473 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4474 " - internal error while execute spoe-send-group\n",
4475 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4476 __FUNCTION__, s);
4477 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4478 agent->id);
4479 return ACT_RET_CONT;
4480 }
4481
4482 ret = spoe_process_group(s, ctx, group, dir);
4483 if (ret == 1)
4484 return ACT_RET_CONT;
4485 else if (ret == 0) {
4486 if (flags & ACT_FLAG_FINAL) {
4487 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4488 " - failed to process group '%s': interrupted by caller\n",
4489 (int)now.tv_sec, (int)now.tv_usec,
4490 agent->id, __FUNCTION__, s, group->id);
4491 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004492 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004493 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004494 return ACT_RET_CONT;
4495 }
4496 return ACT_RET_YIELD;
4497 }
4498 else
4499 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004500}
4501
4502/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4503 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4504 * action should be:
4505 *
4506 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4507 *
4508 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4509 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4510 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4511 * group.
4512 *
4513 * The function returns 1 in success case, otherwise, it returns 0 and err is
4514 * filled.
4515 */
4516static int
4517check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4518{
4519 struct flt_conf *fconf;
4520 struct spoe_config *conf;
4521 struct spoe_agent *agent = NULL;
4522 struct spoe_group *group;
4523 struct spoe_message *msg;
4524 char *engine_id = rule->arg.act.p[0];
4525 char *group_id = rule->arg.act.p[1];
4526 unsigned int where = 0;
4527
4528 switch (rule->from) {
4529 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4530 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4531 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4532 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4533 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4534 default:
4535 memprintf(err,
4536 "internal error, unexpected rule->from=%d, please report this bug!",
4537 rule->from);
4538 goto error;
4539 }
4540
4541 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4542 * <px> */
4543 list_for_each_entry(fconf, &px->filter_configs, list) {
4544 conf = fconf->conf;
4545
4546 /* This is not an SPOE filter */
4547 if (fconf->id != spoe_filter_id)
4548 continue;
4549
4550 /* This is the good engine */
4551 if (!strcmp(conf->id, engine_id)) {
4552 agent = conf->agent;
4553 break;
4554 }
4555 }
4556 if (agent == NULL) {
4557 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4558 engine_id, group_id);
4559 goto error;
4560 }
4561
4562 /* Try to find the right group */
4563 list_for_each_entry(group, &agent->groups, list) {
4564 /* This is the good group */
4565 if (!strcmp(group->id, group_id))
4566 break;
4567 }
4568 if (&group->list == &agent->groups) {
4569 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4570 group_id, engine_id);
4571 goto error;
4572 }
4573
4574 /* Ok, we found the group, we need to check messages and their
4575 * arguments */
4576 list_for_each_entry(msg, &group->messages, by_grp) {
4577 struct spoe_arg *arg;
4578
4579 list_for_each_entry(arg, &msg->args, list) {
4580 if (!(arg->expr->fetch->val & where)) {
4581 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4582 "some args extract information from '%s',"
4583 "none of which is available here ('%s')",
4584 msg->id, group->id, msg->conf.file, msg->conf.line,
4585 sample_ckp_names(arg->expr->fetch->use),
4586 sample_ckp_names(where));
4587 goto error;
4588 }
4589 }
4590 }
4591
4592 free(engine_id);
4593 free(group_id);
4594 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4595 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4596 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4597 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4598 return 1;
4599
4600 error:
4601 free(engine_id);
4602 free(group_id);
4603 return 0;
4604}
4605
4606/* Parse 'send-spoe-group' action following the format:
4607 *
4608 * ... send-spoe-group <engine-id> <group-id>
4609 *
4610 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4611 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4612 * ids are saved and used later, when the rule will be checked.
4613 */
4614static enum act_parse_ret
4615parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4616 struct act_rule *rule, char **err)
4617{
4618 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4619 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4620 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4621 return ACT_RET_PRS_ERR;
4622 }
4623 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4624 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4625
4626 (*orig_arg) += 2;
4627
4628 rule->action = ACT_CUSTOM;
4629 rule->action_ptr = spoe_send_group;
4630 rule->check_ptr = check_send_spoe_group;
4631 return ACT_RET_PRS_OK;
4632}
4633
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004634
4635/* Declare the filter parser for "spoe" keyword */
4636static struct flt_kw_list flt_kws = { "SPOE", { }, {
4637 { "spoe", parse_spoe_flt, NULL },
4638 { NULL, NULL, NULL },
4639 }
4640};
4641
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004642/* Delcate the action parser for "spoe-action" keyword */
4643static struct action_kw_list tcp_req_action_kws = { { }, {
4644 { "send-spoe-group", parse_send_spoe_group },
4645 { /* END */ },
4646 }
4647};
4648static struct action_kw_list tcp_res_action_kws = { { }, {
4649 { "send-spoe-group", parse_send_spoe_group },
4650 { /* END */ },
4651 }
4652};
4653static struct action_kw_list http_req_action_kws = { { }, {
4654 { "send-spoe-group", parse_send_spoe_group },
4655 { /* END */ },
4656 }
4657};
4658static struct action_kw_list http_res_action_kws = { { }, {
4659 { "send-spoe-group", parse_send_spoe_group },
4660 { /* END */ },
4661 }
4662};
4663
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004664__attribute__((constructor))
4665static void __spoe_init(void)
4666{
4667 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004668 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4669 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4670 http_req_keywords_register(&http_req_action_kws);
4671 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004672
Willy Tarreaubafbe012017-11-24 17:34:44 +01004673 pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
4674 pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004675}
4676
4677__attribute__((destructor))
4678static void
4679__spoe_deinit(void)
4680{
Willy Tarreaubafbe012017-11-24 17:34:44 +01004681 pool_destroy(pool_head_spoe_ctx);
4682 pool_destroy(pool_head_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004683}