blob: 73e8f623a382db7e4c6b3797aef99a3fd08682fe [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);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200108static 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[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200317 /* 1.0 is now unsupported because of a bug about frame's flags*/
318 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200319 {NULL, 0, 0}
320};
321
322/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200323#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200324
Christopher Faulet8ef75252017-02-20 22:56:03 +0100325/* Convert a string to a SPOE version value. The string must follow the format
326 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
327 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200328static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100329spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200330{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100331 const char *p, *end;
332 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200333
Christopher Faulet8ef75252017-02-20 22:56:03 +0100334 p = str;
335 end = str+len;
336 maj = min = 0;
337 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200338
Christopher Faulet8ef75252017-02-20 22:56:03 +0100339 /* skip leading spaces */
340 while (p < end && isspace(*p))
341 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200342
Christopher Faulet8ef75252017-02-20 22:56:03 +0100343 /* parse Major number, until the '.' */
344 while (*p != '.') {
345 if (p >= end || *p < '0' || *p > '9')
346 goto out;
347 maj *= 10;
348 maj += (*p - '0');
349 p++;
350 }
351
352 /* check Major version */
353 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200354 goto out;
355
Christopher Faulet8ef75252017-02-20 22:56:03 +0100356 p++; /* skip the '.' */
357 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
358 goto out;
359
360 /* Parse Minor number */
361 while (p < end) {
362 if (*p < '0' || *p > '9')
363 break;
364 min *= 10;
365 min += (*p - '0');
366 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200367 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100368
369 /* check Minor number */
370 if (min > 999)
371 goto out;
372
373 /* skip trailing spaces */
374 while (p < end && isspace(*p))
375 p++;
376 if (p != end)
377 goto out;
378
379 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200380 out:
381 return vsn;
382}
383
Christopher Faulet8ef75252017-02-20 22:56:03 +0100384/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
385 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
386 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200387static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100388spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200389{
Willy Tarreau83061a82018-07-13 11:56:34 +0200390 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100391 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100392 char *p, *end;
393 unsigned int flags = SPOE_FRM_FL_FIN;
394 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200395
Christopher Faulet8ef75252017-02-20 22:56:03 +0100396 p = frame;
397 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200398
Christopher Faulet8ef75252017-02-20 22:56:03 +0100399 /* Set Frame type */
400 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200401
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100402 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200403 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100404 memcpy(p, (char *)&flags, 4);
405 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200406
407 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100408 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200409
410 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
411 * and "capabilities" */
412
413 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100414 sz = SLEN(SUPPORTED_VERSIONS_KEY);
415 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
416 goto too_big;
417
418 *p++ = SPOE_DATA_T_STR;
419 sz = SLEN(SUPPORTED_VERSIONS_VAL);
420 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
421 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200422
423 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100424 sz = SLEN(MAX_FRAME_SIZE_KEY);
425 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
426 goto too_big;
427
428 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200429 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100430 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200431
432 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100433 sz = SLEN(CAPABILITIES_KEY);
434 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
435 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200436
Christopher Faulet8ef75252017-02-20 22:56:03 +0100437 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100438 chk = get_trash_chunk();
439 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200440 memcpy(chk->area, "pipelining", 10);
441 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100442 }
443 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200444 if (chk->data) chk->area[chk->data++] = ',';
445 memcpy(chk->area+chk->data, "async", 5);
446 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100447 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100448 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200449 if (chk->data) chk->area[chk->data++] = ',';
450 memcpy(chk->area+chk->data, "fragmentation", 13);
451 chk->data += 5;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100452 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200453 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100454 goto too_big;
455
456 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100457 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100458 sz = SLEN(ENGINE_ID_KEY);
459 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
460 goto too_big;
461
462 *p++ = SPOE_DATA_T_STR;
463 sz = strlen(agent->engine_id);
464 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
465 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100466 }
467
Christopher Faulet8ef75252017-02-20 22:56:03 +0100468 return (p - frame);
469
470 too_big:
471 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
472 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200473}
474
Christopher Faulet8ef75252017-02-20 22:56:03 +0100475/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
476 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
477 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200478static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100479spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200480{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100481 const char *reason;
482 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100483 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100484 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200485
Christopher Faulet8ef75252017-02-20 22:56:03 +0100486 p = frame;
487 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200488
Christopher Faulet8ef75252017-02-20 22:56:03 +0100489 /* Set Frame type */
490 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200491
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100492 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200493 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100494 memcpy(p, (char *)&flags, 4);
495 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200496
497 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100498 *p++ = 0; *p++ = 0;
499
500 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
501 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200502
503 /* There are 2 mandatory items: "status-code" and "message" */
504
505 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100506 sz = SLEN(STATUS_CODE_KEY);
507 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
508 goto too_big;
509
510 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200511 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100512 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200513
514 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100515 sz = SLEN(MSG_KEY);
516 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
517 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200518
Christopher Faulet8ef75252017-02-20 22:56:03 +0100519 /*Get the message corresponding to the status code */
520 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
521
522 *p++ = SPOE_DATA_T_STR;
523 sz = strlen(reason);
524 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
525 goto too_big;
526
527 return (p - frame);
528
529 too_big:
530 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
531 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200532}
533
Christopher Faulet8ef75252017-02-20 22:56:03 +0100534/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
535 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
536 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200537static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100538spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100539 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200540{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100541 char *p, *end;
542 unsigned int stream_id, frame_id;
543 unsigned int flags = SPOE_FRM_FL_FIN;
544 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200545
Christopher Faulet8ef75252017-02-20 22:56:03 +0100546 p = frame;
547 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200548
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100549 stream_id = ctx->stream_id;
550 frame_id = ctx->frame_id;
551
552 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
553 /* The fragmentation is not supported by the applet */
554 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
555 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
556 return -1;
557 }
558 flags = ctx->frag_ctx.flags;
559 }
560
561 /* Set Frame type */
562 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
563
564 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200565 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100566 memcpy(p, (char *)&flags, 4);
567 p += 4;
568
569 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200570 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100571 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200572 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100573 goto too_big;
574
575 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200576 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100577 if (p + sz >= end)
578 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200579 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100580 p += sz;
581
582 return (p - frame);
583
584 too_big:
585 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
586 return 0;
587}
588
589/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
590 * returns the number of encoded bytes in the frame on success, 0 if an encoding
591 * error occurred and -1 if a fatal error occurred. */
592static int
593spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
594 char *frame, size_t size)
595{
596 char *p, *end;
597 unsigned int stream_id, frame_id;
598 unsigned int flags;
599 size_t sz;
600
601 p = frame;
602 end = frame+size;
603
Christopher Faulet8ef75252017-02-20 22:56:03 +0100604 /* <ctx> is null when the stream has aborted the processing of a
605 * fragmented frame. In this case, we must notify the corresponding
606 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100607 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100608 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100609 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
610 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
611 }
612 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100613 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100614 stream_id = ctx->stream_id;
615 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100616 }
617
Christopher Faulet8ef75252017-02-20 22:56:03 +0100618 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100619 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100620
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100621 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200622 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100623 memcpy(p, (char *)&flags, 4);
624 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200625
626 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200627 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100628 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200629 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100630 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200631
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100632 if (ctx == NULL)
633 goto end;
634
Christopher Faulet8ef75252017-02-20 22:56:03 +0100635 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200636 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100637 if (p + sz >= end)
638 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200639 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100640 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100641
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100642 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100643 return (p - frame);
644
645 too_big:
646 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
647 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200648}
649
Christopher Faulet8ef75252017-02-20 22:56:03 +0100650/* Decode and process the HELLO frame sent by an agent. It returns the number of
651 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
652 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200653static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100654spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200655{
Christopher Faulet305c6072017-02-23 16:17:53 +0100656 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
657 char *p, *end;
658 int vsn, max_frame_size;
659 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100660
661 p = frame;
662 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200663
664 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100665 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
666 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200667 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100668 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200669
Christopher Faulet8ef75252017-02-20 22:56:03 +0100670 if (size < 7 /* TYPE + METADATA */) {
671 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
672 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200673 }
674
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100675 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100676 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200677 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100678 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200679
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100680 /* Fragmentation is not supported for HELLO frame */
681 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100682 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100683 return -1;
684 }
685
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200686 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100687 if (*p != 0 || *(p+1) != 0) {
688 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
689 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200690 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100691 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200692
693 /* There are 3 mandatory items: "version", "max-frame-size" and
694 * "capabilities" */
695
696 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100697 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100698 while (p < end) {
699 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200700 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100701 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200702
703 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100704 ret = spoe_decode_buffer(&p, end, &str, &sz);
705 if (ret == -1 || !sz) {
706 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
707 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200708 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100709
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200710 /* Check "version" K/V item */
711 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100712 int i, type = *p++;
713
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200714 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100715 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
716 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
717 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200718 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100719 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
720 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
721 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200722 }
723
Christopher Faulet8ef75252017-02-20 22:56:03 +0100724 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200725 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100726 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200727 return -1;
728 }
729 for (i = 0; supported_versions[i].str != NULL; ++i) {
730 if (vsn >= supported_versions[i].min &&
731 vsn <= supported_versions[i].max)
732 break;
733 }
734 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100735 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200736 return -1;
737 }
738 }
739 /* Check "max-frame-size" K/V item */
740 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100741 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200742
743 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200744 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
745 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
746 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
747 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100748 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
749 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200750 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200751 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100752 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
753 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200754 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100755 if (sz < MIN_FRAME_SIZE ||
756 sz > SPOE_APPCTX(appctx)->max_frame_size) {
757 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200758 return -1;
759 }
760 max_frame_size = sz;
761 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100762 /* Check "capabilities" K/V item */
763 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100764 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100765
766 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100767 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
768 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
769 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100770 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100771 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
772 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
773 return 0;
774 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100775
Christopher Faulet8ef75252017-02-20 22:56:03 +0100776 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100777 char *delim;
778
779 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100780 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100781
Christopher Faulet8ef75252017-02-20 22:56:03 +0100782 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
783 str += 10; sz -= 10;
784 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100785 flags |= SPOE_APPCTX_FL_PIPELINING;
786 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100787 else if (sz >= 5 && !strncmp(str, "async", 5)) {
788 str += 5; sz -= 5;
789 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100790 flags |= SPOE_APPCTX_FL_ASYNC;
791 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100792 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
793 str += 13; sz -= 13;
794 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100795 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
796 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100797
Christopher Faulet8ef75252017-02-20 22:56:03 +0100798 /* Get the next comma or break */
799 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100800 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100801 delim++;
802 sz -= (delim - str);
803 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100804 }
805 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200806 else {
807 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100808 if (spoe_skip_data(&p, end) == -1) {
809 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
810 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200811 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200812 }
813 }
814
815 /* Final checks */
816 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100817 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200818 return -1;
819 }
820 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100821 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200822 return -1;
823 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100824 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
825 flags &= ~SPOE_APPCTX_FL_PIPELINING;
826 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
827 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200828
Christopher Faulet42bfa462017-01-04 14:14:19 +0100829 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
830 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
831 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100832
833 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200834}
835
836/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
837 * bytes on success, 0 if the frame can be ignored and -1 if an error
838 * occurred. */
839static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100840spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200841{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100842 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100843 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100844
845 p = frame;
846 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200847
848 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100849 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
850 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200851 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100852 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200853
Christopher Faulet8ef75252017-02-20 22:56:03 +0100854 if (size < 7 /* TYPE + METADATA */) {
855 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
856 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200857 }
858
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100859 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100860 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200861 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100862 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200863
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100864 /* Fragmentation is not supported for DISCONNECT frame */
865 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100866 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100867 return -1;
868 }
869
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200870 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100871 if (*p != 0 || *(p+1) != 0) {
872 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
873 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200874 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100875 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200876
877 /* There are 2 mandatory items: "status-code" and "message" */
878
879 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100880 while (p < end) {
881 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200882 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100883 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200884
885 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100886 ret = spoe_decode_buffer(&p, end, &str, &sz);
887 if (ret == -1 || !sz) {
888 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
889 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200890 }
891
892 /* Check "status-code" K/V item */
893 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100894 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200895
896 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200897 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
898 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
899 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
900 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100901 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
902 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200903 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200904 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100905 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
906 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200907 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100908 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200909 }
910
911 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100912 else if (!memcmp(str, MSG_KEY, sz)) {
913 int type = *p++;
914
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200915 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100916 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
917 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
918 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200919 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100920 ret = spoe_decode_buffer(&p, end, &str, &sz);
921 if (ret == -1 || sz > 255) {
922 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
923 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200924 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100925#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
926 SPOE_APPCTX(appctx)->reason = str;
927 SPOE_APPCTX(appctx)->rlen = sz;
928#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200929 }
930 else {
931 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100932 if (spoe_skip_data(&p, end) == -1) {
933 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
934 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200935 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200936 }
937 }
938
Christopher Faulet8ef75252017-02-20 22:56:03 +0100939 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200940}
941
942
Christopher Fauleta1cda022016-12-21 08:58:06 +0100943/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200944 * success, 0 if the frame can be ignored and -1 if an error occurred. */
945static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100946spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100947 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200948{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100949 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100950 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100951 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100952 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100953 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100954
955 p = frame;
956 end = frame + size;
957 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200958
959 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100960 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
961 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200962 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100963 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200964
Christopher Faulet8ef75252017-02-20 22:56:03 +0100965 if (size < 7 /* TYPE + METADATA */) {
966 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
967 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200968 }
969
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100970 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100971 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200972 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100973 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200974
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100975 /* Fragmentation is not supported for now */
976 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100977 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100978 return -1;
979 }
980
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200981 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200982 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100983 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100984 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100985 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200986 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100987 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200988 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100989 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100990
Christopher Faulet8ef75252017-02-20 22:56:03 +0100991 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100992 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200993 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100994 if ((*ctx)->stream_id == (unsigned int)stream_id &&
995 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100996 goto found;
997 }
998 }
999 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001000 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
1001 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +01001002 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001003 goto found;
1004 }
1005 }
1006
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001007 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
1008 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
1009 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
1010
1011 /* ABRT bit is set for an unfinished fragmented frame */
1012 if (flags & SPOE_FRM_FL_ABRT) {
1013 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001014 (*ctx)->state = SPOE_CTX_ST_ERROR;
1015 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1016 /* Ignore the payload */
1017 goto end;
1018 }
1019 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1020 /* For now, we ignore the ack */
1021 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1022 return 0;
1023 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001024
Christopher Fauleta1cda022016-12-21 08:58:06 +01001025 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001026 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1027 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001028 " - stream-id=%u - frame-id=%u\n",
1029 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1030 __FUNCTION__, appctx,
1031 (unsigned int)stream_id, (unsigned int)frame_id);
1032
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001033 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Faulet3a47e5e2018-05-25 10:42:37 +02001034 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1035 return -1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001036 return 0;
1037
1038 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001039 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1040 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001041 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001042 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001043 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001044
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001045 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001046 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001047 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1048 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001049 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001050
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001051 /* Transfer the buffer ownership to the SPOE context */
1052 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001053 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001054
Christopher Faulet8ef75252017-02-20 22:56:03 +01001055 (*ctx)->state = SPOE_CTX_ST_DONE;
1056
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001057 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001058 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001059 " - ACK frame received"
1060 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001061 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001062 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1063 (*ctx)->frame_id, flags);
1064 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001065}
1066
Christopher Fauletba7bc162016-11-07 21:07:38 +01001067/* This function is used in cfgparse.c and declared in proto/checks.h. It
1068 * prepare the request to send to agents during a healthcheck. It returns 0 on
1069 * success and -1 if an error occurred. */
1070int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001071spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001072{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001073 struct appctx appctx;
1074 struct spoe_appctx spoe_appctx;
1075 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1076 size_t sz;
1077 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001078
Christopher Faulet42bfa462017-01-04 14:14:19 +01001079 memset(&appctx, 0, sizeof(appctx));
1080 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001081 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001082
1083 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001084 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001085
Christopher Faulet8ef75252017-02-20 22:56:03 +01001086 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1087 end = frame + MAX_FRAME_SIZE;
1088
1089 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1090 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001091 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001092 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093
Christopher Faulet8ef75252017-02-20 22:56:03 +01001094 /* Add "healthcheck" K/V item */
1095 sz = SLEN(HEALTHCHECK_KEY);
1096 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1097 return -1;
1098 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001099
Christopher Faulet8ef75252017-02-20 22:56:03 +01001100 *len = frame - buf;
1101 sz = htonl(*len - 4);
1102 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001103
Christopher Faulet8ef75252017-02-20 22:56:03 +01001104 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001105 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001106 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001107 return 0;
1108}
1109
1110/* This function is used in checks.c and declared in proto/checks.h. It decode
1111 * the response received from an agent during a healthcheck. It returns 0 on
1112 * success and -1 if an error occurred. */
1113int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001114spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001115{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001116 struct appctx appctx;
1117 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001118
Christopher Faulet42bfa462017-01-04 14:14:19 +01001119 memset(&appctx, 0, sizeof(appctx));
1120 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001121
Christopher Faulet42bfa462017-01-04 14:14:19 +01001122 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001123 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001124
Christopher Faulet8ef75252017-02-20 22:56:03 +01001125 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1126 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001127 goto error;
1128 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001129 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1130 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001131
1132 return 0;
1133
1134 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001135 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1136 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1137 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001138 return -1;
1139}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001140
Christopher Fauleta1cda022016-12-21 08:58:06 +01001141/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1142 * the frame can be ignored, 1 to retry later, and the frame legnth on
1143 * success. */
1144static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001145spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001146{
1147 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001148 int ret;
1149 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001150
Christopher Faulet8ef75252017-02-20 22:56:03 +01001151 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1152 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001153 netint = htonl(framesz);
1154 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001155 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001156 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001157 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001158 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001159 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001160 }
1161 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001162 return -1; /* error */
1163 }
1164 return framesz;
1165}
1166
1167/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1168 * when the frame can be ignored, 1 to retry later and the frame length on
1169 * success. */
1170static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001171spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001172{
1173 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001174 int ret;
1175 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001176
Willy Tarreau06d80a92017-10-19 14:32:15 +02001177 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001178 if (ret > 0) {
1179 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001180 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001181 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001182 return -1;
1183 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001184 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001185 }
1186 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001187 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001188 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001189 }
1190 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001191 return -1; /* error */
1192 }
1193 return framesz;
1194}
1195
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001196/********************************************************************
1197 * Functions that manage the SPOE applet
1198 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001199static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001200spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001201{
1202 si_applet_want_get(appctx->owner);
1203 si_applet_want_put(appctx->owner);
1204 appctx_wakeup(appctx);
1205 return 1;
1206}
1207
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001208/* Callback function that catches applet timeouts. If a timeout occurred, we set
1209 * <appctx->st1> flag and the SPOE applet is woken up. */
1210static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001211spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001212{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001213 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001214
1215 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1216 if (tick_is_expired(task->expire, now_ms)) {
1217 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001218 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001219 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001220 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001221 return task;
1222}
1223
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001224/* Callback function that releases a SPOE applet. This happens when the
1225 * connection with the agent is closed. */
1226static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001227spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001228{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001229 struct stream_interface *si = appctx->owner;
1230 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1231 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001232 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001233
1234 if (spoe_appctx == NULL)
1235 return;
1236
1237 appctx->ctx.spoe.ptr = NULL;
1238 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001239
1240 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1241 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1242 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001243
Christopher Faulet8ef75252017-02-20 22:56:03 +01001244 /* Remove applet from the list of running applets */
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001245 HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001246 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001247 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1248 LIST_DEL(&spoe_appctx->list);
1249 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001250 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001251 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001252
Christopher Faulet8ef75252017-02-20 22:56:03 +01001253 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001254 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001255 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1256 eb32_delete(&spoe_appctx->node);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001257 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001258 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001259
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001260 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001261 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1262 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001263
1264 si_shutw(si);
1265 si_shutr(si);
1266 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001267 }
1268
Christopher Faulet8ef75252017-02-20 22:56:03 +01001269 /* Destroy the task attached to this applet */
1270 if (spoe_appctx->task) {
1271 task_delete(spoe_appctx->task);
1272 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001273 }
1274
Christopher Faulet8ef75252017-02-20 22:56:03 +01001275 /* Notify all waiting streams */
1276 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001277 LIST_DEL(&ctx->list);
1278 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001279 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001280 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001281 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001282 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001283 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001284 }
1285
Christopher Faulet8ef75252017-02-20 22:56:03 +01001286 /* If the applet was processing a fragmented frame, notify the
1287 * corresponding stream. */
1288 if (spoe_appctx->frag_ctx.ctx) {
1289 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001290 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001291 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001292 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001293 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1294 }
1295
Christopher Faulet8ef75252017-02-20 22:56:03 +01001296 /* Release allocated memory */
1297 spoe_release_buffer(&spoe_appctx->buffer,
1298 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001299 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001300
Christopher Faulet24289f22017-09-25 14:48:02 +02001301 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001302 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001303
Christopher Faulet8ef75252017-02-20 22:56:03 +01001304 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001305 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001306 LIST_DEL(&ctx->list);
1307 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001308 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001309 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001310 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001311 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001312 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001313 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001314 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001315 LIST_DEL(&ctx->list);
1316 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001317 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001318 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001319 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001320 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001321 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1322 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001323
1324 end:
1325 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001326 agent->rt[tid].frame_size = agent->max_frame_size;
1327 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1328 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001329}
1330
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001331static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001332spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001333{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001334 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001335 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001336 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001337 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001338
Christopher Fauleta1cda022016-12-21 08:58:06 +01001339 if (si->state <= SI_ST_CON) {
1340 si_applet_want_put(si);
1341 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1342 goto stop;
1343 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001344 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001345 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001346 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001347 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001348
Christopher Fauleta1cda022016-12-21 08:58:06 +01001349 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001350 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1351 " - Connection timed out\n",
1352 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1353 __FUNCTION__, appctx);
1354 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001355 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001356 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001357
Christopher Faulet42bfa462017-01-04 14:14:19 +01001358 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001359 SPOE_APPCTX(appctx)->task->expire =
1360 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001361
Christopher Faulet8ef75252017-02-20 22:56:03 +01001362 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1363 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001364 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001365 ret = spoe_prepare_hahello_frame(appctx, frame,
1366 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001367 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001368 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001369
1370 switch (ret) {
1371 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001372 case 0: /* ignore => an error, cannot be ignored */
1373 goto exit;
1374
1375 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001376 goto stop;
1377
Christopher Faulet8ef75252017-02-20 22:56:03 +01001378 default:
1379 /* HELLO frame successfully sent, now wait for the
1380 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001381 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1382 goto next;
1383 }
1384
1385 next:
1386 return 0;
1387 stop:
1388 return 1;
1389 exit:
1390 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1391 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001392}
1393
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001394static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001395spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001396{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001397 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001398 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001399 char *frame;
1400 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001401
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001402
Christopher Fauletb067b062017-01-04 16:39:11 +01001403 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001404 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001405 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001406 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001407
Christopher Fauleta1cda022016-12-21 08:58:06 +01001408 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001409 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1410 " - Connection timed out\n",
1411 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1412 __FUNCTION__, appctx);
1413 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001414 goto exit;
1415 }
1416
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001417 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001418 ret = spoe_recv_frame(appctx, frame,
1419 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001420 if (ret > 1) {
1421 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1422 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1423 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001424 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001425 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001426 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001427 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001428
Christopher Fauleta1cda022016-12-21 08:58:06 +01001429 switch (ret) {
1430 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001431 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001432 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1433 goto next;
1434
1435 case 1: /* retry later */
1436 goto stop;
1437
1438 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001439 /* HELLO handshake is finished, set the idle timeout and
1440 * add the applet in the list of running applets. */
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001441 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001442 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001443 SPOE_APPCTX(appctx)->node.key = 0;
1444 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001445
1446 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001447 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001448 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001449 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001450
Christopher Fauleta1cda022016-12-21 08:58:06 +01001451 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001452 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001453 if (trash.data)
1454 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001455
1456 SPOE_APPCTX(appctx)->task->expire =
1457 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001458 return 0;
1459 stop:
1460 return 1;
1461 exit:
1462 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1463 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001464}
1465
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001466
Christopher Fauleta1cda022016-12-21 08:58:06 +01001467static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001468spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001469{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001470 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1471 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001472 char *frame, *buf;
1473 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001474
Christopher Faulet8ef75252017-02-20 22:56:03 +01001475 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1476 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001477 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001478
1479 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1480 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1481 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1482 SPOE_APPCTX(appctx)->max_frame_size);
1483 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001484 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001485 *skip = 1;
1486 ret = 1;
1487 goto end;
1488 }
1489 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001490 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001491 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1492 SPOE_APPCTX(appctx)->max_frame_size);
1493
1494 }
1495
Christopher Faulet8ef75252017-02-20 22:56:03 +01001496 if (ret > 1)
1497 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001498
Christopher Faulet8ef75252017-02-20 22:56:03 +01001499 switch (ret) {
1500 case -1: /* error */
1501 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1502 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001503
Christopher Faulet8ef75252017-02-20 22:56:03 +01001504 case 0: /* ignore */
1505 if (ctx == NULL)
1506 goto abort_frag_frame;
1507
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001508 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001509 LIST_DEL(&ctx->list);
1510 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001511 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001512 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001513 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001514 ctx->state = SPOE_CTX_ST_ERROR;
1515 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1516 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001517 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001518 break;
1519
1520 case 1: /* retry */
1521 *skip = 1;
1522 break;
1523
1524 default:
1525 if (ctx == NULL)
1526 goto abort_frag_frame;
1527
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001528 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001529 LIST_DEL(&ctx->list);
1530 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001531 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001532 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001533 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001534 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1535 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1536 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001537 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001538 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001539 }
1540 goto end;
1541
1542 frag_frame_sent:
1543 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001544 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001545 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1546 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1547 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001548 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1549 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1550 goto end;
1551
1552 no_frag_frame_sent:
1553 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1554 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001555 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001556 }
1557 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1558 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1559 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1560 }
1561 else {
1562 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001563 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001564 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1565 }
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001566 HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001567 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001568 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1569 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1570 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001571 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001572
Christopher Faulet8ef75252017-02-20 22:56:03 +01001573 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1574 goto end;
1575
1576 abort_frag_frame:
1577 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1578 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1579 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1580 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1581 goto end;
1582
1583 end:
1584 return ret;
1585}
1586
1587static int
1588spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1589{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001590 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001591 struct spoe_context *ctx = NULL;
1592 char *frame;
1593 int ret;
1594
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001595 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001596 ret = spoe_recv_frame(appctx, frame,
1597 SPOE_APPCTX(appctx)->max_frame_size);
1598 if (ret > 1) {
1599 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1600 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001601 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001602 goto end;
1603 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001604 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001605 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1606 }
1607 switch (ret) {
1608 case -1: /* error */
1609 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1610 break;
1611
1612 case 0: /* ignore */
1613 break;
1614
1615 case 1: /* retry */
1616 *skip = 1;
1617 break;
1618
1619 default:
1620 LIST_DEL(&ctx->list);
1621 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001622 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001623 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1624 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001625 if (ctx->spoe_appctx) {
1626 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001627 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001628 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001629 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1630 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1631 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1632 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1633 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1634 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1635 }
1636 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1637 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001638 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1639 break;
1640 }
1641
1642 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001643 if (trash.data)
1644 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001645 end:
1646 return ret;
1647}
1648
1649static int
1650spoe_handle_processing_appctx(struct appctx *appctx)
1651{
1652 struct stream_interface *si = appctx->owner;
1653 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001654 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001655
1656 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1657 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1658 goto exit;
1659 }
1660
1661 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1662 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1663 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1664 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1665 goto next;
1666 }
1667
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001668 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001669 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001670 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001671 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1672 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001673 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001674
Christopher Faulet8f82b202018-01-24 16:23:03 +01001675 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1676 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001677
Christopher Faulet8f82b202018-01-24 16:23:03 +01001678 /* receiving_frame loop */
1679 while (!skip_receiving) {
1680 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1681 switch (ret) {
1682 case -1: /* error */
1683 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001684
Christopher Faulet8f82b202018-01-24 16:23:03 +01001685 case 0: /* ignore */
1686 active_r = 1;
1687 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001688
Christopher Faulet8f82b202018-01-24 16:23:03 +01001689 case 1: /* retry */
1690 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001691
Christopher Faulet8f82b202018-01-24 16:23:03 +01001692 default:
1693 active_r = 1;
1694 break;
1695 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001696 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001697
Christopher Faulet8f82b202018-01-24 16:23:03 +01001698 /* send_frame loop */
1699 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1700 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1701 switch (ret) {
1702 case -1: /* error */
1703 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001704
Christopher Faulet8f82b202018-01-24 16:23:03 +01001705 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001706 if (SPOE_APPCTX(appctx)->node.key)
1707 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001708 active_s++;
1709 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001710
Christopher Faulet8f82b202018-01-24 16:23:03 +01001711 case 1: /* retry */
1712 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001713
Christopher Faulet8f82b202018-01-24 16:23:03 +01001714 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001715 if (SPOE_APPCTX(appctx)->node.key)
1716 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001717 active_s++;
1718 break;
1719 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001720 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001721
Christopher Faulet8f82b202018-01-24 16:23:03 +01001722 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001723 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1724 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1725 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001726
Christopher Faulet8f82b202018-01-24 16:23:03 +01001727 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001728 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001729 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001730 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001731 }
1732 return 1;
1733
Christopher Faulet8f82b202018-01-24 16:23:03 +01001734 next:
1735 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1736 return 0;
1737
Christopher Fauleta1cda022016-12-21 08:58:06 +01001738 exit:
1739 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1740 return 0;
1741}
1742
1743static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001744spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001745{
1746 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001747 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001748 char *frame, *buf;
1749 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001750
Christopher Fauleta1cda022016-12-21 08:58:06 +01001751 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1752 goto exit;
1753
1754 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1755 goto exit;
1756
Christopher Faulet8ef75252017-02-20 22:56:03 +01001757 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1758 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001759 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001760 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1761 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001762 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001763 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001764
1765 switch (ret) {
1766 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001767 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001768 goto exit;
1769
1770 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001771 goto stop;
1772
1773 default:
1774 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1775 " - disconnected by HAProxy (%d): %s\n",
1776 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001777 __FUNCTION__, appctx,
1778 SPOE_APPCTX(appctx)->status_code,
1779 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001780
1781 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1782 goto next;
1783 }
1784
1785 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001786 SPOE_APPCTX(appctx)->task->expire =
1787 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001788 return 0;
1789 stop:
1790 return 1;
1791 exit:
1792 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1793 return 0;
1794}
1795
1796static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001797spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001798{
1799 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001800 char *frame;
1801 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001802
Christopher Fauletb067b062017-01-04 16:39:11 +01001803 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001804 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001805 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001806 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001807
Christopher Fauletb067b062017-01-04 16:39:11 +01001808 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001809 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001810 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001811 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001812
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001813 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001814 ret = spoe_recv_frame(appctx, frame,
1815 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001816 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001817 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001818 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001819 }
1820
1821 switch (ret) {
1822 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001823 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1824 " - error on frame (%s)\n",
1825 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001826 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001827 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001828 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001829 goto exit;
1830
1831 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001832 goto next;
1833
1834 case 1: /* retry */
1835 goto stop;
1836
1837 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001838 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001839 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001840 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001841 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001842 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1843 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001844 goto exit;
1845 }
1846
1847 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001848 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001849 if (trash.data)
1850 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001851
Christopher Fauleta1cda022016-12-21 08:58:06 +01001852 return 0;
1853 stop:
1854 return 1;
1855 exit:
1856 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1857 return 0;
1858}
1859
1860/* I/O Handler processing messages exchanged with the agent */
1861static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001862spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001863{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001864 struct stream_interface *si = appctx->owner;
1865 struct spoe_agent *agent;
1866
1867 if (SPOE_APPCTX(appctx) == NULL)
1868 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001869
Christopher Faulet8ef75252017-02-20 22:56:03 +01001870 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1871 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001872
Christopher Fauleta1cda022016-12-21 08:58:06 +01001873 switchstate:
1874 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1875 " - appctx-state=%s\n",
1876 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1877 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1878
1879 switch (appctx->st0) {
1880 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001881 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001882 goto out;
1883 goto switchstate;
1884
1885 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001886 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001887 goto out;
1888 goto switchstate;
1889
1890 case SPOE_APPCTX_ST_IDLE:
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001891 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001892 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001893 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001894 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001895 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001896 SPOE_APPCTX(appctx)->task->expire =
1897 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001898 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001899 goto switchstate;
1900 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001901 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1902 /* fall through */
1903
1904 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001905 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1906 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001907 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001908 goto out;
1909 goto switchstate;
1910
1911 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001912 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001913 goto out;
1914 goto switchstate;
1915
1916 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001917 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001918 goto out;
1919 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001920
1921 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001922 appctx->st0 = SPOE_APPCTX_ST_END;
1923 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1924
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001925 si_shutw(si);
1926 si_shutr(si);
1927 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001928 /* fall through */
1929
1930 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001931 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001932 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001933 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001934 if (stopping)
1935 spoe_wakeup_appctx(appctx);
1936
Christopher Faulet42bfa462017-01-04 14:14:19 +01001937 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1938 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001939 si_oc(si)->flags |= CF_READ_DONTWAIT;
1940 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001941}
1942
1943struct applet spoe_applet = {
1944 .obj_type = OBJ_TYPE_APPLET,
1945 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001946 .fct = spoe_handle_appctx,
1947 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001948};
1949
1950/* Create a SPOE applet. On success, the created applet is returned, else
1951 * NULL. */
1952static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001953spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001954{
1955 struct appctx *appctx;
1956 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001957 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001958
Emeric Brun1138fd02017-06-19 12:38:55 +02001959 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001960 goto out_error;
1961
Willy Tarreaubafbe012017-11-24 17:34:44 +01001962 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001963 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001964 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001965 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001966
Christopher Faulet42bfa462017-01-04 14:14:19 +01001967 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001968 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001969 goto out_free_spoe_appctx;
1970
1971 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001972 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001973 SPOE_APPCTX(appctx)->task->context = appctx;
1974 SPOE_APPCTX(appctx)->agent = conf->agent;
1975 SPOE_APPCTX(appctx)->version = 0;
1976 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1977 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001978 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001979 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001980 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001981
1982 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1983 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001984 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001985
1986 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1987 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001988
Willy Tarreau5820a362016-12-22 15:59:02 +01001989 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001990 if (!sess)
1991 goto out_free_spoe;
1992
Willy Tarreau87787ac2017-08-28 16:22:54 +02001993 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001994 goto out_free_sess;
1995
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001996 stream_set_backend(strm, conf->agent->b.be);
1997
1998 /* applet is waiting for data */
1999 si_applet_cant_get(&strm->si[0]);
2000 appctx_wakeup(appctx);
2001
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002002 strm->do_log = NULL;
2003 strm->res.flags |= CF_READ_DONTWAIT;
2004
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002005 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002006 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002007 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002008 HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002009
Emeric Brunc60def82017-09-27 14:59:38 +02002010 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002011 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002012 return appctx;
2013
2014 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002015 out_free_sess:
2016 session_free(sess);
2017 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01002018 task_free(SPOE_APPCTX(appctx)->task);
2019 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002020 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002021 out_free_appctx:
2022 appctx_free(appctx);
2023 out_error:
2024 return NULL;
2025}
2026
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002027static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002028spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002029{
2030 struct spoe_config *conf = FLT_CONF(ctx->filter);
2031 struct spoe_agent *agent = conf->agent;
2032 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002033 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002034
Christopher Fauleta1cda022016-12-21 08:58:06 +01002035 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002036 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002037 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002038 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002039
2040 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002041 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002042 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2043 ctx->strm);
2044
Christopher Fauleta1cda022016-12-21 08:58:06 +01002045 /* Do not try to create a new applet if there is no server up for the
2046 * agent's backend. */
2047 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2048 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2049 " - cannot create SPOE appctx: no server up\n",
2050 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2051 __FUNCTION__, ctx->strm);
2052 goto end;
2053 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002054
Christopher Fauleta1cda022016-12-21 08:58:06 +01002055 /* Do not try to create a new applet if we have reached the maximum of
2056 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002057 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002058 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002059 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2060 " - cannot create SPOE appctx: max CPS reached\n",
2061 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2062 __FUNCTION__, ctx->strm);
2063 goto end;
2064 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002065 }
2066
Christopher Faulet8ef75252017-02-20 22:56:03 +01002067 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002068 if (appctx == NULL) {
2069 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2070 " - failed to create SPOE appctx\n",
2071 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2072 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002073 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002074 "SPOE: [%s] failed to create SPOE applet\n",
2075 agent->id);
2076
Christopher Fauleta1cda022016-12-21 08:58:06 +01002077 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002078 }
2079
Christopher Fauleta1cda022016-12-21 08:58:06 +01002080 /* Increase the per-process number of cumulated connections */
2081 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002082 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002083
Christopher Fauleta1cda022016-12-21 08:58:06 +01002084 end:
2085 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002086 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002087 ctx->status_code = SPOE_CTX_ERR_RES;
2088 return -1;
2089 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002090
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002091 /* Add the SPOE context in the sending queue */
Christopher Faulet24289f22017-09-25 14:48:02 +02002092 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002093 HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002094 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2095 ctx->stats.tv_queue = now;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002096
2097 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002098 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002099 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002100 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002101 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002102 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002103
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002104 /* Finally try to wakeup an IDLE applet. */
2105 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2106 struct eb32_node *node;
2107
2108 node = eb32_first(&agent->rt[tid].idle_applets);
2109 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2110 if (node && spoe_appctx) {
2111 eb32_delete(&spoe_appctx->node);
2112 spoe_appctx->node.key++;
2113 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2114 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002115 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002116 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002117 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002118}
2119
2120/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002121 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002122 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002123/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2124 * fragmented_content. If the next message can be processed, it returns 0. If
2125 * the message is too big, it returns -1.*/
2126static int
2127spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2128 struct spoe_message *msg, int dir,
2129 char **buf, char *end)
2130{
2131 struct sample *smp;
2132 struct spoe_arg *arg;
2133 int ret;
2134
2135 if (msg->cond) {
2136 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2137 ret = acl_pass(ret);
2138 if (msg->cond->pol == ACL_COND_UNLESS)
2139 ret = !ret;
2140
2141 /* the rule does not match */
2142 if (!ret)
2143 goto next;
2144 }
2145
2146 /* Resume encoding of a SPOE argument */
2147 if (ctx->frag_ctx.curarg != NULL) {
2148 arg = ctx->frag_ctx.curarg;
2149 goto encode_argument;
2150 }
2151
2152 if (ctx->frag_ctx.curoff != UINT_MAX)
2153 goto encode_msg_payload;
2154
2155 /* Check if there is enough space for the message name and the
2156 * number of arguments. It implies <msg->id_len> is encoded on 2
2157 * bytes, at most (< 2288). */
2158 if (*buf + 2 + msg->id_len + 1 > end)
2159 goto too_big;
2160
2161 /* Encode the message name */
2162 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2163 goto too_big;
2164
2165 /* Set the number of arguments for this message */
2166 **buf = msg->nargs;
2167 (*buf)++;
2168
2169 ctx->frag_ctx.curoff = 0;
2170 encode_msg_payload:
2171
2172 /* Loop on arguments */
2173 list_for_each_entry(arg, &msg->args, list) {
2174 ctx->frag_ctx.curarg = arg;
2175 ctx->frag_ctx.curoff = UINT_MAX;
2176
2177 encode_argument:
2178 if (ctx->frag_ctx.curoff != UINT_MAX)
2179 goto encode_arg_value;
2180
2181 /* Encode the arguement name as a string. It can by NULL */
2182 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2183 goto too_big;
2184
2185 ctx->frag_ctx.curoff = 0;
2186 encode_arg_value:
2187
2188 /* Fetch the arguement value */
2189 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2190 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2191 if (ret == -1 || ctx->frag_ctx.curoff)
2192 goto too_big;
2193 }
2194
2195 next:
2196 return 0;
2197
2198 too_big:
2199 return -1;
2200}
2201
Christopher Fauletc718b822017-09-21 16:50:56 +02002202/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2203 * handle fragmented content. On success it returns 1. If an error occurred, -1
2204 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2205 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002206static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002207spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002208 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002209{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002210 struct spoe_config *conf = FLT_CONF(ctx->filter);
2211 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002212 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002213 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002214
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002215 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002216 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002217
Christopher Fauletc718b822017-09-21 16:50:56 +02002218 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2219 /* Resume encoding of a SPOE message */
2220 if (ctx->frag_ctx.curmsg != NULL) {
2221 msg = ctx->frag_ctx.curmsg;
2222 goto encode_evt_message;
2223 }
2224
2225 list_for_each_entry(msg, messages, by_evt) {
2226 ctx->frag_ctx.curmsg = msg;
2227 ctx->frag_ctx.curarg = NULL;
2228 ctx->frag_ctx.curoff = UINT_MAX;
2229
2230 encode_evt_message:
2231 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2232 goto too_big;
2233 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002234 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002235 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2236 /* Resume encoding of a SPOE message */
2237 if (ctx->frag_ctx.curmsg != NULL) {
2238 msg = ctx->frag_ctx.curmsg;
2239 goto encode_grp_message;
2240 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002241
Christopher Fauletc718b822017-09-21 16:50:56 +02002242 list_for_each_entry(msg, messages, by_grp) {
2243 ctx->frag_ctx.curmsg = msg;
2244 ctx->frag_ctx.curarg = NULL;
2245 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002246
Christopher Fauletc718b822017-09-21 16:50:56 +02002247 encode_grp_message:
2248 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2249 goto too_big;
2250 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002251 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002252 else
2253 goto skip;
2254
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002255
Christopher Faulet57583e42017-09-04 15:41:09 +02002256 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002257 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002258 goto skip;
2259
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002260 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002261 " - encode %s messages - spoe_appctx=%p"
2262 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002263 (int)now.tv_sec, (int)now.tv_usec,
2264 agent->id, __FUNCTION__, s,
2265 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002266 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002267 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002268
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002269 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002270 ctx->frag_ctx.curmsg = NULL;
2271 ctx->frag_ctx.curarg = NULL;
2272 ctx->frag_ctx.curoff = 0;
2273 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002274
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002275 return 1;
2276
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002277 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002278 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2279 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2280 return -1;
2281 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002282
2283 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002284 " - encode fragmented messages - spoe_appctx=%p"
2285 " - curmsg=%p - curarg=%p - curoff=%u"
2286 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002287 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002288 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002289 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002290 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002291
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002292 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002293 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2294 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2295 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002296
2297 skip:
2298 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2299 " - skip the frame because nothing has been encoded\n",
2300 (int)now.tv_sec, (int)now.tv_usec,
2301 agent->id, __FUNCTION__, s);
2302 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002303}
2304
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002305
2306/***************************************************************************
2307 * Functions that handle SPOE actions
2308 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002309/* Helper function to set a variable */
2310static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002311spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002312 struct sample *smp)
2313{
2314 struct spoe_config *conf = FLT_CONF(ctx->filter);
2315 struct spoe_agent *agent = conf->agent;
2316 char varname[64];
2317
2318 memset(varname, 0, sizeof(varname));
2319 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2320 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002321 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2322 vars_set_by_name(varname, len, smp);
2323 else
2324 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002325}
2326
2327/* Helper function to unset a variable */
2328static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002329spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002330 struct sample *smp)
2331{
2332 struct spoe_config *conf = FLT_CONF(ctx->filter);
2333 struct spoe_agent *agent = conf->agent;
2334 char varname[64];
2335
2336 memset(varname, 0, sizeof(varname));
2337 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2338 scope, agent->var_pfx, len, name);
2339 vars_unset_by_name_ifexist(varname, len, smp);
2340}
2341
2342
Christopher Faulet8ef75252017-02-20 22:56:03 +01002343static inline int
2344spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2345 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002346{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002347 char *str, *scope, *p = *buf;
2348 struct sample smp;
2349 uint64_t sz;
2350 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002351
Christopher Faulet8ef75252017-02-20 22:56:03 +01002352 if (p + 2 >= end)
2353 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002354
Christopher Faulet8ef75252017-02-20 22:56:03 +01002355 /* SET-VAR requires 3 arguments */
2356 if (*p++ != 3)
2357 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002358
Christopher Faulet8ef75252017-02-20 22:56:03 +01002359 switch (*p++) {
2360 case SPOE_SCOPE_PROC: scope = "proc"; break;
2361 case SPOE_SCOPE_SESS: scope = "sess"; break;
2362 case SPOE_SCOPE_TXN : scope = "txn"; break;
2363 case SPOE_SCOPE_REQ : scope = "req"; break;
2364 case SPOE_SCOPE_RES : scope = "res"; break;
2365 default: goto skip;
2366 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002367
Christopher Faulet8ef75252017-02-20 22:56:03 +01002368 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2369 goto skip;
2370 memset(&smp, 0, sizeof(smp));
2371 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002372
Christopher Faulet8ef75252017-02-20 22:56:03 +01002373 if (spoe_decode_data(&p, end, &smp) == -1)
2374 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002375
Christopher Faulet8ef75252017-02-20 22:56:03 +01002376 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2377 " - set-var '%s.%s.%.*s'\n",
2378 (int)now.tv_sec, (int)now.tv_usec,
2379 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2380 __FUNCTION__, s, scope,
2381 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2382 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002383
Christopher Faulet8ef75252017-02-20 22:56:03 +01002384 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002385
Christopher Faulet8ef75252017-02-20 22:56:03 +01002386 ret = (p - *buf);
2387 *buf = p;
2388 return ret;
2389 skip:
2390 return 0;
2391}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002392
Christopher Faulet8ef75252017-02-20 22:56:03 +01002393static inline int
2394spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2395 char **buf, char *end, int dir)
2396{
2397 char *str, *scope, *p = *buf;
2398 struct sample smp;
2399 uint64_t sz;
2400 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002401
Christopher Faulet8ef75252017-02-20 22:56:03 +01002402 if (p + 2 >= end)
2403 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002404
Christopher Faulet8ef75252017-02-20 22:56:03 +01002405 /* UNSET-VAR requires 2 arguments */
2406 if (*p++ != 2)
2407 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002408
Christopher Faulet8ef75252017-02-20 22:56:03 +01002409 switch (*p++) {
2410 case SPOE_SCOPE_PROC: scope = "proc"; break;
2411 case SPOE_SCOPE_SESS: scope = "sess"; break;
2412 case SPOE_SCOPE_TXN : scope = "txn"; break;
2413 case SPOE_SCOPE_REQ : scope = "req"; break;
2414 case SPOE_SCOPE_RES : scope = "res"; break;
2415 default: goto skip;
2416 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002417
Christopher Faulet8ef75252017-02-20 22:56:03 +01002418 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2419 goto skip;
2420 memset(&smp, 0, sizeof(smp));
2421 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002422
Christopher Faulet8ef75252017-02-20 22:56:03 +01002423 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2424 " - unset-var '%s.%s.%.*s'\n",
2425 (int)now.tv_sec, (int)now.tv_usec,
2426 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2427 __FUNCTION__, s, scope,
2428 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2429 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002430
Christopher Faulet8ef75252017-02-20 22:56:03 +01002431 spoe_unset_var(ctx, scope, str, sz, &smp);
2432
2433 ret = (p - *buf);
2434 *buf = p;
2435 return ret;
2436 skip:
2437 return 0;
2438}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002439
Christopher Faulet8ef75252017-02-20 22:56:03 +01002440/* Process SPOE actions for a specific event. It returns 1 on success. If an
2441 * error occurred, 0 is returned. */
2442static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002443spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002444{
2445 char *p, *end;
2446 int ret;
2447
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002448 p = b_head(&ctx->buffer);
2449 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002450
2451 while (p < end) {
2452 enum spoe_action_type type;
2453
2454 type = *p++;
2455 switch (type) {
2456 case SPOE_ACT_T_SET_VAR:
2457 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2458 if (!ret)
2459 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002460 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002461
Christopher Faulet8ef75252017-02-20 22:56:03 +01002462 case SPOE_ACT_T_UNSET_VAR:
2463 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2464 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002465 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002466 break;
2467
2468 default:
2469 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002470 }
2471 }
2472
2473 return 1;
2474 skip:
2475 return 0;
2476}
2477
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002478/***************************************************************************
2479 * Functions that process SPOE events
2480 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002481static void
2482spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2483 struct spoe_context *ctx, int dir)
2484{
2485 if (!tv_iszero(&ctx->stats.tv_start)) {
2486 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2487 ctx->stats.t_total += ctx->stats.t_process;
2488 tv_zero(&ctx->stats.tv_request);
2489 tv_zero(&ctx->stats.tv_queue);
2490 tv_zero(&ctx->stats.tv_wait);
2491 tv_zero(&ctx->stats.tv_response);
2492 }
2493
2494 if (agent->var_t_process) {
2495 struct sample smp;
2496
2497 memset(&smp, 0, sizeof(smp));
2498 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2499 smp.data.u.sint = ctx->stats.t_process;
2500 smp.data.type = SMP_T_SINT;
2501
2502 spoe_set_var(ctx, "txn", agent->var_t_process,
2503 strlen(agent->var_t_process), &smp);
2504 }
2505
2506 if (agent->var_t_total) {
2507 struct sample smp;
2508
2509 memset(&smp, 0, sizeof(smp));
2510 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2511 smp.data.u.sint = ctx->stats.t_total;
2512 smp.data.type = SMP_T_SINT;
2513
2514 spoe_set_var(ctx, "txn", agent->var_t_total,
2515 strlen(agent->var_t_total), &smp);
2516 }
2517}
2518
2519static void
2520spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2521 struct spoe_context *ctx, int dir)
2522{
2523 if (agent->eps_max > 0)
2524 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2525
2526 if (agent->var_on_error) {
2527 struct sample smp;
2528
2529 memset(&smp, 0, sizeof(smp));
2530 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2531 smp.data.u.sint = ctx->status_code;
2532 smp.data.type = SMP_T_BOOL;
2533
2534 spoe_set_var(ctx, "txn", agent->var_on_error,
2535 strlen(agent->var_on_error), &smp);
2536 }
2537
2538 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2539 ? SPOE_CTX_ST_READY
2540 : SPOE_CTX_ST_NONE);
2541}
2542
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002543static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002544spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002545{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002546 /* If a process is already started for this SPOE context, retry
2547 * later. */
2548 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002549 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002550
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002551 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002552 ctx->stats.tv_start = now;
2553 ctx->stats.tv_request = now;
2554 ctx->stats.t_request = -1;
2555 ctx->stats.t_queue = -1;
2556 ctx->stats.t_waiting = -1;
2557 ctx->stats.t_response = -1;
2558 ctx->stats.t_process = -1;
2559
2560 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002561
Christopher Fauleta1cda022016-12-21 08:58:06 +01002562 /* Set the right flag to prevent request and response processing
2563 * in same time. */
2564 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2565 ? SPOE_CTX_FL_REQ_PROCESS
2566 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002567 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002568}
2569
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002570static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002571spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002572{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002573 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002574
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002575 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2576 return;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002577 HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002578 if (sa) {
2579 if (sa->frag_ctx.ctx == ctx) {
2580 sa->frag_ctx.ctx = NULL;
2581 spoe_wakeup_appctx(sa->owner);
2582 }
2583 else
2584 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002585 }
2586
Christopher Fauleta1cda022016-12-21 08:58:06 +01002587 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002588 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002589 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002590
2591 /* Reset processing timer */
2592 ctx->process_exp = TICK_ETERNITY;
2593
Christopher Faulet8ef75252017-02-20 22:56:03 +01002594 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002595
Christopher Fauletfce747b2018-01-24 15:59:32 +01002596 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002597 ctx->frag_ctx.curmsg = NULL;
2598 ctx->frag_ctx.curarg = NULL;
2599 ctx->frag_ctx.curoff = 0;
2600 ctx->frag_ctx.flags = 0;
2601
Christopher Fauleta1cda022016-12-21 08:58:06 +01002602 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002603 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Christopher Fauletebe13992018-04-26 11:33:44 +02002604 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002605 else
2606 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
2607
Christopher Fauleta1cda022016-12-21 08:58:06 +01002608 LIST_DEL(&ctx->list);
2609 LIST_INIT(&ctx->list);
2610 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002611}
2612
Christopher Faulet58d03682017-09-21 16:57:24 +02002613/* Process a list of SPOE messages. First, this functions will process messages
2614 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2615 * to process corresponding actions. During all the processing, it returns 0
2616 * and it returns 1 when the processing is finished. If an error occurred, -1
2617 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002618static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002619spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2620 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002621{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002622 struct spoe_config *conf = FLT_CONF(ctx->filter);
2623 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002624 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002625
2626 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002627 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002628
2629 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2630 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002631 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002632 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002633 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002634 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002635 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002636 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002637
2638 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002639 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002640 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002641 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002642 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002643 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002644 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002645 goto skip;
2646 }
2647 }
2648
Christopher Fauletf7a30922016-11-10 15:04:51 +01002649 if (!tick_isset(ctx->process_exp)) {
2650 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2651 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2652 ctx->process_exp);
2653 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002654 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002655 if (!ret)
2656 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002657
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002658 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002659 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002660 }
2661
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002662 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002663 if (!tv_iszero(&ctx->stats.tv_request))
2664 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002665 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002666 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002667 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002668 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002669 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002670 if (!ret)
2671 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002672 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002673 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002674 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2675 }
2676
2677 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002678 if (ctx->spoe_appctx)
2679 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002680 ret = 0;
2681 goto out;
2682 }
2683
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002684 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2685 ret = 0;
2686 goto out;
2687 }
2688
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002689 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002690 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002691 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002692 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002693 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002694 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002695 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002696 }
2697
2698 out:
2699 return ret;
2700
Christopher Fauleta1cda022016-12-21 08:58:06 +01002701 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002702 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002703 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002704 spoe_stop_processing(agent, ctx);
2705 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002706
Christopher Fauleta1cda022016-12-21 08:58:06 +01002707 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002708 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002709 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002710 if (ctx->status_code) {
2711 HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
2712 spoe_handle_processing_error(s, agent, ctx, dir);
2713 ret = 1;
2714 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002715 return ret;
2716}
2717
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002718/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2719 * See spoe_process_message for details. */
2720static int
2721spoe_process_group(struct stream *s, struct spoe_context *ctx,
2722 struct spoe_group *group, int dir)
2723{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002724 struct spoe_config *conf = FLT_CONF(ctx->filter);
2725 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002726 int ret;
2727
2728 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2729 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002730 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002731 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2732 group->id);
2733
2734 if (LIST_ISEMPTY(&group->messages))
2735 return 1;
2736
2737 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002738 if (ret && ctx->stats.t_process != -1) {
2739 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002740 " - <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 +01002741 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002742 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002743 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002744 ctx->stats.t_response, ctx->stats.t_process,
2745 agent->counters.idles, agent->counters.applets,
2746 agent->counters.nb_sending, agent->counters.nb_waiting,
2747 agent->counters.nb_errors, agent->counters.nb_processed,
2748 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2749 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2750 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2751 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2752 agent->id, group->id, s->uniq_id, ctx->status_code,
2753 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2754 ctx->stats.t_response, ctx->stats.t_process,
2755 agent->counters.idles, agent->counters.applets,
2756 agent->counters.nb_sending, agent->counters.nb_waiting,
2757 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002758 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002759 return ret;
2760}
2761
Christopher Faulet58d03682017-09-21 16:57:24 +02002762/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2763 * See spoe_process_message for details. */
2764static int
2765spoe_process_event(struct stream *s, struct spoe_context *ctx,
2766 enum spoe_event ev)
2767{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002768 struct spoe_config *conf = FLT_CONF(ctx->filter);
2769 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002770 int dir, ret;
2771
2772 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002773 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002774 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002775 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2776 spoe_event_str[ev]);
2777
2778 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2779
2780 if (LIST_ISEMPTY(&(ctx->events[ev])))
2781 return 1;
2782
2783 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002784 if (ret && ctx->stats.t_process != -1) {
2785 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002786 " - <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 +01002787 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2788 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2789 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002790 ctx->stats.t_response, ctx->stats.t_process,
2791 agent->counters.idles, agent->counters.applets,
2792 agent->counters.nb_sending, agent->counters.nb_waiting,
2793 agent->counters.nb_errors, agent->counters.nb_processed,
2794 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2795 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2796 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2797 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2798 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2799 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2800 ctx->stats.t_response, ctx->stats.t_process,
2801 agent->counters.idles, agent->counters.applets,
2802 agent->counters.nb_sending, agent->counters.nb_waiting,
2803 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002804 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002805 return ret;
2806}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002807
2808/***************************************************************************
2809 * Functions that create/destroy SPOE contexts
2810 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002811static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002812spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002813{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002814 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002815 return 1;
2816
Christopher Faulet4596fb72017-01-11 14:05:19 +01002817 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002818 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002819 LIST_DEL(&buffer_wait->list);
2820 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002821 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002822 }
2823
Christopher Faulet4596fb72017-01-11 14:05:19 +01002824 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002825 return 1;
2826
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002827 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002828 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002829 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002830 return 0;
2831}
2832
2833static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002834spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002835{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002836 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002837 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002838 LIST_DEL(&buffer_wait->list);
2839 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002840 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002841 }
2842
2843 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002844 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002845 b_free(buf);
Olivier Houchard673867c2018-05-25 16:58:52 +02002846 offer_buffers(buffer_wait->target, tasks_run_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002847 }
2848}
2849
Christopher Faulet4596fb72017-01-11 14:05:19 +01002850static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002851spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002852{
2853 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2854 return 1;
2855}
2856
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002857static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002858spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002859{
2860 struct spoe_config *conf = FLT_CONF(filter);
2861 struct spoe_context *ctx;
2862
Willy Tarreaubafbe012017-11-24 17:34:44 +01002863 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002864 if (ctx == NULL) {
2865 return NULL;
2866 }
2867 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002868 ctx->filter = filter;
2869 ctx->state = SPOE_CTX_ST_NONE;
2870 ctx->status_code = SPOE_CTX_ERR_NONE;
2871 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002872 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002873 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002874 ctx->buffer = BUF_NULL;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002875 LIST_INIT(&ctx->buffer_wait.list);
2876 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002877 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002878 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002879
Christopher Fauletf7a30922016-11-10 15:04:51 +01002880 ctx->stream_id = 0;
2881 ctx->frame_id = 1;
2882 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002883
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002884 tv_zero(&ctx->stats.tv_start);
2885 tv_zero(&ctx->stats.tv_request);
2886 tv_zero(&ctx->stats.tv_queue);
2887 tv_zero(&ctx->stats.tv_wait);
2888 tv_zero(&ctx->stats.tv_response);
2889 ctx->stats.t_request = -1;
2890 ctx->stats.t_queue = -1;
2891 ctx->stats.t_waiting = -1;
2892 ctx->stats.t_response = -1;
2893 ctx->stats.t_process = -1;
2894 ctx->stats.t_total = 0;
2895
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002896 ctx->strm = s;
2897 ctx->state = SPOE_CTX_ST_READY;
2898 filter->ctx = ctx;
2899
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002900 return ctx;
2901}
2902
2903static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002904spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002905{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002906 struct spoe_config *conf = FLT_CONF(filter);
2907 struct spoe_context *ctx = filter->ctx;
2908
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002909 if (!ctx)
2910 return;
2911
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002912 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002913 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002914 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002915}
2916
2917static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002918spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002919{
2920 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002921 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002922
2923 tv_zero(&ctx->stats.tv_start);
2924 tv_zero(&ctx->stats.tv_request);
2925 tv_zero(&ctx->stats.tv_queue);
2926 tv_zero(&ctx->stats.tv_wait);
2927 tv_zero(&ctx->stats.tv_response);
2928 ctx->stats.t_request = -1;
2929 ctx->stats.t_queue = -1;
2930 ctx->stats.t_waiting = -1;
2931 ctx->stats.t_response = -1;
2932 ctx->stats.t_process = -1;
2933 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002934}
2935
2936
2937/***************************************************************************
2938 * Hooks that manage the filter lifecycle (init/check/deinit)
2939 **************************************************************************/
2940/* Signal handler: Do a soft stop, wakeup SPOE applet */
2941static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002942spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002943{
2944 struct proxy *p;
2945
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002946 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002947 while (p) {
2948 struct flt_conf *fconf;
2949
2950 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002951 struct spoe_config *conf;
2952 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002953 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002954 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002955
Christopher Faulet3b386a32017-02-23 10:17:15 +01002956 if (fconf->id != spoe_filter_id)
2957 continue;
2958
2959 conf = fconf->conf;
2960 agent = conf->agent;
2961
Christopher Faulet24289f22017-09-25 14:48:02 +02002962 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002963 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002964 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2965 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002966 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002967 }
2968 }
2969 p = p->next;
2970 }
2971}
2972
2973
2974/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2975static int
2976spoe_init(struct proxy *px, struct flt_conf *fconf)
2977{
2978 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002979
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002980 /* conf->agent_fe was already initialized during the config
2981 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002982 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002983 conf->agent_fe.cap = PR_CAP_FE;
2984 conf->agent_fe.mode = PR_MODE_TCP;
2985 conf->agent_fe.maxconn = 0;
2986 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2987 conf->agent_fe.conn_retries = CONN_RETRIES;
2988 conf->agent_fe.accept = frontend_accept;
2989 conf->agent_fe.srv = NULL;
2990 conf->agent_fe.timeout.client = TICK_ETERNITY;
2991 conf->agent_fe.default_target = &spoe_applet.obj_type;
2992 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2993
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002994 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002995 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002996 sighandler_registered = 1;
2997 }
2998
2999 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003000}
3001
3002/* Free ressources allocated by the SPOE filter. */
3003static void
3004spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3005{
3006 struct spoe_config *conf = fconf->conf;
3007
3008 if (conf) {
3009 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003010
Christopher Faulet8ef75252017-02-20 22:56:03 +01003011 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003012 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003013 free(conf);
3014 }
3015 fconf->conf = NULL;
3016}
3017
3018/* Check configuration of a SPOE filter for a specified proxy.
3019 * Return 1 on error, else 0. */
3020static int
3021spoe_check(struct proxy *px, struct flt_conf *fconf)
3022{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003023 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003024 struct spoe_config *conf = fconf->conf;
3025 struct proxy *target;
3026
Christopher Faulet7ee86672017-09-19 11:08:28 +02003027 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3028 * are uniq */
3029 list_for_each_entry(f, &px->filter_configs, list) {
3030 struct spoe_config *c = f->conf;
3031
3032 /* This is not an SPOE filter */
3033 if (f->id != spoe_filter_id)
3034 continue;
3035 /* This is the current SPOE filter */
3036 if (f == fconf)
3037 continue;
3038
3039 /* Check engine Id. It should be uniq */
3040 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003041 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3042 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003043 return 1;
3044 }
3045 }
3046
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003047 target = proxy_be_by_name(conf->agent->b.name);
3048 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003049 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3050 " declared at %s:%d.\n",
3051 px->id, conf->agent->b.name, conf->agent->id,
3052 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003053 return 1;
3054 }
3055 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003056 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3057 " at %s:%d does not support HTTP mode.\n",
3058 px->id, target->id, conf->agent->id,
3059 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003060 return 1;
3061 }
3062
3063 free(conf->agent->b.name);
3064 conf->agent->b.name = NULL;
3065 conf->agent->b.be = target;
3066 return 0;
3067}
3068
3069/**************************************************************************
3070 * Hooks attached to a stream
3071 *************************************************************************/
3072/* Called when a filter instance is created and attach to a stream. It creates
3073 * the context that will be used to process this stream. */
3074static int
3075spoe_start(struct stream *s, struct filter *filter)
3076{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003077 struct spoe_config *conf = FLT_CONF(filter);
3078 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003079 struct spoe_context *ctx;
3080
3081 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003082 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003083 __FUNCTION__, s);
3084
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003085 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003086 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3087 " - failed to create SPOE context\n",
3088 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003089 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003090 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003091 "SPOE: [%s] failed to create SPOE context\n",
3092 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003093 return 0;
3094 }
3095
Christopher Faulet11610f32017-09-21 10:23:10 +02003096 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003097 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3098
Christopher Faulet11610f32017-09-21 10:23:10 +02003099 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003100 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3101
Christopher Faulet11610f32017-09-21 10:23:10 +02003102 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003103 filter->pre_analyzers |= AN_RES_INSPECT;
3104
Christopher Faulet11610f32017-09-21 10:23:10 +02003105 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003106 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3107
Christopher Faulet11610f32017-09-21 10:23:10 +02003108 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003109 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3110
Christopher Faulet11610f32017-09-21 10:23:10 +02003111 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003112 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3113
3114 return 1;
3115}
3116
3117/* Called when a filter instance is detached from a stream. It release the
3118 * attached SPOE context. */
3119static void
3120spoe_stop(struct stream *s, struct filter *filter)
3121{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003122 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3123 (int)now.tv_sec, (int)now.tv_usec,
3124 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3125 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003126 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003127}
3128
Christopher Fauletf7a30922016-11-10 15:04:51 +01003129
3130/*
3131 * Called when the stream is woken up because of expired timer.
3132 */
3133static void
3134spoe_check_timeouts(struct stream *s, struct filter *filter)
3135{
3136 struct spoe_context *ctx = filter->ctx;
3137
Christopher Fauletac580602018-03-20 16:09:20 +01003138 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003139 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003140}
3141
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003142/* Called when we are ready to filter data on a channel */
3143static int
3144spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3145{
3146 struct spoe_context *ctx = filter->ctx;
3147 int ret = 1;
3148
3149 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3150 " - ctx-flags=0x%08x\n",
3151 (int)now.tv_sec, (int)now.tv_usec,
3152 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3153 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3154
Christopher Fauletb067b062017-01-04 16:39:11 +01003155 if (ctx->state == SPOE_CTX_ST_NONE)
3156 goto out;
3157
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003158 if (!(chn->flags & CF_ISRESP)) {
3159 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3160 chn->analysers |= AN_REQ_INSPECT_FE;
3161 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3162 chn->analysers |= AN_REQ_INSPECT_BE;
3163
3164 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3165 goto out;
3166
3167 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003168 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003169 if (!ret)
3170 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003171 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3172 }
3173 else {
3174 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3175 chn->analysers |= AN_RES_INSPECT;
3176
3177 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3178 goto out;
3179
Christopher Faulet8ef75252017-02-20 22:56:03 +01003180 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003181 if (!ret) {
3182 channel_dont_read(chn);
3183 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003184 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003185 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003186 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003187 }
3188
3189 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003190 return ret;
3191}
3192
3193/* Called before a processing happens on a given channel */
3194static int
3195spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3196 struct channel *chn, unsigned an_bit)
3197{
3198 struct spoe_context *ctx = filter->ctx;
3199 int ret = 1;
3200
3201 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3202 " - ctx-flags=0x%08x - ana=0x%08x\n",
3203 (int)now.tv_sec, (int)now.tv_usec,
3204 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3205 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3206 ctx->flags, an_bit);
3207
Christopher Fauletb067b062017-01-04 16:39:11 +01003208 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003209 goto out;
3210
3211 switch (an_bit) {
3212 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003213 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003214 break;
3215 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003216 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003217 break;
3218 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003219 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003220 break;
3221 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003222 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003223 break;
3224 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003225 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003226 break;
3227 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003228 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003229 break;
3230 }
3231
3232 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003233 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003234 channel_dont_read(chn);
3235 channel_dont_close(chn);
3236 }
3237 return ret;
3238}
3239
3240/* Called when the filtering on the channel ends. */
3241static int
3242spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3243{
3244 struct spoe_context *ctx = filter->ctx;
3245
3246 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3247 " - ctx-flags=0x%08x\n",
3248 (int)now.tv_sec, (int)now.tv_usec,
3249 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3250 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3251
3252 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003253 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003254 }
3255
3256 return 1;
3257}
3258
3259/********************************************************************
3260 * Functions that manage the filter initialization
3261 ********************************************************************/
3262struct flt_ops spoe_ops = {
3263 /* Manage SPOE filter, called for each filter declaration */
3264 .init = spoe_init,
3265 .deinit = spoe_deinit,
3266 .check = spoe_check,
3267
3268 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003269 .attach = spoe_start,
3270 .detach = spoe_stop,
3271 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003272
3273 /* Handle channels activity */
3274 .channel_start_analyze = spoe_start_analyze,
3275 .channel_pre_analyze = spoe_chn_pre_analyze,
3276 .channel_end_analyze = spoe_end_analyze,
3277};
3278
3279
3280static int
3281cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3282{
3283 const char *err;
3284 int i, err_code = 0;
3285
3286 if ((cfg_scope == NULL && curengine != NULL) ||
3287 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003288 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003289 goto out;
3290
3291 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3292 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003293 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3294 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003295 err_code |= ERR_ALERT | ERR_ABORT;
3296 goto out;
3297 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003298 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3299 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003300 goto out;
3301 }
3302
3303 err = invalid_char(args[1]);
3304 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003305 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3306 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003307 err_code |= ERR_ALERT | ERR_ABORT;
3308 goto out;
3309 }
3310
3311 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003312 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3313 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003314 err_code |= ERR_ALERT | ERR_ABORT;
3315 goto out;
3316 }
3317 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003318 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003319 err_code |= ERR_ALERT | ERR_ABORT;
3320 goto out;
3321 }
3322
3323 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003324
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003325 curagent->conf.file = strdup(file);
3326 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003327
3328 curagent->timeout.hello = TICK_ETERNITY;
3329 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003330 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003331
3332 curagent->engine_id = NULL;
3333 curagent->var_pfx = NULL;
3334 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003335 curagent->var_t_process = NULL;
3336 curagent->var_t_total = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003337 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3338 if (global.nbthread == 1)
3339 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003340 curagent->cps_max = 0;
3341 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003342 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003343 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003344
3345 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003346 LIST_INIT(&curagent->events[i]);
3347 LIST_INIT(&curagent->groups);
3348 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003349
Christopher Faulet24289f22017-09-25 14:48:02 +02003350 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003351 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003352 err_code |= ERR_ALERT | ERR_ABORT;
3353 goto out;
3354 }
3355 for (i = 0; i < global.nbthread; ++i) {
3356 curagent->rt[i].frame_size = curagent->max_frame_size;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003357 curagent->rt[i].processing = 0;
Christopher Faulet24289f22017-09-25 14:48:02 +02003358 LIST_INIT(&curagent->rt[i].applets);
3359 LIST_INIT(&curagent->rt[i].sending_queue);
3360 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003361 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003362 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003363 }
3364 else if (!strcmp(args[0], "use-backend")) {
3365 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003366 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3367 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003368 err_code |= ERR_ALERT | ERR_FATAL;
3369 goto out;
3370 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003371 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003372 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003373 free(curagent->b.name);
3374 curagent->b.name = strdup(args[1]);
3375 }
3376 else if (!strcmp(args[0], "messages")) {
3377 int cur_arg = 1;
3378 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003379 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003380
Christopher Faulet11610f32017-09-21 10:23:10 +02003381 list_for_each_entry(ph, &curmphs, list) {
3382 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003383 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3384 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003385 err_code |= ERR_ALERT | ERR_FATAL;
3386 goto out;
3387 }
3388 }
3389
Christopher Faulet11610f32017-09-21 10:23:10 +02003390 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003391 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003392 err_code |= ERR_ALERT | ERR_ABORT;
3393 goto out;
3394 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003395 ph->id = strdup(args[cur_arg]);
3396 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003397 cur_arg++;
3398 }
3399 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003400 else if (!strcmp(args[0], "groups")) {
3401 int cur_arg = 1;
3402 while (*args[cur_arg]) {
3403 struct spoe_placeholder *ph = NULL;
3404
3405 list_for_each_entry(ph, &curgphs, list) {
3406 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003407 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3408 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003409 err_code |= ERR_ALERT | ERR_FATAL;
3410 goto out;
3411 }
3412 }
3413
3414 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003415 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003416 err_code |= ERR_ALERT | ERR_ABORT;
3417 goto out;
3418 }
3419 ph->id = strdup(args[cur_arg]);
3420 LIST_ADDQ(&curgphs, &ph->list);
3421 cur_arg++;
3422 }
3423 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003424 else if (!strcmp(args[0], "timeout")) {
3425 unsigned int *tv = NULL;
3426 const char *res;
3427 unsigned timeout;
3428
3429 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003430 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3431 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003432 err_code |= ERR_ALERT | ERR_FATAL;
3433 goto out;
3434 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003435 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3436 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003437 if (!strcmp(args[1], "hello"))
3438 tv = &curagent->timeout.hello;
3439 else if (!strcmp(args[1], "idle"))
3440 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003441 else if (!strcmp(args[1], "processing"))
3442 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003443 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003444 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3445 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003446 err_code |= ERR_ALERT | ERR_FATAL;
3447 goto out;
3448 }
3449 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003450 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3451 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003452 err_code |= ERR_ALERT | ERR_FATAL;
3453 goto out;
3454 }
3455 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3456 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003457 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3458 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003459 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003460 goto out;
3461 }
3462 *tv = MS_TO_TICKS(timeout);
3463 }
3464 else if (!strcmp(args[0], "option")) {
3465 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003466 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3467 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003468 err_code |= ERR_ALERT | ERR_FATAL;
3469 goto out;
3470 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003471
Christopher Faulet305c6072017-02-23 16:17:53 +01003472 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003473 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003474 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003475 if (kwm == 1)
3476 curagent->flags &= ~SPOE_FL_PIPELINING;
3477 else
3478 curagent->flags |= SPOE_FL_PIPELINING;
3479 goto out;
3480 }
3481 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003482 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003483 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003484 if (kwm == 1)
3485 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003486 else {
3487 if (global.nbthread == 1)
3488 curagent->flags |= SPOE_FL_ASYNC;
3489 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003490 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3491 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003492 err_code |= ERR_WARN;
3493 }
3494 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003495 goto out;
3496 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003497 else if (!strcmp(args[1], "send-frag-payload")) {
3498 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3499 goto out;
3500 if (kwm == 1)
3501 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3502 else
3503 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3504 goto out;
3505 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003506 else if (!strcmp(args[1], "dontlog-normal")) {
3507 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3508 goto out;
3509 if (kwm == 1)
3510 curpxopts2 &= ~PR_O2_NOLOGNORM;
3511 else
3512 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003513 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003514 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003515
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003516 /* Following options does not support negation */
3517 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003518 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3519 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003520 err_code |= ERR_ALERT | ERR_FATAL;
3521 goto out;
3522 }
3523
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003524 if (!strcmp(args[1], "var-prefix")) {
3525 char *tmp;
3526
3527 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003528 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3529 file, linenum, args[0],
3530 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003531 err_code |= ERR_ALERT | ERR_FATAL;
3532 goto out;
3533 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003534 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3535 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003536 tmp = args[2];
3537 while (*tmp) {
3538 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003539 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003540 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003541 err_code |= ERR_ALERT | ERR_FATAL;
3542 goto out;
3543 }
3544 tmp++;
3545 }
3546 curagent->var_pfx = strdup(args[2]);
3547 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003548 else if (!strcmp(args[1], "force-set-var")) {
3549 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3550 goto out;
3551 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3552 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003553 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003554 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003555 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003556 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3557 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003558 else if (!strcmp(args[1], "set-on-error")) {
3559 char *tmp;
3560
3561 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003562 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3563 file, linenum, args[0],
3564 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003565 err_code |= ERR_ALERT | ERR_FATAL;
3566 goto out;
3567 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003568 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3569 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003570 tmp = args[2];
3571 while (*tmp) {
3572 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003573 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003574 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003575 err_code |= ERR_ALERT | ERR_FATAL;
3576 goto out;
3577 }
3578 tmp++;
3579 }
3580 curagent->var_on_error = strdup(args[2]);
3581 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003582 else if (!strcmp(args[1], "set-process-time")) {
3583 char *tmp;
3584
3585 if (!*args[2]) {
3586 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3587 file, linenum, args[0],
3588 args[1]);
3589 err_code |= ERR_ALERT | ERR_FATAL;
3590 goto out;
3591 }
3592 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3593 goto out;
3594 tmp = args[2];
3595 while (*tmp) {
3596 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003597 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003598 file, linenum, args[0], args[1]);
3599 err_code |= ERR_ALERT | ERR_FATAL;
3600 goto out;
3601 }
3602 tmp++;
3603 }
3604 curagent->var_t_process = strdup(args[2]);
3605 }
3606 else if (!strcmp(args[1], "set-total-time")) {
3607 char *tmp;
3608
3609 if (!*args[2]) {
3610 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3611 file, linenum, args[0],
3612 args[1]);
3613 err_code |= ERR_ALERT | ERR_FATAL;
3614 goto out;
3615 }
3616 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3617 goto out;
3618 tmp = args[2];
3619 while (*tmp) {
3620 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003621 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003622 file, linenum, args[0], args[1]);
3623 err_code |= ERR_ALERT | ERR_FATAL;
3624 goto out;
3625 }
3626 tmp++;
3627 }
3628 curagent->var_t_total = strdup(args[2]);
3629 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003630 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003631 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3632 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003633 err_code |= ERR_ALERT | ERR_FATAL;
3634 goto out;
3635 }
Christopher Faulet48026722016-11-16 15:01:12 +01003636 }
3637 else if (!strcmp(args[0], "maxconnrate")) {
3638 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003639 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3640 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003641 err_code |= ERR_ALERT | ERR_FATAL;
3642 goto out;
3643 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003644 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003645 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003646 curagent->cps_max = atol(args[1]);
3647 }
3648 else if (!strcmp(args[0], "maxerrrate")) {
3649 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003650 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3651 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003652 err_code |= ERR_ALERT | ERR_FATAL;
3653 goto out;
3654 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003655 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003656 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003657 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003658 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003659 else if (!strcmp(args[0], "max-frame-size")) {
3660 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003661 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3662 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003663 err_code |= ERR_ALERT | ERR_FATAL;
3664 goto out;
3665 }
3666 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3667 goto out;
3668 curagent->max_frame_size = atol(args[1]);
3669 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3670 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003671 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3672 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003673 err_code |= ERR_ALERT | ERR_FATAL;
3674 goto out;
3675 }
3676 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003677 else if (!strcmp(args[0], "max-waiting-frames")) {
3678 if (!*args[1]) {
3679 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3680 file, linenum, args[0]);
3681 err_code |= ERR_ALERT | ERR_FATAL;
3682 goto out;
3683 }
3684 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3685 goto out;
3686 curagent->max_fpa = atol(args[1]);
3687 if (curagent->max_fpa < 1) {
3688 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3689 file, linenum, args[0]);
3690 err_code |= ERR_ALERT | ERR_FATAL;
3691 goto out;
3692 }
3693 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003694 else if (!strcmp(args[0], "register-var-names")) {
3695 int cur_arg;
3696
3697 if (!*args[1]) {
3698 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3699 file, linenum, args[0]);
3700 err_code |= ERR_ALERT | ERR_FATAL;
3701 goto out;
3702 }
3703 cur_arg = 1;
3704 while (*args[cur_arg]) {
3705 struct spoe_var_placeholder *vph;
3706
3707 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3708 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3709 err_code |= ERR_ALERT | ERR_ABORT;
3710 goto out;
3711 }
3712 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3713 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3714 err_code |= ERR_ALERT | ERR_ABORT;
3715 goto out;
3716 }
3717 LIST_ADDQ(&curvars, &vph->list);
3718 cur_arg++;
3719 }
3720 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003721 else if (!strcmp(args[0], "log")) {
3722 char *errmsg = NULL;
3723
3724 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3725 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3726 err_code |= ERR_ALERT | ERR_FATAL;
3727 goto out;
3728 }
3729 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003730 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003731 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3732 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003733 err_code |= ERR_ALERT | ERR_FATAL;
3734 goto out;
3735 }
3736 out:
3737 return err_code;
3738}
Christopher Faulet11610f32017-09-21 10:23:10 +02003739static int
3740cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3741{
3742 struct spoe_group *grp;
3743 const char *err;
3744 int err_code = 0;
3745
3746 if ((cfg_scope == NULL && curengine != NULL) ||
3747 (cfg_scope != NULL && curengine == NULL) ||
3748 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3749 goto out;
3750
3751 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3752 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003753 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3754 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003755 err_code |= ERR_ALERT | ERR_ABORT;
3756 goto out;
3757 }
3758 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3759 err_code |= ERR_ABORT;
3760 goto out;
3761 }
3762
3763 err = invalid_char(args[1]);
3764 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003765 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3766 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003767 err_code |= ERR_ALERT | ERR_ABORT;
3768 goto out;
3769 }
3770
3771 list_for_each_entry(grp, &curgrps, list) {
3772 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003773 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3774 " name as another one declared at %s:%d.\n",
3775 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003776 err_code |= ERR_ALERT | ERR_FATAL;
3777 goto out;
3778 }
3779 }
3780
3781 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003782 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003783 err_code |= ERR_ALERT | ERR_ABORT;
3784 goto out;
3785 }
3786
3787 curgrp->id = strdup(args[1]);
3788 curgrp->conf.file = strdup(file);
3789 curgrp->conf.line = linenum;
3790 LIST_INIT(&curgrp->phs);
3791 LIST_INIT(&curgrp->messages);
3792 LIST_ADDQ(&curgrps, &curgrp->list);
3793 }
3794 else if (!strcmp(args[0], "messages")) {
3795 int cur_arg = 1;
3796 while (*args[cur_arg]) {
3797 struct spoe_placeholder *ph = NULL;
3798
3799 list_for_each_entry(ph, &curgrp->phs, list) {
3800 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003801 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3802 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003803 err_code |= ERR_ALERT | ERR_FATAL;
3804 goto out;
3805 }
3806 }
3807
3808 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003809 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003810 err_code |= ERR_ALERT | ERR_ABORT;
3811 goto out;
3812 }
3813 ph->id = strdup(args[cur_arg]);
3814 LIST_ADDQ(&curgrp->phs, &ph->list);
3815 cur_arg++;
3816 }
3817 }
3818 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003819 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3820 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003821 err_code |= ERR_ALERT | ERR_FATAL;
3822 goto out;
3823 }
3824 out:
3825 return err_code;
3826}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003827
3828static int
3829cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3830{
3831 struct spoe_message *msg;
3832 struct spoe_arg *arg;
3833 const char *err;
3834 char *errmsg = NULL;
3835 int err_code = 0;
3836
3837 if ((cfg_scope == NULL && curengine != NULL) ||
3838 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003839 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003840 goto out;
3841
3842 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3843 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003844 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3845 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003846 err_code |= ERR_ALERT | ERR_ABORT;
3847 goto out;
3848 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003849 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3850 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003851 goto out;
3852 }
3853
3854 err = invalid_char(args[1]);
3855 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003856 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3857 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003858 err_code |= ERR_ALERT | ERR_ABORT;
3859 goto out;
3860 }
3861
3862 list_for_each_entry(msg, &curmsgs, list) {
3863 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003864 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3865 " name as another one declared at %s:%d.\n",
3866 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003867 err_code |= ERR_ALERT | ERR_FATAL;
3868 goto out;
3869 }
3870 }
3871
3872 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003873 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003874 err_code |= ERR_ALERT | ERR_ABORT;
3875 goto out;
3876 }
3877
3878 curmsg->id = strdup(args[1]);
3879 curmsg->id_len = strlen(curmsg->id);
3880 curmsg->event = SPOE_EV_NONE;
3881 curmsg->conf.file = strdup(file);
3882 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003883 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003884 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003885 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003886 LIST_INIT(&curmsg->by_evt);
3887 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003888 LIST_ADDQ(&curmsgs, &curmsg->list);
3889 }
3890 else if (!strcmp(args[0], "args")) {
3891 int cur_arg = 1;
3892
3893 curproxy->conf.args.ctx = ARGC_SPOE;
3894 curproxy->conf.args.file = file;
3895 curproxy->conf.args.line = linenum;
3896 while (*args[cur_arg]) {
3897 char *delim = strchr(args[cur_arg], '=');
3898 int idx = 0;
3899
3900 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003901 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003902 err_code |= ERR_ALERT | ERR_ABORT;
3903 goto out;
3904 }
3905
3906 if (!delim) {
3907 arg->name = NULL;
3908 arg->name_len = 0;
3909 delim = args[cur_arg];
3910 }
3911 else {
3912 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3913 arg->name_len = delim - args[cur_arg];
3914 delim++;
3915 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003916 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3917 &idx, file, linenum, &errmsg,
3918 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003919 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003920 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003921 err_code |= ERR_ALERT | ERR_FATAL;
3922 free(arg->name);
3923 free(arg);
3924 goto out;
3925 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003926 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003927 LIST_ADDQ(&curmsg->args, &arg->list);
3928 cur_arg++;
3929 }
3930 curproxy->conf.args.file = NULL;
3931 curproxy->conf.args.line = 0;
3932 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003933 else if (!strcmp(args[0], "acl")) {
3934 err = invalid_char(args[1]);
3935 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003936 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3937 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003938 err_code |= ERR_ALERT | ERR_FATAL;
3939 goto out;
3940 }
3941 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003942 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3943 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003944 err_code |= ERR_ALERT | ERR_FATAL;
3945 goto out;
3946 }
3947 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003948 else if (!strcmp(args[0], "event")) {
3949 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003950 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003951 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003952 goto out;
3953 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003954 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3955 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003956
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003957 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3958 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3959 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3960 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3961
3962 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3963 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3964 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3965 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3966 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3967 curmsg->event = SPOE_EV_ON_TCP_RSP;
3968
3969 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3970 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3971 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3972 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3973 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3974 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3975 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003976 ha_alert("parsing [%s:%d] : unkown event '%s'.\n",
3977 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003978 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003979 goto out;
3980 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003981
3982 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3983 struct acl_cond *cond;
3984
3985 cond = build_acl_cond(file, linenum, &curmsg->acls,
3986 curproxy, (const char **)args+2,
3987 &errmsg);
3988 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003989 ha_alert("parsing [%s:%d] : error detected while "
3990 "parsing an 'event %s' condition : %s.\n",
3991 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003992 err_code |= ERR_ALERT | ERR_FATAL;
3993 goto out;
3994 }
3995 curmsg->cond = cond;
3996 }
3997 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003998 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3999 "or 'unless' followed by a condition but found '%s'.\n",
4000 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004001 err_code |= ERR_ALERT | ERR_FATAL;
4002 goto out;
4003 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004004 }
4005 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004006 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4007 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004008 err_code |= ERR_ALERT | ERR_FATAL;
4009 goto out;
4010 }
4011 out:
4012 free(errmsg);
4013 return err_code;
4014}
4015
4016/* Return -1 on error, else 0 */
4017static int
4018parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4019 struct flt_conf *fconf, char **err, void *private)
4020{
4021 struct list backup_sections;
4022 struct spoe_config *conf;
4023 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004024 struct spoe_group *grp, *grpback;
4025 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004026 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004027 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004028 char *file = NULL, *engine = NULL;
4029 int ret, pos = *cur_arg + 1;
4030
Christopher Faulet84c844e2018-03-23 14:37:14 +01004031 LIST_INIT(&curmsgs);
4032 LIST_INIT(&curgrps);
4033 LIST_INIT(&curmphs);
4034 LIST_INIT(&curgphs);
4035 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004036 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004037 curpxopts = 0;
4038 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004039
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004040 conf = calloc(1, sizeof(*conf));
4041 if (conf == NULL) {
4042 memprintf(err, "%s: out of memory", args[*cur_arg]);
4043 goto error;
4044 }
4045 conf->proxy = px;
4046
4047 while (*args[pos]) {
4048 if (!strcmp(args[pos], "config")) {
4049 if (!*args[pos+1]) {
4050 memprintf(err, "'%s' : '%s' option without value",
4051 args[*cur_arg], args[pos]);
4052 goto error;
4053 }
4054 file = args[pos+1];
4055 pos += 2;
4056 }
4057 else if (!strcmp(args[pos], "engine")) {
4058 if (!*args[pos+1]) {
4059 memprintf(err, "'%s' : '%s' option without value",
4060 args[*cur_arg], args[pos]);
4061 goto error;
4062 }
4063 engine = args[pos+1];
4064 pos += 2;
4065 }
4066 else {
4067 memprintf(err, "unknown keyword '%s'", args[pos]);
4068 goto error;
4069 }
4070 }
4071 if (file == NULL) {
4072 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4073 goto error;
4074 }
4075
4076 /* backup sections and register SPOE sections */
4077 LIST_INIT(&backup_sections);
4078 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004079 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4080 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004081 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004082
4083 /* Parse SPOE filter configuration file */
4084 curengine = engine;
4085 curproxy = px;
4086 curagent = NULL;
4087 curmsg = NULL;
4088 ret = readcfgfile(file);
4089 curproxy = NULL;
4090
4091 /* unregister SPOE sections and restore previous sections */
4092 cfg_unregister_sections();
4093 cfg_restore_sections(&backup_sections);
4094
4095 if (ret == -1) {
4096 memprintf(err, "Could not open configuration file %s : %s",
4097 file, strerror(errno));
4098 goto error;
4099 }
4100 if (ret & (ERR_ABORT|ERR_FATAL)) {
4101 memprintf(err, "Error(s) found in configuration file %s", file);
4102 goto error;
4103 }
4104
4105 /* Check SPOE agent */
4106 if (curagent == NULL) {
4107 memprintf(err, "No SPOE agent found in file %s", file);
4108 goto error;
4109 }
4110 if (curagent->b.name == NULL) {
4111 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4112 curagent->id, curagent->conf.file, curagent->conf.line);
4113 goto error;
4114 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004115 if (curagent->timeout.hello == TICK_ETERNITY ||
4116 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004117 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004118 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4119 " | While not properly invalid, you will certainly encounter various problems\n"
4120 " | with such a configuration. To fix this, please ensure that all following\n"
4121 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4122 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004123 }
4124 if (curagent->var_pfx == NULL) {
4125 char *tmp = curagent->id;
4126
4127 while (*tmp) {
4128 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
4129 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4130 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4131 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4132 goto error;
4133 }
4134 tmp++;
4135 }
4136 curagent->var_pfx = strdup(curagent->id);
4137 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01004138 if (curagent->engine_id == NULL)
4139 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004140
Christopher Fauletb7426d12018-03-21 14:12:17 +01004141 if (curagent->var_on_error) {
4142 struct arg arg;
4143
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004144 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004145 curagent->var_pfx, curagent->var_on_error);
4146
4147 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004148 arg.data.str.area = trash.area;
4149 arg.data.str.data = trash.data;
Christopher Fauletb7426d12018-03-21 14:12:17 +01004150 if (!vars_check_arg(&arg, err)) {
4151 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4152 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4153 goto error;
4154 }
4155 }
4156
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004157 if (curagent->var_t_process) {
4158 struct arg arg;
4159
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004160 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004161 curagent->var_pfx, curagent->var_t_process);
4162
4163 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004164 arg.data.str.area = trash.area;
4165 arg.data.str.data = trash.data;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004166 if (!vars_check_arg(&arg, err)) {
4167 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4168 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4169 goto error;
4170 }
4171 }
4172
4173 if (curagent->var_t_total) {
4174 struct arg arg;
4175
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004176 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004177 curagent->var_pfx, curagent->var_t_total);
4178
4179 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004180 arg.data.str.area = trash.area;
4181 arg.data.str.data = trash.data;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004182 if (!vars_check_arg(&arg, err)) {
4183 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4184 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4185 goto error;
4186 }
4187 }
4188
Christopher Faulet11610f32017-09-21 10:23:10 +02004189 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004190 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4191 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004192 goto finish;
4193 }
4194
Christopher Faulet11610f32017-09-21 10:23:10 +02004195 /* Replace placeholders by the corresponding messages for the SPOE
4196 * agent */
4197 list_for_each_entry(ph, &curmphs, list) {
4198 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004199 struct spoe_arg *arg;
4200 unsigned int where;
4201
Christopher Faulet11610f32017-09-21 10:23:10 +02004202 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004203 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4204 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4205 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4206 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4207 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4208 }
4209 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4210 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4211 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004212 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4213 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004214 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004215 }
4216 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004217 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4218 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004219 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004220 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004221
4222 where = 0;
4223 switch (msg->event) {
4224 case SPOE_EV_ON_CLIENT_SESS:
4225 where |= SMP_VAL_FE_CON_ACC;
4226 break;
4227
4228 case SPOE_EV_ON_TCP_REQ_FE:
4229 where |= SMP_VAL_FE_REQ_CNT;
4230 break;
4231
4232 case SPOE_EV_ON_HTTP_REQ_FE:
4233 where |= SMP_VAL_FE_HRQ_HDR;
4234 break;
4235
4236 case SPOE_EV_ON_TCP_REQ_BE:
4237 if (px->cap & PR_CAP_FE)
4238 where |= SMP_VAL_FE_REQ_CNT;
4239 if (px->cap & PR_CAP_BE)
4240 where |= SMP_VAL_BE_REQ_CNT;
4241 break;
4242
4243 case SPOE_EV_ON_HTTP_REQ_BE:
4244 if (px->cap & PR_CAP_FE)
4245 where |= SMP_VAL_FE_HRQ_HDR;
4246 if (px->cap & PR_CAP_BE)
4247 where |= SMP_VAL_BE_HRQ_HDR;
4248 break;
4249
4250 case SPOE_EV_ON_SERVER_SESS:
4251 where |= SMP_VAL_BE_SRV_CON;
4252 break;
4253
4254 case SPOE_EV_ON_TCP_RSP:
4255 if (px->cap & PR_CAP_FE)
4256 where |= SMP_VAL_FE_RES_CNT;
4257 if (px->cap & PR_CAP_BE)
4258 where |= SMP_VAL_BE_RES_CNT;
4259 break;
4260
4261 case SPOE_EV_ON_HTTP_RSP:
4262 if (px->cap & PR_CAP_FE)
4263 where |= SMP_VAL_FE_HRS_HDR;
4264 if (px->cap & PR_CAP_BE)
4265 where |= SMP_VAL_BE_HRS_HDR;
4266 break;
4267
4268 default:
4269 break;
4270 }
4271
4272 list_for_each_entry(arg, &msg->args, list) {
4273 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004274 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004275 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004276 "none of which is available here ('%s')",
4277 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004278 sample_ckp_names(arg->expr->fetch->use),
4279 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004280 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004281 }
4282 }
4283
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004284 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004285 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4286 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004287 }
4288 }
4289 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004290 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004291 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004292 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004293 continue;
4294 }
4295
Christopher Faulet11610f32017-09-21 10:23:10 +02004296 /* Replace placeholders by the corresponding groups for the SPOE
4297 * agent */
4298 list_for_each_entry(ph, &curgphs, list) {
4299 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4300 if (!strcmp(grp->id, ph->id)) {
4301 grp->agent = curagent;
4302 LIST_DEL(&grp->list);
4303 LIST_ADDQ(&curagent->groups, &grp->list);
4304 goto next_aph;
4305 }
4306 }
4307 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4308 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4309 goto error;
4310 next_aph:
4311 continue;
4312 }
4313
4314 /* Replace placeholders by the corresponding message for each SPOE
4315 * group of the SPOE agent */
4316 list_for_each_entry(grp, &curagent->groups, list) {
4317 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4318 list_for_each_entry(msg, &curmsgs, list) {
4319 if (!strcmp(msg->id, ph->id)) {
4320 if (msg->group != NULL) {
4321 memprintf(err, "SPOE message '%s' already belongs to "
4322 "the SPOE group '%s' declare at %s:%d",
4323 msg->id, msg->group->id,
4324 msg->group->conf.file,
4325 msg->group->conf.line);
4326 goto error;
4327 }
4328
4329 /* Scope for arguments are not checked for now. We will check
4330 * them only if a rule use the corresponding SPOE group. */
4331 msg->agent = curagent;
4332 msg->group = grp;
4333 LIST_DEL(&ph->list);
4334 LIST_ADDQ(&grp->messages, &msg->by_grp);
4335 goto next_mph_grp;
4336 }
4337 }
4338 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4339 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4340 goto error;
4341 next_mph_grp:
4342 continue;
4343 }
4344 }
4345
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004346 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004347 /* move curmsgs to the agent message list */
4348 curmsgs.n->p = &curagent->messages;
4349 curmsgs.p->n = &curagent->messages;
4350 curagent->messages = curmsgs;
4351 LIST_INIT(&curmsgs);
4352
Christopher Faulet7ee86672017-09-19 11:08:28 +02004353 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004354 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004355
4356 /* Start agent's proxy initialization here. It will be finished during
4357 * the filter init. */
4358 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4359 init_new_proxy(&conf->agent_fe);
4360 conf->agent_fe.id = conf->agent->id;
4361 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004362 conf->agent_fe.options |= curpxopts;
4363 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004364
4365 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4366 LIST_DEL(&logsrv->list);
4367 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4368 }
4369
Christopher Faulet11610f32017-09-21 10:23:10 +02004370 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4371 LIST_DEL(&ph->list);
4372 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004373 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004374 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4375 LIST_DEL(&ph->list);
4376 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004377 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004378 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4379 struct arg arg;
4380
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004381 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004382 curagent->var_pfx, vph->name);
4383
4384 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004385 arg.data.str.area = trash.area;
4386 arg.data.str.data = trash.data;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004387 if (!vars_check_arg(&arg, err)) {
4388 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4389 curagent->id, curagent->var_pfx, vph->name, *err);
4390 goto error;
4391 }
4392
4393 LIST_DEL(&vph->list);
4394 free(vph->name);
4395 free(vph);
4396 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004397 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4398 LIST_DEL(&grp->list);
4399 spoe_release_group(grp);
4400 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004401 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004402 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004403 fconf->ops = &spoe_ops;
4404 fconf->conf = conf;
4405 return 0;
4406
4407 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004408 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004409 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4410 LIST_DEL(&ph->list);
4411 spoe_release_placeholder(ph);
4412 }
4413 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4414 LIST_DEL(&ph->list);
4415 spoe_release_placeholder(ph);
4416 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004417 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4418 LIST_DEL(&vph->list);
4419 free(vph->name);
4420 free(vph);
4421 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004422 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4423 LIST_DEL(&grp->list);
4424 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004425 }
4426 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4427 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004428 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004429 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004430 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4431 LIST_DEL(&logsrv->list);
4432 free(logsrv);
4433 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004434 free(conf);
4435 return -1;
4436}
4437
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004438/* Send message of a SPOE group. This is the action_ptr callback of a rule
4439 * associated to a "send-spoe-group" action.
4440 *
4441 * It returns ACT_RET_CONT is processing is finished without error, it returns
4442 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4443 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004444static enum act_return
4445spoe_send_group(struct act_rule *rule, struct proxy *px,
4446 struct session *sess, struct stream *s, int flags)
4447{
4448 struct filter *filter;
4449 struct spoe_agent *agent = NULL;
4450 struct spoe_group *group = NULL;
4451 struct spoe_context *ctx = NULL;
4452 int ret, dir;
4453
4454 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4455 if (filter->config == rule->arg.act.p[0]) {
4456 agent = rule->arg.act.p[2];
4457 group = rule->arg.act.p[3];
4458 ctx = filter->ctx;
4459 break;
4460 }
4461 }
4462 if (agent == NULL || group == NULL || ctx == NULL)
4463 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004464 if (ctx->state == SPOE_CTX_ST_NONE)
4465 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004466
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004467 switch (rule->from) {
4468 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4469 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4470 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4471 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4472 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4473 default:
4474 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4475 " - internal error while execute spoe-send-group\n",
4476 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4477 __FUNCTION__, s);
4478 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4479 agent->id);
4480 return ACT_RET_CONT;
4481 }
4482
4483 ret = spoe_process_group(s, ctx, group, dir);
4484 if (ret == 1)
4485 return ACT_RET_CONT;
4486 else if (ret == 0) {
4487 if (flags & ACT_FLAG_FINAL) {
4488 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4489 " - failed to process group '%s': interrupted by caller\n",
4490 (int)now.tv_sec, (int)now.tv_usec,
4491 agent->id, __FUNCTION__, s, group->id);
4492 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004493 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004494 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004495 return ACT_RET_CONT;
4496 }
4497 return ACT_RET_YIELD;
4498 }
4499 else
4500 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004501}
4502
4503/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4504 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4505 * action should be:
4506 *
4507 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4508 *
4509 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4510 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4511 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4512 * group.
4513 *
4514 * The function returns 1 in success case, otherwise, it returns 0 and err is
4515 * filled.
4516 */
4517static int
4518check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4519{
4520 struct flt_conf *fconf;
4521 struct spoe_config *conf;
4522 struct spoe_agent *agent = NULL;
4523 struct spoe_group *group;
4524 struct spoe_message *msg;
4525 char *engine_id = rule->arg.act.p[0];
4526 char *group_id = rule->arg.act.p[1];
4527 unsigned int where = 0;
4528
4529 switch (rule->from) {
4530 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4531 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4532 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4533 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4534 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4535 default:
4536 memprintf(err,
4537 "internal error, unexpected rule->from=%d, please report this bug!",
4538 rule->from);
4539 goto error;
4540 }
4541
4542 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4543 * <px> */
4544 list_for_each_entry(fconf, &px->filter_configs, list) {
4545 conf = fconf->conf;
4546
4547 /* This is not an SPOE filter */
4548 if (fconf->id != spoe_filter_id)
4549 continue;
4550
4551 /* This is the good engine */
4552 if (!strcmp(conf->id, engine_id)) {
4553 agent = conf->agent;
4554 break;
4555 }
4556 }
4557 if (agent == NULL) {
4558 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4559 engine_id, group_id);
4560 goto error;
4561 }
4562
4563 /* Try to find the right group */
4564 list_for_each_entry(group, &agent->groups, list) {
4565 /* This is the good group */
4566 if (!strcmp(group->id, group_id))
4567 break;
4568 }
4569 if (&group->list == &agent->groups) {
4570 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4571 group_id, engine_id);
4572 goto error;
4573 }
4574
4575 /* Ok, we found the group, we need to check messages and their
4576 * arguments */
4577 list_for_each_entry(msg, &group->messages, by_grp) {
4578 struct spoe_arg *arg;
4579
4580 list_for_each_entry(arg, &msg->args, list) {
4581 if (!(arg->expr->fetch->val & where)) {
4582 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4583 "some args extract information from '%s',"
4584 "none of which is available here ('%s')",
4585 msg->id, group->id, msg->conf.file, msg->conf.line,
4586 sample_ckp_names(arg->expr->fetch->use),
4587 sample_ckp_names(where));
4588 goto error;
4589 }
4590 }
4591 }
4592
4593 free(engine_id);
4594 free(group_id);
4595 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4596 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4597 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4598 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4599 return 1;
4600
4601 error:
4602 free(engine_id);
4603 free(group_id);
4604 return 0;
4605}
4606
4607/* Parse 'send-spoe-group' action following the format:
4608 *
4609 * ... send-spoe-group <engine-id> <group-id>
4610 *
4611 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4612 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4613 * ids are saved and used later, when the rule will be checked.
4614 */
4615static enum act_parse_ret
4616parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4617 struct act_rule *rule, char **err)
4618{
4619 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4620 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4621 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4622 return ACT_RET_PRS_ERR;
4623 }
4624 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4625 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4626
4627 (*orig_arg) += 2;
4628
4629 rule->action = ACT_CUSTOM;
4630 rule->action_ptr = spoe_send_group;
4631 rule->check_ptr = check_send_spoe_group;
4632 return ACT_RET_PRS_OK;
4633}
4634
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004635
4636/* Declare the filter parser for "spoe" keyword */
4637static struct flt_kw_list flt_kws = { "SPOE", { }, {
4638 { "spoe", parse_spoe_flt, NULL },
4639 { NULL, NULL, NULL },
4640 }
4641};
4642
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004643/* Delcate the action parser for "spoe-action" keyword */
4644static struct action_kw_list tcp_req_action_kws = { { }, {
4645 { "send-spoe-group", parse_send_spoe_group },
4646 { /* END */ },
4647 }
4648};
4649static struct action_kw_list tcp_res_action_kws = { { }, {
4650 { "send-spoe-group", parse_send_spoe_group },
4651 { /* END */ },
4652 }
4653};
4654static struct action_kw_list http_req_action_kws = { { }, {
4655 { "send-spoe-group", parse_send_spoe_group },
4656 { /* END */ },
4657 }
4658};
4659static struct action_kw_list http_res_action_kws = { { }, {
4660 { "send-spoe-group", parse_send_spoe_group },
4661 { /* END */ },
4662 }
4663};
4664
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004665__attribute__((constructor))
4666static void __spoe_init(void)
4667{
4668 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004669 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4670 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4671 http_req_keywords_register(&http_req_action_kws);
4672 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004673
Willy Tarreaubafbe012017-11-24 17:34:44 +01004674 pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
4675 pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004676}
4677
4678__attribute__((destructor))
4679static void
4680__spoe_deinit(void)
4681{
Willy Tarreaubafbe012017-11-24 17:34:44 +01004682 pool_destroy(pool_head_spoe_ctx);
4683 pool_destroy(pool_head_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004684}