Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1 | /* |
| 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 Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 15 | #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> |
| 21 | |
| 22 | #include <types/arg.h> |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 23 | #include <types/global.h> |
Christopher Faulet | 1f40b91 | 2017-02-17 09:32:19 +0100 | [diff] [blame] | 24 | #include <types/spoe.h> |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 25 | |
| 26 | #include <proto/arg.h> |
| 27 | #include <proto/backend.h> |
| 28 | #include <proto/filters.h> |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 29 | #include <proto/freq_ctr.h> |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 30 | #include <proto/frontend.h> |
| 31 | #include <proto/log.h> |
| 32 | #include <proto/proto_http.h> |
| 33 | #include <proto/proxy.h> |
| 34 | #include <proto/sample.h> |
| 35 | #include <proto/session.h> |
| 36 | #include <proto/signal.h> |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 37 | #include <proto/spoe.h> |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 38 | #include <proto/stream.h> |
| 39 | #include <proto/stream_interface.h> |
| 40 | #include <proto/task.h> |
| 41 | #include <proto/vars.h> |
| 42 | |
| 43 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 44 | #define SPOE_PRINTF(x...) fprintf(x) |
| 45 | #else |
| 46 | #define SPOE_PRINTF(x...) |
| 47 | #endif |
| 48 | |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 49 | /* Reserved 4 bytes to the frame size. So a frame and its size can be written |
| 50 | * together in a buffer */ |
| 51 | #define MAX_FRAME_SIZE global.tune.bufsize - 4 |
| 52 | |
| 53 | /* The minimum size for a frame */ |
| 54 | #define MIN_FRAME_SIZE 256 |
| 55 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 56 | /* Reserved for the metadata and the frame type. |
| 57 | * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */ |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 58 | #define FRAME_HDR_SIZE 32 |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 59 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 60 | /* Helper to get SPOE ctx inside an appctx */ |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 61 | #define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr)) |
| 62 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 63 | /* SPOE filter id. Used to identify SPOE filters */ |
| 64 | const char *spoe_filter_id = "SPOE filter"; |
| 65 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 66 | /* Set if the handle on SIGUSR1 is registered */ |
| 67 | static int sighandler_registered = 0; |
| 68 | |
| 69 | /* proxy used during the parsing */ |
| 70 | struct proxy *curproxy = NULL; |
| 71 | |
| 72 | /* The name of the SPOE engine, used during the parsing */ |
| 73 | char *curengine = NULL; |
| 74 | |
| 75 | /* SPOE agent used during the parsing */ |
| 76 | struct spoe_agent *curagent = NULL; |
| 77 | |
| 78 | /* SPOE message used during the parsing */ |
| 79 | struct spoe_message *curmsg = NULL; |
| 80 | |
| 81 | /* list of SPOE messages and placeholders used during the parsing */ |
| 82 | struct list curmsgs; |
| 83 | struct list curmps; |
| 84 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 85 | /* Pools used to allocate SPOE structs */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 86 | static struct pool_head *pool2_spoe_ctx = NULL; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 87 | static struct pool_head *pool2_spoe_appctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 88 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 89 | struct flt_ops spoe_ops; |
| 90 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 91 | static int spoe_queue_context(struct spoe_context *ctx); |
| 92 | static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait); |
| 93 | static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 94 | |
| 95 | /******************************************************************** |
| 96 | * helper functions/globals |
| 97 | ********************************************************************/ |
| 98 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 99 | spoe_release_msg_placeholder(struct spoe_msg_placeholder *mp) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 100 | { |
| 101 | if (!mp) |
| 102 | return; |
| 103 | free(mp->id); |
| 104 | free(mp); |
| 105 | } |
| 106 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 107 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 108 | spoe_release_message(struct spoe_message *msg) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 109 | { |
| 110 | struct spoe_arg *arg, *back; |
| 111 | |
| 112 | if (!msg) |
| 113 | return; |
| 114 | free(msg->id); |
| 115 | free(msg->conf.file); |
| 116 | list_for_each_entry_safe(arg, back, &msg->args, list) { |
| 117 | release_sample_expr(arg->expr); |
| 118 | free(arg->name); |
| 119 | LIST_DEL(&arg->list); |
| 120 | free(arg); |
| 121 | } |
| 122 | free(msg); |
| 123 | } |
| 124 | |
| 125 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 126 | spoe_release_agent(struct spoe_agent *agent) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 127 | { |
| 128 | struct spoe_message *msg, *back; |
| 129 | int i; |
| 130 | |
| 131 | if (!agent) |
| 132 | return; |
| 133 | free(agent->id); |
| 134 | free(agent->conf.file); |
| 135 | free(agent->var_pfx); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 136 | free(agent->engine_id); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 137 | free(agent->var_on_error); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 138 | for (i = 0; i < SPOE_EV_EVENTS; ++i) { |
| 139 | list_for_each_entry_safe(msg, back, &agent->messages[i], list) { |
| 140 | LIST_DEL(&msg->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 141 | spoe_release_message(msg); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | free(agent); |
| 145 | } |
| 146 | |
| 147 | static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 148 | [SPOE_FRM_ERR_NONE] = "normal", |
| 149 | [SPOE_FRM_ERR_IO] = "I/O error", |
| 150 | [SPOE_FRM_ERR_TOUT] = "a timeout occurred", |
| 151 | [SPOE_FRM_ERR_TOO_BIG] = "frame is too big", |
| 152 | [SPOE_FRM_ERR_INVALID] = "invalid frame received", |
| 153 | [SPOE_FRM_ERR_NO_VSN] = "version value not found", |
| 154 | [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found", |
| 155 | [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found", |
| 156 | [SPOE_FRM_ERR_BAD_VSN] = "unsupported version", |
| 157 | [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small", |
| 158 | [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported", |
| 159 | [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames", |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 160 | [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 161 | [SPOE_FRM_ERR_RES] = "resource allocation error", |
| 162 | [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | static const char *spoe_event_str[SPOE_EV_EVENTS] = { |
| 166 | [SPOE_EV_ON_CLIENT_SESS] = "on-client-session", |
| 167 | [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request", |
| 168 | [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request", |
| 169 | [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request", |
| 170 | [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request", |
| 171 | |
| 172 | [SPOE_EV_ON_SERVER_SESS] = "on-server-session", |
| 173 | [SPOE_EV_ON_TCP_RSP] = "on-tcp-response", |
| 174 | [SPOE_EV_ON_HTTP_RSP] = "on-http-response", |
| 175 | }; |
| 176 | |
| 177 | |
| 178 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 179 | |
| 180 | static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 181 | [SPOE_CTX_ST_NONE] = "NONE", |
| 182 | [SPOE_CTX_ST_READY] = "READY", |
| 183 | [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS", |
| 184 | [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS", |
| 185 | [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK", |
| 186 | [SPOE_CTX_ST_DONE] = "DONE", |
| 187 | [SPOE_CTX_ST_ERROR] = "ERROR", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 191 | [SPOE_APPCTX_ST_CONNECT] = "CONNECT", |
| 192 | [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING", |
| 193 | [SPOE_APPCTX_ST_IDLE] = "IDLE", |
| 194 | [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING", |
| 195 | [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY", |
| 196 | [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK", |
| 197 | [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT", |
| 198 | [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING", |
| 199 | [SPOE_APPCTX_ST_EXIT] = "EXIT", |
| 200 | [SPOE_APPCTX_ST_END] = "END", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | #endif |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 204 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 205 | /* Used to generates a unique id for an engine. On success, it returns a |
| 206 | * allocated string. So it is the caller's reponsibility to release it. If the |
| 207 | * allocation failed, it returns NULL. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 208 | static char * |
| 209 | generate_pseudo_uuid() |
| 210 | { |
| 211 | static int init = 0; |
| 212 | |
| 213 | const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"; |
| 214 | const char uuid_chr[] = "0123456789ABCDEF-"; |
| 215 | char *uuid; |
| 216 | int i; |
| 217 | |
| 218 | if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL) |
| 219 | return NULL; |
| 220 | |
| 221 | if (!init) { |
| 222 | srand(now_ms); |
| 223 | init = 1; |
| 224 | } |
| 225 | |
| 226 | for (i = 0; i < sizeof(uuid_fmt)-1; i++) { |
| 227 | int r = rand () % 16; |
| 228 | |
| 229 | switch (uuid_fmt[i]) { |
| 230 | case 'x' : uuid[i] = uuid_chr[r]; break; |
| 231 | case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break; |
| 232 | default : uuid[i] = uuid_fmt[i]; break; |
| 233 | } |
| 234 | } |
| 235 | return uuid; |
| 236 | } |
| 237 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 238 | /* Returns the minimum number of appets alive at a time. This function is used |
| 239 | * to know if more applets should be created for an engine. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 240 | static inline unsigned int |
| 241 | min_applets_act(struct spoe_agent *agent) |
| 242 | { |
| 243 | unsigned int nbsrv; |
| 244 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 245 | /* TODO: Add a config parameter to customize this value. Always 0 for |
| 246 | * now */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 247 | if (agent->min_applets) |
| 248 | return agent->min_applets; |
| 249 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 250 | /* Get the number of active servers for the backend */ |
| 251 | nbsrv = (agent->b.be->srv_act |
| 252 | ? agent->b.be->srv_act |
| 253 | : agent->b.be->srv_bck); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 254 | return 2*nbsrv; |
| 255 | } |
| 256 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 257 | /******************************************************************** |
| 258 | * Functions that encode/decode SPOE frames |
| 259 | ********************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 260 | /* Helper to get static string length, excluding the terminating null byte */ |
| 261 | #define SLEN(str) (sizeof(str)-1) |
| 262 | |
| 263 | /* Predefined key used in HELLO/DISCONNECT frames */ |
| 264 | #define SUPPORTED_VERSIONS_KEY "supported-versions" |
| 265 | #define VERSION_KEY "version" |
| 266 | #define MAX_FRAME_SIZE_KEY "max-frame-size" |
| 267 | #define CAPABILITIES_KEY "capabilities" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 268 | #define ENGINE_ID_KEY "engine-id" |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 269 | #define HEALTHCHECK_KEY "healthcheck" |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 270 | #define STATUS_CODE_KEY "status-code" |
| 271 | #define MSG_KEY "message" |
| 272 | |
| 273 | struct spoe_version { |
| 274 | char *str; |
| 275 | int min; |
| 276 | int max; |
| 277 | }; |
| 278 | |
| 279 | /* All supported versions */ |
| 280 | static struct spoe_version supported_versions[] = { |
| 281 | {"1.0", 1000, 1000}, |
| 282 | {NULL, 0, 0} |
| 283 | }; |
| 284 | |
| 285 | /* Comma-separated list of supported versions */ |
| 286 | #define SUPPORTED_VERSIONS_VAL "1.0" |
| 287 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 288 | /* Convert a string to a SPOE version value. The string must follow the format |
| 289 | * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR). |
| 290 | * If an error occurred, -1 is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 291 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 292 | spoe_str_to_vsn(const char *str, size_t len) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 293 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 294 | const char *p, *end; |
| 295 | int maj, min, vsn; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 296 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 297 | p = str; |
| 298 | end = str+len; |
| 299 | maj = min = 0; |
| 300 | vsn = -1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 301 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 302 | /* skip leading spaces */ |
| 303 | while (p < end && isspace(*p)) |
| 304 | p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 305 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 306 | /* parse Major number, until the '.' */ |
| 307 | while (*p != '.') { |
| 308 | if (p >= end || *p < '0' || *p > '9') |
| 309 | goto out; |
| 310 | maj *= 10; |
| 311 | maj += (*p - '0'); |
| 312 | p++; |
| 313 | } |
| 314 | |
| 315 | /* check Major version */ |
| 316 | if (!maj) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 317 | goto out; |
| 318 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 319 | p++; /* skip the '.' */ |
| 320 | if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */ |
| 321 | goto out; |
| 322 | |
| 323 | /* Parse Minor number */ |
| 324 | while (p < end) { |
| 325 | if (*p < '0' || *p > '9') |
| 326 | break; |
| 327 | min *= 10; |
| 328 | min += (*p - '0'); |
| 329 | p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 330 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 331 | |
| 332 | /* check Minor number */ |
| 333 | if (min > 999) |
| 334 | goto out; |
| 335 | |
| 336 | /* skip trailing spaces */ |
| 337 | while (p < end && isspace(*p)) |
| 338 | p++; |
| 339 | if (p != end) |
| 340 | goto out; |
| 341 | |
| 342 | vsn = maj * 1000 + min; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 343 | out: |
| 344 | return vsn; |
| 345 | } |
| 346 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 347 | /* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of |
| 348 | * encoded bytes in the frame on success, 0 if an encoding error occured and -1 |
| 349 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 350 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 351 | spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 352 | { |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 353 | struct chunk *chk; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 354 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 355 | char *p, *end; |
| 356 | unsigned int flags = SPOE_FRM_FL_FIN; |
| 357 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 358 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 359 | p = frame; |
| 360 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 361 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 362 | /* Set Frame type */ |
| 363 | *p++ = SPOE_FRM_T_HAPROXY_HELLO; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 364 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 365 | /* Set flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 366 | memcpy(p, (char *)&flags, 4); |
| 367 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 368 | |
| 369 | /* No stream-id and frame-id for HELLO frames */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 370 | *p++ = 0; *p++ = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 371 | |
| 372 | /* There are 3 mandatory items: "supported-versions", "max-frame-size" |
| 373 | * and "capabilities" */ |
| 374 | |
| 375 | /* "supported-versions" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 376 | sz = SLEN(SUPPORTED_VERSIONS_KEY); |
| 377 | if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1) |
| 378 | goto too_big; |
| 379 | |
| 380 | *p++ = SPOE_DATA_T_STR; |
| 381 | sz = SLEN(SUPPORTED_VERSIONS_VAL); |
| 382 | if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1) |
| 383 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 384 | |
| 385 | /* "max-fram-size" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 386 | sz = SLEN(MAX_FRAME_SIZE_KEY); |
| 387 | if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1) |
| 388 | goto too_big; |
| 389 | |
| 390 | *p++ = SPOE_DATA_T_UINT32; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 391 | if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 392 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 393 | |
| 394 | /* "capabilities" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 395 | sz = SLEN(CAPABILITIES_KEY); |
| 396 | if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1) |
| 397 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 398 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 399 | *p++ = SPOE_DATA_T_STR; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 400 | chk = get_trash_chunk(); |
| 401 | if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) { |
| 402 | memcpy(chk->str, "pipelining", 10); |
| 403 | chk->len += 10; |
| 404 | } |
| 405 | if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) { |
| 406 | if (chk->len) chk->str[chk->len++] = ','; |
| 407 | memcpy(chk->str+chk->len, "async", 5); |
| 408 | chk->len += 5; |
| 409 | } |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 410 | if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) { |
| 411 | if (chk->len) chk->str[chk->len++] = ','; |
| 412 | memcpy(chk->str+chk->len, "fragmentation", 13); |
| 413 | chk->len += 5; |
| 414 | } |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 415 | if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 416 | goto too_big; |
| 417 | |
| 418 | /* (optionnal) "engine-id" K/V item, if present */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 419 | if (agent != NULL && agent->engine_id != NULL) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 420 | sz = SLEN(ENGINE_ID_KEY); |
| 421 | if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1) |
| 422 | goto too_big; |
| 423 | |
| 424 | *p++ = SPOE_DATA_T_STR; |
| 425 | sz = strlen(agent->engine_id); |
| 426 | if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1) |
| 427 | goto too_big; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 428 | } |
| 429 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 430 | return (p - frame); |
| 431 | |
| 432 | too_big: |
| 433 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 434 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 435 | } |
| 436 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 437 | /* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of |
| 438 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
| 439 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 440 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 441 | spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 442 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 443 | const char *reason; |
| 444 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 445 | unsigned int flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 446 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 447 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 448 | p = frame; |
| 449 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 450 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 451 | /* Set Frame type */ |
| 452 | *p++ = SPOE_FRM_T_HAPROXY_DISCON; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 453 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 454 | /* Set flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 455 | memcpy(p, (char *)&flags, 4); |
| 456 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 457 | |
| 458 | /* No stream-id and frame-id for DISCONNECT frames */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 459 | *p++ = 0; *p++ = 0; |
| 460 | |
| 461 | if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS) |
| 462 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 463 | |
| 464 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 465 | |
| 466 | /* "status-code" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 467 | sz = SLEN(STATUS_CODE_KEY); |
| 468 | if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1) |
| 469 | goto too_big; |
| 470 | |
| 471 | *p++ = SPOE_DATA_T_UINT32; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 472 | if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 473 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 474 | |
| 475 | /* "message" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 476 | sz = SLEN(MSG_KEY); |
| 477 | if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1) |
| 478 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 479 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 480 | /*Get the message corresponding to the status code */ |
| 481 | reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]; |
| 482 | |
| 483 | *p++ = SPOE_DATA_T_STR; |
| 484 | sz = strlen(reason); |
| 485 | if (spoe_encode_buffer(reason, sz, &p, end) == -1) |
| 486 | goto too_big; |
| 487 | |
| 488 | return (p - frame); |
| 489 | |
| 490 | too_big: |
| 491 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 492 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 493 | } |
| 494 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 495 | /* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of |
| 496 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
| 497 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 498 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 499 | spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 500 | char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 501 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 502 | char *p, *end; |
| 503 | unsigned int stream_id, frame_id; |
| 504 | unsigned int flags = SPOE_FRM_FL_FIN; |
| 505 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 506 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 507 | p = frame; |
| 508 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 509 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 510 | stream_id = ctx->stream_id; |
| 511 | frame_id = ctx->frame_id; |
| 512 | |
| 513 | if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) { |
| 514 | /* The fragmentation is not supported by the applet */ |
| 515 | if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) { |
| 516 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
| 517 | return -1; |
| 518 | } |
| 519 | flags = ctx->frag_ctx.flags; |
| 520 | } |
| 521 | |
| 522 | /* Set Frame type */ |
| 523 | *p++ = SPOE_FRM_T_HAPROXY_NOTIFY; |
| 524 | |
| 525 | /* Set flags */ |
| 526 | memcpy(p, (char *)&flags, 4); |
| 527 | p += 4; |
| 528 | |
| 529 | /* Set stream-id and frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 530 | if (encode_varint(stream_id, &p, end) == -1) |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 531 | goto too_big; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 532 | if (encode_varint(frame_id, &p, end) == -1) |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 533 | goto too_big; |
| 534 | |
| 535 | /* Copy encoded messages, if possible */ |
| 536 | sz = ctx->buffer->i; |
| 537 | if (p + sz >= end) |
| 538 | goto too_big; |
| 539 | memcpy(p, ctx->buffer->p, sz); |
| 540 | p += sz; |
| 541 | |
| 542 | return (p - frame); |
| 543 | |
| 544 | too_big: |
| 545 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | /* Encode next part of a fragmented frame sent by HAProxy to an agent. It |
| 550 | * returns the number of encoded bytes in the frame on success, 0 if an encoding |
| 551 | * error occurred and -1 if a fatal error occurred. */ |
| 552 | static int |
| 553 | spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx, |
| 554 | char *frame, size_t size) |
| 555 | { |
| 556 | char *p, *end; |
| 557 | unsigned int stream_id, frame_id; |
| 558 | unsigned int flags; |
| 559 | size_t sz; |
| 560 | |
| 561 | p = frame; |
| 562 | end = frame+size; |
| 563 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 564 | /* <ctx> is null when the stream has aborted the processing of a |
| 565 | * fragmented frame. In this case, we must notify the corresponding |
| 566 | * agent using ids stored in <frag_ctx>. */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 567 | if (ctx == NULL) { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 568 | flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 569 | stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid; |
| 570 | frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid; |
| 571 | } |
| 572 | else { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 573 | flags = ctx->frag_ctx.flags; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 574 | stream_id = ctx->stream_id; |
| 575 | frame_id = ctx->frame_id; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 576 | } |
| 577 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 578 | /* Set Frame type */ |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 579 | *p++ = SPOE_FRM_T_UNSET; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 580 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 581 | /* Set flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 582 | memcpy(p, (char *)&flags, 4); |
| 583 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 584 | |
| 585 | /* Set stream-id and frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 586 | if (encode_varint(stream_id, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 587 | goto too_big; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 588 | if (encode_varint(frame_id, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 589 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 590 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 591 | if (ctx == NULL) |
| 592 | goto end; |
| 593 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 594 | /* Copy encoded messages, if possible */ |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 595 | sz = ctx->buffer->i; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 596 | if (p + sz >= end) |
| 597 | goto too_big; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 598 | memcpy(p, ctx->buffer->p, sz); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 599 | p += sz; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 600 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 601 | end: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 602 | return (p - frame); |
| 603 | |
| 604 | too_big: |
| 605 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 606 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 607 | } |
| 608 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 609 | /* Decode and process the HELLO frame sent by an agent. It returns the number of |
| 610 | * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal |
| 611 | * error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 612 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 613 | spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 614 | { |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 615 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
| 616 | char *p, *end; |
| 617 | int vsn, max_frame_size; |
| 618 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 619 | |
| 620 | p = frame; |
| 621 | end = frame + size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 622 | |
| 623 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 624 | if (*p++ != SPOE_FRM_T_AGENT_HELLO) { |
| 625 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 626 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 627 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 628 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 629 | if (size < 7 /* TYPE + METADATA */) { |
| 630 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 631 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 632 | } |
| 633 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 634 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 635 | memcpy((char *)&flags, p, 4); |
| 636 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 637 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 638 | /* Fragmentation is not supported for HELLO frame */ |
| 639 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 640 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 641 | return -1; |
| 642 | } |
| 643 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 644 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 645 | if (*p != 0 || *(p+1) != 0) { |
| 646 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 647 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 648 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 649 | p += 2; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 650 | |
| 651 | /* There are 3 mandatory items: "version", "max-frame-size" and |
| 652 | * "capabilities" */ |
| 653 | |
| 654 | /* Loop on K/V items */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 655 | vsn = max_frame_size = flags = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 656 | while (p < end) { |
| 657 | char *str; |
| 658 | size_t sz; |
| 659 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 660 | |
| 661 | /* Decode the item key */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 662 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 663 | if (ret == -1 || !sz) { |
| 664 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 665 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 666 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 667 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 668 | /* Check "version" K/V item */ |
| 669 | if (!memcmp(str, VERSION_KEY, sz)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 670 | int i, type = *p++; |
| 671 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 672 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 673 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 674 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 675 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 676 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 677 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) { |
| 678 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 679 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 680 | } |
| 681 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 682 | vsn = spoe_str_to_vsn(str, sz); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 683 | if (vsn == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 684 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 685 | return -1; |
| 686 | } |
| 687 | for (i = 0; supported_versions[i].str != NULL; ++i) { |
| 688 | if (vsn >= supported_versions[i].min && |
| 689 | vsn <= supported_versions[i].max) |
| 690 | break; |
| 691 | } |
| 692 | if (supported_versions[i].str == NULL) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 693 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 694 | return -1; |
| 695 | } |
| 696 | } |
| 697 | /* Check "max-frame-size" K/V item */ |
| 698 | else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 699 | int type = *p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 700 | |
| 701 | /* The value must be integer */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 702 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 703 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 704 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 705 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 706 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 707 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 708 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 709 | if (decode_varint(&p, end, &sz) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 710 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 711 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 712 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 713 | if (sz < MIN_FRAME_SIZE || |
| 714 | sz > SPOE_APPCTX(appctx)->max_frame_size) { |
| 715 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 716 | return -1; |
| 717 | } |
| 718 | max_frame_size = sz; |
| 719 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 720 | /* Check "capabilities" K/V item */ |
| 721 | else if (!memcmp(str, CAPABILITIES_KEY, sz)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 722 | int type = *p++; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 723 | |
| 724 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 725 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 726 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 727 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 728 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 729 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) { |
| 730 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 731 | return 0; |
| 732 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 733 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 734 | while (sz) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 735 | char *delim; |
| 736 | |
| 737 | /* Skip leading spaces */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 738 | for (; isspace(*str) && sz; str++, sz--); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 739 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 740 | if (sz >= 10 && !strncmp(str, "pipelining", 10)) { |
| 741 | str += 10; sz -= 10; |
| 742 | if (!sz || isspace(*str) || *str == ',') |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 743 | flags |= SPOE_APPCTX_FL_PIPELINING; |
| 744 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 745 | else if (sz >= 5 && !strncmp(str, "async", 5)) { |
| 746 | str += 5; sz -= 5; |
| 747 | if (!sz || isspace(*str) || *str == ',') |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 748 | flags |= SPOE_APPCTX_FL_ASYNC; |
| 749 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 750 | else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) { |
| 751 | str += 13; sz -= 13; |
| 752 | if (!sz || isspace(*str) || *str == ',') |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 753 | flags |= SPOE_APPCTX_FL_FRAGMENTATION; |
| 754 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 755 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 756 | /* Get the next comma or break */ |
| 757 | if (!sz || (delim = memchr(str, ',', sz)) == NULL) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 758 | break; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 759 | delim++; |
| 760 | sz -= (delim - str); |
| 761 | str = delim; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 762 | } |
| 763 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 764 | else { |
| 765 | /* Silently ignore unknown item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 766 | if (spoe_skip_data(&p, end) == -1) { |
| 767 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 768 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 769 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
| 773 | /* Final checks */ |
| 774 | if (!vsn) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 775 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 776 | return -1; |
| 777 | } |
| 778 | if (!max_frame_size) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 779 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 780 | return -1; |
| 781 | } |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 782 | if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING)) |
| 783 | flags &= ~SPOE_APPCTX_FL_PIPELINING; |
| 784 | if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC)) |
| 785 | flags &= ~SPOE_APPCTX_FL_ASYNC; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 786 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 787 | SPOE_APPCTX(appctx)->version = (unsigned int)vsn; |
| 788 | SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size; |
| 789 | SPOE_APPCTX(appctx)->flags |= flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 790 | |
| 791 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /* Decode DISCONNECT frame sent by an agent. It returns the number of by read |
| 795 | * bytes on success, 0 if the frame can be ignored and -1 if an error |
| 796 | * occurred. */ |
| 797 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 798 | spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 799 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 800 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 801 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 802 | |
| 803 | p = frame; |
| 804 | end = frame + size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 805 | |
| 806 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 807 | if (*p++ != SPOE_FRM_T_AGENT_DISCON) { |
| 808 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 809 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 810 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 811 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 812 | if (size < 7 /* TYPE + METADATA */) { |
| 813 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 814 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 815 | } |
| 816 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 817 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 818 | memcpy((char *)&flags, p, 4); |
| 819 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 820 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 821 | /* Fragmentation is not supported for DISCONNECT frame */ |
| 822 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 823 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 824 | return -1; |
| 825 | } |
| 826 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 827 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 828 | if (*p != 0 || *(p+1) != 0) { |
| 829 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 830 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 831 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 832 | p += 2; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 833 | |
| 834 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 835 | |
| 836 | /* Loop on K/V items */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 837 | while (p < end) { |
| 838 | char *str; |
| 839 | size_t sz; |
| 840 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 841 | |
| 842 | /* Decode the item key */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 843 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 844 | if (ret == -1 || !sz) { |
| 845 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 846 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /* Check "status-code" K/V item */ |
| 850 | if (!memcmp(str, STATUS_CODE_KEY, sz)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 851 | int type = *p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 852 | |
| 853 | /* The value must be an integer */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 854 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 855 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 856 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 857 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 858 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 859 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 860 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 861 | if (decode_varint(&p, end, &sz) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 862 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 863 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 864 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 865 | SPOE_APPCTX(appctx)->status_code = sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | /* Check "message" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 869 | else if (!memcmp(str, MSG_KEY, sz)) { |
| 870 | int type = *p++; |
| 871 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 872 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 873 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 874 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 875 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 876 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 877 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 878 | if (ret == -1 || sz > 255) { |
| 879 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 880 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 881 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 882 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 883 | SPOE_APPCTX(appctx)->reason = str; |
| 884 | SPOE_APPCTX(appctx)->rlen = sz; |
| 885 | #endif |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 886 | } |
| 887 | else { |
| 888 | /* Silently ignore unknown item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 889 | if (spoe_skip_data(&p, end) == -1) { |
| 890 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 891 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 892 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 893 | } |
| 894 | } |
| 895 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 896 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 900 | /* Decode ACK frame sent by an agent. It returns the number of read bytes on |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 901 | * success, 0 if the frame can be ignored and -1 if an error occurred. */ |
| 902 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 903 | spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 904 | char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 905 | { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 906 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 907 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 908 | uint64_t stream_id, frame_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 909 | int len; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 910 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 911 | |
| 912 | p = frame; |
| 913 | end = frame + size; |
| 914 | *ctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 915 | |
| 916 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 917 | if (*p++ != SPOE_FRM_T_AGENT_ACK) { |
| 918 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 919 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 920 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 921 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 922 | if (size < 7 /* TYPE + METADATA */) { |
| 923 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 924 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 925 | } |
| 926 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 927 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 928 | memcpy((char *)&flags, p, 4); |
| 929 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 930 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 931 | /* Fragmentation is not supported for now */ |
| 932 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 933 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 934 | return -1; |
| 935 | } |
| 936 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 937 | /* Get the stream-id and the frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 938 | if (decode_varint(&p, end, &stream_id) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 939 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 940 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 941 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 942 | if (decode_varint(&p, end, &frame_id) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 943 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 944 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 945 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 946 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 947 | /* Try to find the corresponding SPOE context */ |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 948 | if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 949 | list_for_each_entry((*ctx), &agent->waiting_queue, list) { |
| 950 | if ((*ctx)->stream_id == (unsigned int)stream_id && |
| 951 | (*ctx)->frame_id == (unsigned int)frame_id) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 952 | goto found; |
| 953 | } |
| 954 | } |
| 955 | else { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 956 | list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) { |
| 957 | if ((*ctx)->stream_id == (unsigned int)stream_id && |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 958 | (*ctx)->frame_id == (unsigned int)frame_id) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 959 | goto found; |
| 960 | } |
| 961 | } |
| 962 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 963 | if (SPOE_APPCTX(appctx)->frag_ctx.ctx && |
| 964 | SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id && |
| 965 | SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) { |
| 966 | |
| 967 | /* ABRT bit is set for an unfinished fragmented frame */ |
| 968 | if (flags & SPOE_FRM_FL_ABRT) { |
| 969 | *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx; |
| 970 | (*ctx)->frag_ctx.spoe_appctx = NULL; |
| 971 | (*ctx)->state = SPOE_CTX_ST_ERROR; |
| 972 | (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT; |
| 973 | /* Ignore the payload */ |
| 974 | goto end; |
| 975 | } |
| 976 | /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */ |
| 977 | /* For now, we ignore the ack */ |
| 978 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 979 | return 0; |
| 980 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 981 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 982 | /* No Stream found, ignore the frame */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 983 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 984 | " - Ignore ACK frame" |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 985 | " - stream-id=%u - frame-id=%u\n", |
| 986 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 987 | __FUNCTION__, appctx, |
| 988 | (unsigned int)stream_id, (unsigned int)frame_id); |
| 989 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 990 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 991 | return 0; |
| 992 | |
| 993 | found: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 994 | if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer, |
| 995 | &SPOE_APPCTX(appctx)->buffer_wait)) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 996 | *ctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 997 | return 1; /* Retry later */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 998 | } |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 999 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1000 | /* Copy encoded actions */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1001 | len = (end - p); |
| 1002 | memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len); |
| 1003 | SPOE_APPCTX(appctx)->buffer->i = len; |
| 1004 | p += len; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1005 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1006 | /* Transfer the buffer ownership to the SPOE context */ |
| 1007 | (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer; |
| 1008 | SPOE_APPCTX(appctx)->buffer = &buf_empty; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1009 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1010 | (*ctx)->state = SPOE_CTX_ST_DONE; |
| 1011 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1012 | end: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1013 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1014 | " - ACK frame received" |
| 1015 | " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1016 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1017 | __FUNCTION__, appctx, *ctx, (*ctx)->stream_id, |
| 1018 | (*ctx)->frame_id, flags); |
| 1019 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1020 | } |
| 1021 | |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1022 | /* This function is used in cfgparse.c and declared in proto/checks.h. It |
| 1023 | * prepare the request to send to agents during a healthcheck. It returns 0 on |
| 1024 | * success and -1 if an error occurred. */ |
| 1025 | int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1026 | spoe_prepare_healthcheck_request(char **req, int *len) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1027 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1028 | struct appctx appctx; |
| 1029 | struct spoe_appctx spoe_appctx; |
| 1030 | char *frame, *end, buf[MAX_FRAME_SIZE+4]; |
| 1031 | size_t sz; |
| 1032 | int ret; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1033 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1034 | memset(&appctx, 0, sizeof(appctx)); |
| 1035 | memset(&spoe_appctx, 0, sizeof(spoe_appctx)); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1036 | memset(buf, 0, sizeof(buf)); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1037 | |
| 1038 | appctx.ctx.spoe.ptr = &spoe_appctx; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1039 | SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1040 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1041 | frame = buf+4; /* Reserved the 4 first bytes for the frame size */ |
| 1042 | end = frame + MAX_FRAME_SIZE; |
| 1043 | |
| 1044 | ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE); |
| 1045 | if (ret <= 0) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1046 | return -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1047 | frame += ret; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1048 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1049 | /* Add "healthcheck" K/V item */ |
| 1050 | sz = SLEN(HEALTHCHECK_KEY); |
| 1051 | if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1) |
| 1052 | return -1; |
| 1053 | *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1054 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1055 | *len = frame - buf; |
| 1056 | sz = htonl(*len - 4); |
| 1057 | memcpy(buf, (char *)&sz, 4); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1058 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1059 | if ((*req = malloc(*len)) == NULL) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1060 | return -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1061 | memcpy(*req, buf, *len); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | /* This function is used in checks.c and declared in proto/checks.h. It decode |
| 1066 | * the response received from an agent during a healthcheck. It returns 0 on |
| 1067 | * success and -1 if an error occurred. */ |
| 1068 | int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1069 | spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1070 | { |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1071 | struct appctx appctx; |
| 1072 | struct spoe_appctx spoe_appctx; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1073 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1074 | memset(&appctx, 0, sizeof(appctx)); |
| 1075 | memset(&spoe_appctx, 0, sizeof(spoe_appctx)); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1076 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1077 | appctx.ctx.spoe.ptr = &spoe_appctx; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1078 | SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1079 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1080 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1081 | spoe_handle_agentdiscon_frame(&appctx, frame, size); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1082 | goto error; |
| 1083 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1084 | if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0) |
| 1085 | goto error; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1086 | |
| 1087 | return 0; |
| 1088 | |
| 1089 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1090 | if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS) |
| 1091 | SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN; |
| 1092 | strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1093 | return -1; |
| 1094 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1095 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1096 | /* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when |
| 1097 | * the frame can be ignored, 1 to retry later, and the frame legnth on |
| 1098 | * success. */ |
| 1099 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1100 | spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1101 | { |
| 1102 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1103 | int ret; |
| 1104 | uint32_t netint; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1105 | |
| 1106 | if (si_ic(si)->buf == &buf_empty) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1107 | goto retry; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1108 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1109 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1110 | * length. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1111 | netint = htonl(framesz); |
| 1112 | memcpy(buf, (char *)&netint, 4); |
| 1113 | ret = bi_putblk(si_ic(si), buf, framesz+4); |
| 1114 | |
| 1115 | if (ret <= 0) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1116 | if (ret == -1) { |
| 1117 | retry: |
| 1118 | si_applet_cant_put(si); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1119 | return 1; /* retry */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1120 | } |
| 1121 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1122 | return -1; /* error */ |
| 1123 | } |
| 1124 | return framesz; |
| 1125 | } |
| 1126 | |
| 1127 | /* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0 |
| 1128 | * when the frame can be ignored, 1 to retry later and the frame length on |
| 1129 | * success. */ |
| 1130 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1131 | spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1132 | { |
| 1133 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1134 | int ret; |
| 1135 | uint32_t netint; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1136 | |
| 1137 | if (si_oc(si)->buf == &buf_empty) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1138 | goto retry; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1139 | |
| 1140 | ret = bo_getblk(si_oc(si), (char *)&netint, 4, 0); |
| 1141 | if (ret > 0) { |
| 1142 | framesz = ntohl(netint); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1143 | if (framesz > SPOE_APPCTX(appctx)->max_frame_size) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1144 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1145 | return -1; |
| 1146 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1147 | ret = bo_getblk(si_oc(si), buf, framesz, 4); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1148 | } |
| 1149 | if (ret <= 0) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1150 | if (ret == 0) { |
| 1151 | retry: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1152 | return 1; /* retry */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1153 | } |
| 1154 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1155 | return -1; /* error */ |
| 1156 | } |
| 1157 | return framesz; |
| 1158 | } |
| 1159 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1160 | /******************************************************************** |
| 1161 | * Functions that manage the SPOE applet |
| 1162 | ********************************************************************/ |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1163 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1164 | spoe_wakeup_appctx(struct appctx *appctx) |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1165 | { |
| 1166 | si_applet_want_get(appctx->owner); |
| 1167 | si_applet_want_put(appctx->owner); |
| 1168 | appctx_wakeup(appctx); |
| 1169 | return 1; |
| 1170 | } |
| 1171 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1172 | /* Callback function that catches applet timeouts. If a timeout occurred, we set |
| 1173 | * <appctx->st1> flag and the SPOE applet is woken up. */ |
| 1174 | static struct task * |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1175 | spoe_process_appctx(struct task * task) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1176 | { |
| 1177 | struct appctx *appctx = task->context; |
| 1178 | |
| 1179 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1180 | if (tick_is_expired(task->expire, now_ms)) { |
| 1181 | task->expire = TICK_ETERNITY; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1182 | appctx->st1 = SPOE_APPCTX_ERR_TOUT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1183 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1184 | spoe_wakeup_appctx(appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1185 | return task; |
| 1186 | } |
| 1187 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1188 | /* Callback function that releases a SPOE applet. This happens when the |
| 1189 | * connection with the agent is closed. */ |
| 1190 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1191 | spoe_release_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1192 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1193 | struct stream_interface *si = appctx->owner; |
| 1194 | struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx); |
| 1195 | struct spoe_agent *agent; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1196 | struct spoe_context *ctx, *back; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1197 | |
| 1198 | if (spoe_appctx == NULL) |
| 1199 | return; |
| 1200 | |
| 1201 | appctx->ctx.spoe.ptr = NULL; |
| 1202 | agent = spoe_appctx->agent; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1203 | |
| 1204 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n", |
| 1205 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1206 | __FUNCTION__, appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1207 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1208 | /* Remove applet from the list of running applets */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1209 | agent->applets_act--; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1210 | if (!LIST_ISEMPTY(&spoe_appctx->list)) { |
| 1211 | LIST_DEL(&spoe_appctx->list); |
| 1212 | LIST_INIT(&spoe_appctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1213 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1214 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1215 | /* Shutdown the server connection, if needed */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1216 | if (appctx->st0 != SPOE_APPCTX_ST_END) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1217 | if (appctx->st0 == SPOE_APPCTX_ST_IDLE) |
| 1218 | agent->applets_idle--; |
| 1219 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1220 | appctx->st0 = SPOE_APPCTX_ST_END; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1221 | if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE) |
| 1222 | spoe_appctx->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1223 | |
| 1224 | si_shutw(si); |
| 1225 | si_shutr(si); |
| 1226 | si_ic(si)->flags |= CF_READ_NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1227 | } |
| 1228 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1229 | /* Destroy the task attached to this applet */ |
| 1230 | if (spoe_appctx->task) { |
| 1231 | task_delete(spoe_appctx->task); |
| 1232 | task_free(spoe_appctx->task); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1233 | } |
| 1234 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1235 | /* Notify all waiting streams */ |
| 1236 | list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1237 | LIST_DEL(&ctx->list); |
| 1238 | LIST_INIT(&ctx->list); |
| 1239 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1240 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1241 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1242 | } |
| 1243 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1244 | /* If the applet was processing a fragmented frame, notify the |
| 1245 | * corresponding stream. */ |
| 1246 | if (spoe_appctx->frag_ctx.ctx) { |
| 1247 | ctx = spoe_appctx->frag_ctx.ctx; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1248 | ctx->frag_ctx.spoe_appctx = NULL; |
| 1249 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1250 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1251 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1252 | } |
| 1253 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1254 | /* Release allocated memory */ |
| 1255 | spoe_release_buffer(&spoe_appctx->buffer, |
| 1256 | &spoe_appctx->buffer_wait); |
| 1257 | pool_free2(pool2_spoe_appctx, spoe_appctx); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1258 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1259 | if (!LIST_ISEMPTY(&agent->applets)) |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1260 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1261 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1262 | /* If this was the last running applet, notify all waiting streams */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1263 | list_for_each_entry_safe(ctx, back, &agent->sending_queue, list) { |
| 1264 | LIST_DEL(&ctx->list); |
| 1265 | LIST_INIT(&ctx->list); |
| 1266 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1267 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1268 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1269 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1270 | list_for_each_entry_safe(ctx, back, &agent->waiting_queue, list) { |
| 1271 | LIST_DEL(&ctx->list); |
| 1272 | LIST_INIT(&ctx->list); |
| 1273 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1274 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1275 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1276 | } |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1277 | |
| 1278 | end: |
| 1279 | /* Update runtinme agent info */ |
| 1280 | agent->frame_size = agent->max_frame_size; |
| 1281 | list_for_each_entry(spoe_appctx, &agent->applets, list) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1282 | agent->frame_size = MIN(spoe_appctx->max_frame_size, |
| 1283 | agent->frame_size); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1284 | } |
| 1285 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1286 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1287 | spoe_handle_connect_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1288 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1289 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1290 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1291 | char *frame, *buf; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1292 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1293 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1294 | if (si->state <= SI_ST_CON) { |
| 1295 | si_applet_want_put(si); |
| 1296 | task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG); |
| 1297 | goto stop; |
| 1298 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1299 | if (si->state != SI_ST_EST) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1300 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1301 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1302 | } |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1303 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1304 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1305 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1306 | " - Connection timed out\n", |
| 1307 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1308 | __FUNCTION__, appctx); |
| 1309 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1310 | goto exit; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1311 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1312 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1313 | if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1314 | SPOE_APPCTX(appctx)->task->expire = |
| 1315 | tick_add_ifset(now_ms, agent->timeout.hello); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1316 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1317 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1318 | * length. */ |
| 1319 | buf = trash.str; frame = buf+4; |
| 1320 | ret = spoe_prepare_hahello_frame(appctx, frame, |
| 1321 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1322 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1323 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1324 | |
| 1325 | switch (ret) { |
| 1326 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1327 | case 0: /* ignore => an error, cannot be ignored */ |
| 1328 | goto exit; |
| 1329 | |
| 1330 | case 1: /* retry later */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1331 | goto stop; |
| 1332 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1333 | default: |
| 1334 | /* HELLO frame successfully sent, now wait for the |
| 1335 | * reply. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1336 | appctx->st0 = SPOE_APPCTX_ST_CONNECTING; |
| 1337 | goto next; |
| 1338 | } |
| 1339 | |
| 1340 | next: |
| 1341 | return 0; |
| 1342 | stop: |
| 1343 | return 1; |
| 1344 | exit: |
| 1345 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1346 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1347 | } |
| 1348 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1349 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1350 | spoe_handle_connecting_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1351 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1352 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1353 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1354 | char *frame; |
| 1355 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1356 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1357 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1358 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1359 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1360 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1361 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1362 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1363 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1364 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1365 | " - Connection timed out\n", |
| 1366 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1367 | __FUNCTION__, appctx); |
| 1368 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1369 | goto exit; |
| 1370 | } |
| 1371 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1372 | frame = trash.str; trash.len = 0; |
| 1373 | ret = spoe_recv_frame(appctx, frame, |
| 1374 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1375 | if (ret > 1) { |
| 1376 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1377 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1378 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1379 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1380 | trash.len = ret + 4; |
| 1381 | ret = spoe_handle_agenthello_frame(appctx, frame, ret); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1382 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1383 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1384 | switch (ret) { |
| 1385 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1386 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1387 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1388 | goto next; |
| 1389 | |
| 1390 | case 1: /* retry later */ |
| 1391 | goto stop; |
| 1392 | |
| 1393 | default: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1394 | /* HELLO handshake is finished, set the idle timeout and |
| 1395 | * add the applet in the list of running applets. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1396 | agent->applets_idle++; |
| 1397 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1398 | LIST_DEL(&SPOE_APPCTX(appctx)->list); |
| 1399 | LIST_ADD(&agent->applets, &SPOE_APPCTX(appctx)->list); |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1400 | |
| 1401 | /* Update runtinme agent info */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1402 | agent->frame_size = MIN(SPOE_APPCTX(appctx)->max_frame_size, |
| 1403 | agent->frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1404 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1405 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1406 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1407 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1408 | /* Do not forget to remove processed frame from the output buffer */ |
| 1409 | if (trash.len) |
| 1410 | bo_skip(si_oc(si), trash.len); |
| 1411 | |
| 1412 | SPOE_APPCTX(appctx)->task->expire = |
| 1413 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1414 | return 0; |
| 1415 | stop: |
| 1416 | return 1; |
| 1417 | exit: |
| 1418 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1419 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1420 | } |
| 1421 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1422 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1423 | static int |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1424 | spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1425 | { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1426 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
| 1427 | struct spoe_context *ctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1428 | char *frame, *buf; |
| 1429 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1430 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1431 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1432 | * length. */ |
| 1433 | buf = trash.str; frame = buf+4; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1434 | |
| 1435 | if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) { |
| 1436 | ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx; |
| 1437 | ret = spoe_prepare_hafrag_frame(appctx, ctx, frame, |
| 1438 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1439 | } |
| 1440 | else if (LIST_ISEMPTY(&agent->sending_queue)) { |
| 1441 | *skip = 1; |
| 1442 | ret = 1; |
| 1443 | goto end; |
| 1444 | } |
| 1445 | else { |
| 1446 | ctx = LIST_NEXT(&agent->sending_queue, typeof(ctx), list); |
| 1447 | ret = spoe_prepare_hanotify_frame(appctx, ctx, frame, |
| 1448 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1449 | |
| 1450 | } |
| 1451 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1452 | if (ret > 1) |
| 1453 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1454 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1455 | switch (ret) { |
| 1456 | case -1: /* error */ |
| 1457 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1458 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1459 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1460 | case 0: /* ignore */ |
| 1461 | if (ctx == NULL) |
| 1462 | goto abort_frag_frame; |
| 1463 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1464 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1465 | LIST_DEL(&ctx->list); |
| 1466 | LIST_INIT(&ctx->list); |
| 1467 | ctx->state = SPOE_CTX_ST_ERROR; |
| 1468 | ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100); |
| 1469 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1470 | break; |
| 1471 | |
| 1472 | case 1: /* retry */ |
| 1473 | *skip = 1; |
| 1474 | break; |
| 1475 | |
| 1476 | default: |
| 1477 | if (ctx == NULL) |
| 1478 | goto abort_frag_frame; |
| 1479 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1480 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1481 | LIST_DEL(&ctx->list); |
| 1482 | LIST_INIT(&ctx->list); |
| 1483 | if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) || |
| 1484 | (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN)) |
| 1485 | goto no_frag_frame_sent; |
| 1486 | else { |
| 1487 | *skip = 1; |
| 1488 | goto frag_frame_sent; |
| 1489 | } |
| 1490 | } |
| 1491 | goto end; |
| 1492 | |
| 1493 | frag_frame_sent: |
| 1494 | appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY; |
| 1495 | SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx; |
| 1496 | SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id; |
| 1497 | SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id; |
| 1498 | |
| 1499 | ctx->frag_ctx.spoe_appctx = SPOE_APPCTX(appctx); |
| 1500 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
| 1501 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1502 | goto end; |
| 1503 | |
| 1504 | no_frag_frame_sent: |
| 1505 | if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) { |
| 1506 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1507 | LIST_ADDQ(&agent->waiting_queue, &ctx->list); |
| 1508 | } |
| 1509 | else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) { |
| 1510 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1511 | LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list); |
| 1512 | } |
| 1513 | else { |
| 1514 | appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK; |
| 1515 | LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list); |
| 1516 | } |
| 1517 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1518 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1519 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1520 | |
| 1521 | ctx->frag_ctx.spoe_appctx = NULL; |
| 1522 | ctx->state = SPOE_CTX_ST_WAITING_ACK; |
| 1523 | goto end; |
| 1524 | |
| 1525 | abort_frag_frame: |
| 1526 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1527 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1528 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1529 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1530 | goto end; |
| 1531 | |
| 1532 | end: |
| 1533 | return ret; |
| 1534 | } |
| 1535 | |
| 1536 | static int |
| 1537 | spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip) |
| 1538 | { |
| 1539 | struct spoe_context *ctx = NULL; |
| 1540 | char *frame; |
| 1541 | int ret; |
| 1542 | |
| 1543 | frame = trash.str; trash.len = 0; |
| 1544 | ret = spoe_recv_frame(appctx, frame, |
| 1545 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1546 | if (ret > 1) { |
| 1547 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1548 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1549 | goto end; |
| 1550 | } |
| 1551 | trash.len = ret + 4; |
| 1552 | ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret); |
| 1553 | } |
| 1554 | switch (ret) { |
| 1555 | case -1: /* error */ |
| 1556 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1557 | break; |
| 1558 | |
| 1559 | case 0: /* ignore */ |
| 1560 | break; |
| 1561 | |
| 1562 | case 1: /* retry */ |
| 1563 | *skip = 1; |
| 1564 | break; |
| 1565 | |
| 1566 | default: |
| 1567 | LIST_DEL(&ctx->list); |
| 1568 | LIST_INIT(&ctx->list); |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1569 | |
| 1570 | if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY && |
| 1571 | ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) { |
| 1572 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1573 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1574 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1575 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1576 | } |
| 1577 | else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) |
| 1578 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1579 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1580 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1581 | break; |
| 1582 | } |
| 1583 | |
| 1584 | /* Do not forget to remove processed frame from the output buffer */ |
| 1585 | if (trash.len) |
| 1586 | bo_skip(si_oc(appctx->owner), trash.len); |
| 1587 | end: |
| 1588 | return ret; |
| 1589 | } |
| 1590 | |
| 1591 | static int |
| 1592 | spoe_handle_processing_appctx(struct appctx *appctx) |
| 1593 | { |
| 1594 | struct stream_interface *si = appctx->owner; |
| 1595 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1596 | unsigned int fpa = 0; |
| 1597 | int ret, skip_sending = 0, skip_receiving = 0; |
| 1598 | |
| 1599 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) { |
| 1600 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
| 1601 | goto exit; |
| 1602 | } |
| 1603 | |
| 1604 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
| 1605 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
| 1606 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1607 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1608 | goto next; |
| 1609 | } |
| 1610 | |
| 1611 | process: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1612 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1613 | " - process: fpa=%u/%u - skip_sending=%d - skip_receiving=%d" |
| 1614 | " - appctx-state=%s\n", |
| 1615 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1616 | __FUNCTION__, appctx, fpa, agent->max_fpa, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1617 | skip_sending, skip_receiving, |
| 1618 | spoe_appctx_state_str[appctx->st0]); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1619 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1620 | if (fpa > agent->max_fpa) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1621 | goto stop; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1622 | else if (skip_sending || appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1623 | if (skip_receiving) |
| 1624 | goto stop; |
| 1625 | goto recv_frame; |
| 1626 | } |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1627 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1628 | /* send_frame */ |
| 1629 | ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1630 | switch (ret) { |
| 1631 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1632 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1633 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1634 | case 0: /* ignore */ |
| 1635 | agent->sending_rate++; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1636 | fpa++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1637 | break; |
| 1638 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1639 | case 1: /* retry */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1640 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1641 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1642 | default: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1643 | agent->sending_rate++; |
| 1644 | fpa++; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1645 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1646 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1647 | if (fpa > agent->max_fpa) |
| 1648 | goto stop; |
| 1649 | |
| 1650 | recv_frame: |
| 1651 | if (skip_receiving) |
| 1652 | goto process; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1653 | ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1654 | switch (ret) { |
| 1655 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1656 | goto next; |
| 1657 | |
| 1658 | case 0: /* ignore */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1659 | fpa++; |
| 1660 | break; |
| 1661 | |
| 1662 | case 1: /* retry */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1663 | break; |
| 1664 | |
| 1665 | default: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1666 | fpa++; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1667 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1668 | } |
| 1669 | goto process; |
| 1670 | |
| 1671 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1672 | SPOE_APPCTX(appctx)->task->expire = |
| 1673 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1674 | return 0; |
| 1675 | stop: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1676 | if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1677 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1678 | agent->applets_idle++; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1679 | } |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1680 | if (fpa || (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PERSIST)) { |
| 1681 | LIST_DEL(&SPOE_APPCTX(appctx)->list); |
| 1682 | LIST_ADD(&agent->applets, &SPOE_APPCTX(appctx)->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1683 | if (fpa) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1684 | SPOE_APPCTX(appctx)->task->expire = |
| 1685 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1686 | } |
| 1687 | return 1; |
| 1688 | |
| 1689 | exit: |
| 1690 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1691 | return 0; |
| 1692 | } |
| 1693 | |
| 1694 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1695 | spoe_handle_disconnect_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1696 | { |
| 1697 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1698 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1699 | char *frame, *buf; |
| 1700 | int ret; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1701 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1702 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) |
| 1703 | goto exit; |
| 1704 | |
| 1705 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) |
| 1706 | goto exit; |
| 1707 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1708 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1709 | * length. */ |
| 1710 | buf = trash.str; frame = buf+4; |
| 1711 | ret = spoe_prepare_hadiscon_frame(appctx, frame, |
| 1712 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1713 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1714 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1715 | |
| 1716 | switch (ret) { |
| 1717 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1718 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1719 | goto exit; |
| 1720 | |
| 1721 | case 1: /* retry */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1722 | goto stop; |
| 1723 | |
| 1724 | default: |
| 1725 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1726 | " - disconnected by HAProxy (%d): %s\n", |
| 1727 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1728 | __FUNCTION__, appctx, |
| 1729 | SPOE_APPCTX(appctx)->status_code, |
| 1730 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1731 | |
| 1732 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1733 | goto next; |
| 1734 | } |
| 1735 | |
| 1736 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1737 | SPOE_APPCTX(appctx)->task->expire = |
| 1738 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1739 | return 0; |
| 1740 | stop: |
| 1741 | return 1; |
| 1742 | exit: |
| 1743 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1744 | return 0; |
| 1745 | } |
| 1746 | |
| 1747 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1748 | spoe_handle_disconnecting_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1749 | { |
| 1750 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1751 | char *frame; |
| 1752 | int ret; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1753 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1754 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1755 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1756 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1757 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1758 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1759 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1760 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1761 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1762 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1763 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1764 | frame = trash.str; trash.len = 0; |
| 1765 | ret = spoe_recv_frame(appctx, frame, |
| 1766 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1767 | if (ret > 1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1768 | trash.len = ret + 4; |
| 1769 | ret = spoe_handle_agentdiscon_frame(appctx, frame, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | switch (ret) { |
| 1773 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1774 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1775 | " - error on frame (%s)\n", |
| 1776 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1777 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1778 | __FUNCTION__, appctx, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1779 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1780 | goto exit; |
| 1781 | |
| 1782 | case 0: /* ignore */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1783 | goto next; |
| 1784 | |
| 1785 | case 1: /* retry */ |
| 1786 | goto stop; |
| 1787 | |
| 1788 | default: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1789 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1790 | " - disconnected by peer (%d): %.*s\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1791 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1792 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1793 | __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code, |
| 1794 | SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1795 | goto exit; |
| 1796 | } |
| 1797 | |
| 1798 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1799 | /* Do not forget to remove processed frame from the output buffer */ |
| 1800 | if (trash.len) |
| 1801 | bo_skip(si_oc(appctx->owner), trash.len); |
| 1802 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1803 | return 0; |
| 1804 | stop: |
| 1805 | return 1; |
| 1806 | exit: |
| 1807 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1808 | return 0; |
| 1809 | } |
| 1810 | |
| 1811 | /* I/O Handler processing messages exchanged with the agent */ |
| 1812 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1813 | spoe_handle_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1814 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1815 | struct stream_interface *si = appctx->owner; |
| 1816 | struct spoe_agent *agent; |
| 1817 | |
| 1818 | if (SPOE_APPCTX(appctx) == NULL) |
| 1819 | return; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1820 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1821 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
| 1822 | agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1823 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1824 | switchstate: |
| 1825 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1826 | " - appctx-state=%s\n", |
| 1827 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1828 | __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]); |
| 1829 | |
| 1830 | switch (appctx->st0) { |
| 1831 | case SPOE_APPCTX_ST_CONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1832 | if (spoe_handle_connect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1833 | goto out; |
| 1834 | goto switchstate; |
| 1835 | |
| 1836 | case SPOE_APPCTX_ST_CONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1837 | if (spoe_handle_connecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1838 | goto out; |
| 1839 | goto switchstate; |
| 1840 | |
| 1841 | case SPOE_APPCTX_ST_IDLE: |
| 1842 | if (stopping && |
| 1843 | LIST_ISEMPTY(&agent->sending_queue) && |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1844 | LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1845 | SPOE_APPCTX(appctx)->task->expire = |
| 1846 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1847 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1848 | goto switchstate; |
| 1849 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1850 | agent->applets_idle--; |
| 1851 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1852 | /* fall through */ |
| 1853 | |
| 1854 | case SPOE_APPCTX_ST_PROCESSING: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1855 | case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY: |
| 1856 | case SPOE_APPCTX_ST_WAITING_SYNC_ACK: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1857 | if (spoe_handle_processing_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1858 | goto out; |
| 1859 | goto switchstate; |
| 1860 | |
| 1861 | case SPOE_APPCTX_ST_DISCONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1862 | if (spoe_handle_disconnect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1863 | goto out; |
| 1864 | goto switchstate; |
| 1865 | |
| 1866 | case SPOE_APPCTX_ST_DISCONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1867 | if (spoe_handle_disconnecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1868 | goto out; |
| 1869 | goto switchstate; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1870 | |
| 1871 | case SPOE_APPCTX_ST_EXIT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1872 | appctx->st0 = SPOE_APPCTX_ST_END; |
| 1873 | SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY; |
| 1874 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1875 | si_shutw(si); |
| 1876 | si_shutr(si); |
| 1877 | si_ic(si)->flags |= CF_READ_NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1878 | /* fall through */ |
| 1879 | |
| 1880 | case SPOE_APPCTX_ST_END: |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1881 | return; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1882 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1883 | out: |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1884 | if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY) |
| 1885 | task_queue(SPOE_APPCTX(appctx)->task); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1886 | si_oc(si)->flags |= CF_READ_DONTWAIT; |
| 1887 | task_wakeup(si_strm(si)->task, TASK_WOKEN_IO); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | struct applet spoe_applet = { |
| 1891 | .obj_type = OBJ_TYPE_APPLET, |
| 1892 | .name = "<SPOE>", /* used for logging */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1893 | .fct = spoe_handle_appctx, |
| 1894 | .release = spoe_release_appctx, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1895 | }; |
| 1896 | |
| 1897 | /* Create a SPOE applet. On success, the created applet is returned, else |
| 1898 | * NULL. */ |
| 1899 | static struct appctx * |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1900 | spoe_create_appctx(struct spoe_config *conf) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1901 | { |
| 1902 | struct appctx *appctx; |
| 1903 | struct session *sess; |
| 1904 | struct task *task; |
| 1905 | struct stream *strm; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1906 | |
| 1907 | if ((appctx = appctx_new(&spoe_applet)) == NULL) |
| 1908 | goto out_error; |
| 1909 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1910 | appctx->ctx.spoe.ptr = pool_alloc_dirty(pool2_spoe_appctx); |
| 1911 | if (SPOE_APPCTX(appctx) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1912 | goto out_free_appctx; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1913 | memset(appctx->ctx.spoe.ptr, 0, pool2_spoe_appctx->size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1914 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1915 | appctx->st0 = SPOE_APPCTX_ST_CONNECT; |
| 1916 | if ((SPOE_APPCTX(appctx)->task = task_new()) == NULL) |
| 1917 | goto out_free_spoe_appctx; |
| 1918 | |
| 1919 | SPOE_APPCTX(appctx)->owner = appctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1920 | SPOE_APPCTX(appctx)->task->process = spoe_process_appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1921 | SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY; |
| 1922 | SPOE_APPCTX(appctx)->task->context = appctx; |
| 1923 | SPOE_APPCTX(appctx)->agent = conf->agent; |
| 1924 | SPOE_APPCTX(appctx)->version = 0; |
| 1925 | SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size; |
| 1926 | SPOE_APPCTX(appctx)->flags = 0; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1927 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1928 | SPOE_APPCTX(appctx)->buffer = &buf_empty; |
| 1929 | |
| 1930 | LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list); |
| 1931 | SPOE_APPCTX(appctx)->buffer_wait.target = appctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1932 | SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1933 | |
| 1934 | LIST_INIT(&SPOE_APPCTX(appctx)->list); |
| 1935 | LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1936 | |
Willy Tarreau | 5820a36 | 2016-12-22 15:59:02 +0100 | [diff] [blame] | 1937 | sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1938 | if (!sess) |
| 1939 | goto out_free_spoe; |
| 1940 | |
| 1941 | if ((task = task_new()) == NULL) |
| 1942 | goto out_free_sess; |
| 1943 | |
| 1944 | if ((strm = stream_new(sess, task, &appctx->obj_type)) == NULL) |
| 1945 | goto out_free_task; |
| 1946 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1947 | stream_set_backend(strm, conf->agent->b.be); |
| 1948 | |
| 1949 | /* applet is waiting for data */ |
| 1950 | si_applet_cant_get(&strm->si[0]); |
| 1951 | appctx_wakeup(appctx); |
| 1952 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1953 | strm->do_log = NULL; |
| 1954 | strm->res.flags |= CF_READ_DONTWAIT; |
| 1955 | |
| 1956 | conf->agent_fe.feconn++; |
| 1957 | jobs++; |
| 1958 | totalconn++; |
| 1959 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1960 | task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT); |
| 1961 | LIST_ADDQ(&conf->agent->applets, &SPOE_APPCTX(appctx)->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1962 | conf->agent->applets_act++; |
Emeric Brun | 5f77fef | 2017-05-29 15:26:51 +0200 | [diff] [blame] | 1963 | |
| 1964 | task_wakeup(task, TASK_WOKEN_INIT); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1965 | return appctx; |
| 1966 | |
| 1967 | /* Error unrolling */ |
| 1968 | out_free_task: |
| 1969 | task_free(task); |
| 1970 | out_free_sess: |
| 1971 | session_free(sess); |
| 1972 | out_free_spoe: |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1973 | task_free(SPOE_APPCTX(appctx)->task); |
| 1974 | out_free_spoe_appctx: |
| 1975 | pool_free2(pool2_spoe_appctx, SPOE_APPCTX(appctx)); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1976 | out_free_appctx: |
| 1977 | appctx_free(appctx); |
| 1978 | out_error: |
| 1979 | return NULL; |
| 1980 | } |
| 1981 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1982 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1983 | spoe_queue_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1984 | { |
| 1985 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 1986 | struct spoe_agent *agent = conf->agent; |
| 1987 | struct appctx *appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1988 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1989 | unsigned int min_applets; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1990 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1991 | min_applets = min_applets_act(agent); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1992 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1993 | /* Check if we need to create a new SPOE applet or not. */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1994 | if (agent->applets_act >= min_applets && |
| 1995 | agent->applets_idle && |
| 1996 | agent->sending_rate) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1997 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1998 | |
| 1999 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2000 | " - try to create new SPOE appctx\n", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2001 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__, |
| 2002 | ctx->strm); |
| 2003 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2004 | /* Do not try to create a new applet if there is no server up for the |
| 2005 | * agent's backend. */ |
| 2006 | if (!agent->b.be->srv_act && !agent->b.be->srv_bck) { |
| 2007 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2008 | " - cannot create SPOE appctx: no server up\n", |
| 2009 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2010 | __FUNCTION__, ctx->strm); |
| 2011 | goto end; |
| 2012 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2013 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2014 | /* Do not try to create a new applet if we have reached the maximum of |
| 2015 | * connection per seconds */ |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 2016 | if (agent->cps_max > 0) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2017 | if (!freq_ctr_remain(&agent->conn_per_sec, agent->cps_max, 0)) { |
| 2018 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2019 | " - cannot create SPOE appctx: max CPS reached\n", |
| 2020 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2021 | __FUNCTION__, ctx->strm); |
| 2022 | goto end; |
| 2023 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2024 | } |
| 2025 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2026 | appctx = spoe_create_appctx(conf); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2027 | if (appctx == NULL) { |
| 2028 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2029 | " - failed to create SPOE appctx\n", |
| 2030 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2031 | __FUNCTION__, ctx->strm); |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2032 | send_log(ctx->strm->be, LOG_EMERG, |
| 2033 | "SPOE: [%s] failed to create SPOE applet\n", |
| 2034 | agent->id); |
| 2035 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2036 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2037 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2038 | if (agent->applets_act <= min_applets) |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2039 | SPOE_APPCTX(appctx)->flags |= SPOE_APPCTX_FL_PERSIST; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2040 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2041 | /* Increase the per-process number of cumulated connections */ |
| 2042 | if (agent->cps_max > 0) |
| 2043 | update_freq_ctr(&agent->conn_per_sec, 1); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2044 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2045 | end: |
| 2046 | /* The only reason to return an error is when there is no applet */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2047 | if (LIST_ISEMPTY(&agent->applets)) { |
| 2048 | ctx->status_code = SPOE_CTX_ERR_RES; |
| 2049 | return -1; |
| 2050 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2051 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2052 | /* Add the SPOE context in the sending queue and update all running |
| 2053 | * info */ |
| 2054 | LIST_ADDQ(&agent->sending_queue, &ctx->list); |
| 2055 | if (agent->sending_rate) |
| 2056 | agent->sending_rate--; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2057 | |
| 2058 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2059 | " - Add stream in sending queue" |
| 2060 | " - applets_act=%u - applets_idle=%u - sending_rate=%u\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2061 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2062 | ctx->strm, agent->applets_act, agent->applets_idle, |
| 2063 | agent->sending_rate); |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2064 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2065 | /* Finally try to wakeup the first IDLE applet found and move it at the |
| 2066 | * end of the list. */ |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2067 | list_for_each_entry(spoe_appctx, &agent->applets, list) { |
| 2068 | appctx = spoe_appctx->owner; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2069 | if (appctx->st0 == SPOE_APPCTX_ST_IDLE) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2070 | spoe_wakeup_appctx(appctx); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2071 | LIST_DEL(&spoe_appctx->list); |
| 2072 | LIST_ADDQ(&agent->applets, &spoe_appctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2073 | break; |
| 2074 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2075 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2076 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | /*************************************************************************** |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2080 | * Functions that encode SPOE messages |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2081 | **************************************************************************/ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2082 | /* Encode SPOE messages for a specific event. Info in <ctx->frag_ctx>, if any, |
| 2083 | * are used to handle fragmented content. On success it returns 1. If an error |
| 2084 | * occurred, -1 is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2085 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2086 | spoe_encode_messages(struct stream *s, struct spoe_context *ctx, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2087 | struct list *messages, int dir) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2088 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2089 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2090 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2091 | struct spoe_message *msg; |
| 2092 | struct sample *smp; |
| 2093 | struct spoe_arg *arg; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2094 | char *p, *end; |
| 2095 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2096 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2097 | p = ctx->buffer->p; |
| 2098 | end = p + agent->frame_size - FRAME_HDR_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2099 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2100 | /* Resume encoding of a SPOE message */ |
| 2101 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2102 | msg = ctx->frag_ctx.curmsg; |
| 2103 | goto encode_message; |
| 2104 | } |
| 2105 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2106 | /* Loop on messages */ |
| 2107 | list_for_each_entry(msg, messages, list) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2108 | ctx->frag_ctx.curmsg = msg; |
| 2109 | ctx->frag_ctx.curarg = NULL; |
| 2110 | ctx->frag_ctx.curoff = UINT_MAX; |
| 2111 | |
| 2112 | encode_message: |
| 2113 | /* Resume encoding of a SPOE argument */ |
| 2114 | if (ctx->frag_ctx.curarg != NULL) { |
| 2115 | arg = ctx->frag_ctx.curarg; |
| 2116 | goto encode_argument; |
| 2117 | } |
| 2118 | |
| 2119 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2120 | goto encode_msg_payload; |
| 2121 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2122 | /* Check if there is enough space for the message name and the |
| 2123 | * number of arguments. It implies <msg->id_len> is encoded on 2 |
| 2124 | * bytes, at most (< 2288). */ |
| 2125 | if (p + 2 + msg->id_len + 1 > end) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2126 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2127 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2128 | /* Encode the message name */ |
| 2129 | if (spoe_encode_buffer(msg->id, msg->id_len, &p, end) == -1) |
| 2130 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2131 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2132 | /* Set the number of arguments for this message */ |
| 2133 | *p++ = msg->nargs; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2134 | |
| 2135 | ctx->frag_ctx.curoff = 0; |
| 2136 | encode_msg_payload: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2137 | |
| 2138 | /* Loop on arguments */ |
| 2139 | list_for_each_entry(arg, &msg->args, list) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2140 | ctx->frag_ctx.curarg = arg; |
| 2141 | ctx->frag_ctx.curoff = UINT_MAX; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2142 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2143 | encode_argument: |
| 2144 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2145 | goto encode_arg_value; |
| 2146 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2147 | /* Encode the arguement name as a string. It can by NULL */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2148 | if (spoe_encode_buffer(arg->name, arg->name_len, &p, end) == -1) |
| 2149 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2150 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2151 | ctx->frag_ctx.curoff = 0; |
| 2152 | encode_arg_value: |
| 2153 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2154 | /* Fetch the arguement value */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2155 | smp = sample_process(s->be, s->sess, s, |
| 2156 | dir|SMP_OPT_FINAL, arg->expr, NULL); |
| 2157 | ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, &p, end); |
| 2158 | if (ret == -1 || ctx->frag_ctx.curoff) |
| 2159 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2160 | } |
| 2161 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2162 | |
| 2163 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2164 | " - encode %s messages - spoe_appctx=%p" |
| 2165 | "- max_size=%u - encoded=%ld\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2166 | (int)now.tv_sec, (int)now.tv_usec, |
| 2167 | agent->id, __FUNCTION__, s, |
| 2168 | ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"), |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2169 | ctx->frag_ctx.spoe_appctx, (agent->frame_size - FRAME_HDR_SIZE), |
| 2170 | p - ctx->buffer->p); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2171 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2172 | ctx->buffer->i = p - ctx->buffer->p; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2173 | ctx->frag_ctx.curmsg = NULL; |
| 2174 | ctx->frag_ctx.curarg = NULL; |
| 2175 | ctx->frag_ctx.curoff = 0; |
| 2176 | ctx->frag_ctx.flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2177 | return 1; |
| 2178 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2179 | too_big: |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 2180 | if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) { |
| 2181 | ctx->status_code = SPOE_CTX_ERR_TOO_BIG; |
| 2182 | return -1; |
| 2183 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2184 | |
| 2185 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2186 | " - encode fragmented messages - spoe_appctx=%p" |
| 2187 | " - curmsg=%p - curarg=%p - curoff=%u" |
| 2188 | " - max_size=%u - encoded=%ld\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2189 | (int)now.tv_sec, (int)now.tv_usec, |
| 2190 | agent->id, __FUNCTION__, s, ctx->frag_ctx.spoe_appctx, |
| 2191 | ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2192 | (agent->frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2193 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2194 | ctx->buffer->i = p - ctx->buffer->p; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2195 | ctx->flags |= SPOE_CTX_FL_FRAGMENTED; |
| 2196 | ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN; |
| 2197 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2198 | } |
| 2199 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2200 | |
| 2201 | /*************************************************************************** |
| 2202 | * Functions that handle SPOE actions |
| 2203 | **************************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2204 | /* Helper function to set a variable */ |
| 2205 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2206 | spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2207 | struct sample *smp) |
| 2208 | { |
| 2209 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2210 | struct spoe_agent *agent = conf->agent; |
| 2211 | char varname[64]; |
| 2212 | |
| 2213 | memset(varname, 0, sizeof(varname)); |
| 2214 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2215 | scope, agent->var_pfx, len, name); |
| 2216 | vars_set_by_name_ifexist(varname, len, smp); |
| 2217 | } |
| 2218 | |
| 2219 | /* Helper function to unset a variable */ |
| 2220 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2221 | spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2222 | struct sample *smp) |
| 2223 | { |
| 2224 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2225 | struct spoe_agent *agent = conf->agent; |
| 2226 | char varname[64]; |
| 2227 | |
| 2228 | memset(varname, 0, sizeof(varname)); |
| 2229 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2230 | scope, agent->var_pfx, len, name); |
| 2231 | vars_unset_by_name_ifexist(varname, len, smp); |
| 2232 | } |
| 2233 | |
| 2234 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2235 | static inline int |
| 2236 | spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx, |
| 2237 | char **buf, char *end, int dir) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2238 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2239 | char *str, *scope, *p = *buf; |
| 2240 | struct sample smp; |
| 2241 | uint64_t sz; |
| 2242 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2243 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2244 | if (p + 2 >= end) |
| 2245 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2246 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2247 | /* SET-VAR requires 3 arguments */ |
| 2248 | if (*p++ != 3) |
| 2249 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2250 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2251 | switch (*p++) { |
| 2252 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2253 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2254 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2255 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2256 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2257 | default: goto skip; |
| 2258 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2259 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2260 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2261 | goto skip; |
| 2262 | memset(&smp, 0, sizeof(smp)); |
| 2263 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2264 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2265 | if (spoe_decode_data(&p, end, &smp) == -1) |
| 2266 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2267 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2268 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2269 | " - set-var '%s.%s.%.*s'\n", |
| 2270 | (int)now.tv_sec, (int)now.tv_usec, |
| 2271 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2272 | __FUNCTION__, s, scope, |
| 2273 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2274 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2275 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2276 | spoe_set_var(ctx, scope, str, sz, &smp); |
Christopher Faulet | b5cff60 | 2016-11-24 14:53:22 +0100 | [diff] [blame] | 2277 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2278 | ret = (p - *buf); |
| 2279 | *buf = p; |
| 2280 | return ret; |
| 2281 | skip: |
| 2282 | return 0; |
| 2283 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2284 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2285 | static inline int |
| 2286 | spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx, |
| 2287 | char **buf, char *end, int dir) |
| 2288 | { |
| 2289 | char *str, *scope, *p = *buf; |
| 2290 | struct sample smp; |
| 2291 | uint64_t sz; |
| 2292 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2293 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2294 | if (p + 2 >= end) |
| 2295 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2296 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2297 | /* UNSET-VAR requires 2 arguments */ |
| 2298 | if (*p++ != 2) |
| 2299 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2300 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2301 | switch (*p++) { |
| 2302 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2303 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2304 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2305 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2306 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2307 | default: goto skip; |
| 2308 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2309 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2310 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2311 | goto skip; |
| 2312 | memset(&smp, 0, sizeof(smp)); |
| 2313 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2314 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2315 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2316 | " - unset-var '%s.%s.%.*s'\n", |
| 2317 | (int)now.tv_sec, (int)now.tv_usec, |
| 2318 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2319 | __FUNCTION__, s, scope, |
| 2320 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2321 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2322 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2323 | spoe_unset_var(ctx, scope, str, sz, &smp); |
| 2324 | |
| 2325 | ret = (p - *buf); |
| 2326 | *buf = p; |
| 2327 | return ret; |
| 2328 | skip: |
| 2329 | return 0; |
| 2330 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2331 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2332 | /* Process SPOE actions for a specific event. It returns 1 on success. If an |
| 2333 | * error occurred, 0 is returned. */ |
| 2334 | static int |
| 2335 | spoe_process_actions(struct stream *s, struct spoe_context *ctx, |
| 2336 | enum spoe_event ev, int dir) |
| 2337 | { |
| 2338 | char *p, *end; |
| 2339 | int ret; |
| 2340 | |
| 2341 | p = ctx->buffer->p; |
| 2342 | end = p + ctx->buffer->i; |
| 2343 | |
| 2344 | while (p < end) { |
| 2345 | enum spoe_action_type type; |
| 2346 | |
| 2347 | type = *p++; |
| 2348 | switch (type) { |
| 2349 | case SPOE_ACT_T_SET_VAR: |
| 2350 | ret = spoe_decode_action_set_var(s, ctx, &p, end, dir); |
| 2351 | if (!ret) |
| 2352 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2353 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2354 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2355 | case SPOE_ACT_T_UNSET_VAR: |
| 2356 | ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir); |
| 2357 | if (!ret) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2358 | goto skip; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2359 | break; |
| 2360 | |
| 2361 | default: |
| 2362 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2363 | } |
| 2364 | } |
| 2365 | |
| 2366 | return 1; |
| 2367 | skip: |
| 2368 | return 0; |
| 2369 | } |
| 2370 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2371 | /*************************************************************************** |
| 2372 | * Functions that process SPOE events |
| 2373 | **************************************************************************/ |
| 2374 | static inline int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2375 | spoe_start_event_processing(struct spoe_context *ctx, int dir) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2376 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2377 | /* If a process is already started for this SPOE context, retry |
| 2378 | * later. */ |
| 2379 | if (ctx->flags & SPOE_CTX_FL_PROCESS) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2380 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2381 | |
| 2382 | /* Set the right flag to prevent request and response processing |
| 2383 | * in same time. */ |
| 2384 | ctx->flags |= ((dir == SMP_OPT_DIR_REQ) |
| 2385 | ? SPOE_CTX_FL_REQ_PROCESS |
| 2386 | : SPOE_CTX_FL_RSP_PROCESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2387 | return 1; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2388 | } |
| 2389 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2390 | static inline void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2391 | spoe_stop_event_processing(struct spoe_context *ctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2392 | { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2393 | struct spoe_appctx *sa = ctx->frag_ctx.spoe_appctx; |
| 2394 | |
| 2395 | if (sa) { |
| 2396 | sa->frag_ctx.ctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2397 | spoe_wakeup_appctx(sa->owner); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2398 | } |
| 2399 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2400 | /* Reset the flag to allow next processing */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2401 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2402 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2403 | ctx->status_code = 0; |
| 2404 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2405 | /* Reset processing timer */ |
| 2406 | ctx->process_exp = TICK_ETERNITY; |
| 2407 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2408 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2409 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2410 | ctx->frag_ctx.spoe_appctx = NULL; |
| 2411 | ctx->frag_ctx.curmsg = NULL; |
| 2412 | ctx->frag_ctx.curarg = NULL; |
| 2413 | ctx->frag_ctx.curoff = 0; |
| 2414 | ctx->frag_ctx.flags = 0; |
| 2415 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2416 | if (!LIST_ISEMPTY(&ctx->list)) { |
| 2417 | LIST_DEL(&ctx->list); |
| 2418 | LIST_INIT(&ctx->list); |
| 2419 | } |
| 2420 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2421 | |
| 2422 | /* Process a SPOE event. First, this functions will process messages attached to |
| 2423 | * this event and send them to an agent in a NOTIFY frame. Then, it will wait a |
| 2424 | * ACK frame to process corresponding actions. During all the processing, it |
| 2425 | * returns 0 and it returns 1 when the processing is finished. If an error |
| 2426 | * occurred, -1 is returned. */ |
| 2427 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2428 | spoe_process_event(struct stream *s, struct spoe_context *ctx, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2429 | enum spoe_event ev) |
| 2430 | { |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2431 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2432 | struct spoe_agent *agent = conf->agent; |
| 2433 | int dir, ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2434 | |
| 2435 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2436 | " - ctx-state=%s - event=%s\n", |
| 2437 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2438 | agent->id, __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2439 | spoe_event_str[ev]); |
| 2440 | |
| 2441 | dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 2442 | |
| 2443 | if (LIST_ISEMPTY(&(ctx->messages[ev]))) |
| 2444 | goto out; |
| 2445 | |
| 2446 | if (ctx->state == SPOE_CTX_ST_ERROR) |
| 2447 | goto error; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2448 | |
| 2449 | if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) { |
| 2450 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2451 | " - failed to process event '%s': timeout\n", |
| 2452 | (int)now.tv_sec, (int)now.tv_usec, |
| 2453 | agent->id, __FUNCTION__, s, spoe_event_str[ev]); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2454 | ctx->status_code = SPOE_CTX_ERR_TOUT; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2455 | goto error; |
| 2456 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2457 | |
| 2458 | if (ctx->state == SPOE_CTX_ST_READY) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2459 | if (agent->eps_max > 0) { |
| 2460 | if (!freq_ctr_remain(&agent->err_per_sec, agent->eps_max, 0)) { |
| 2461 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2462 | " - skip event '%s': max EPS reached\n", |
| 2463 | (int)now.tv_sec, (int)now.tv_usec, |
| 2464 | agent->id, __FUNCTION__, s, spoe_event_str[ev]); |
| 2465 | goto skip; |
| 2466 | } |
| 2467 | } |
| 2468 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2469 | if (!tick_isset(ctx->process_exp)) { |
| 2470 | ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing); |
| 2471 | s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire), |
| 2472 | ctx->process_exp); |
| 2473 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2474 | ret = spoe_start_event_processing(ctx, dir); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2475 | if (!ret) |
| 2476 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2477 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2478 | if (spoe_queue_context(ctx) < 0) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2479 | goto error; |
| 2480 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2481 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2482 | /* fall through */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2483 | } |
| 2484 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2485 | if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2486 | if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait)) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2487 | goto out; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2488 | ret = spoe_encode_messages(s, ctx, &(ctx->messages[ev]), dir); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2489 | if (ret < 0) |
| 2490 | goto error; |
| 2491 | ctx->state = SPOE_CTX_ST_SENDING_MSGS; |
| 2492 | } |
| 2493 | |
| 2494 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) { |
| 2495 | if (ctx->frag_ctx.spoe_appctx) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2496 | spoe_wakeup_appctx(ctx->frag_ctx.spoe_appctx->owner); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2497 | ret = 0; |
| 2498 | goto out; |
| 2499 | } |
| 2500 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2501 | if (ctx->state == SPOE_CTX_ST_WAITING_ACK) { |
| 2502 | ret = 0; |
| 2503 | goto out; |
| 2504 | } |
| 2505 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2506 | if (ctx->state == SPOE_CTX_ST_DONE) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2507 | spoe_process_actions(s, ctx, ev, dir); |
| 2508 | ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2509 | ctx->frame_id++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2510 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2511 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | out: |
| 2515 | return ret; |
| 2516 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2517 | error: |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 2518 | if (agent->eps_max > 0) |
| 2519 | update_freq_ctr(&agent->err_per_sec, 1); |
| 2520 | |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 2521 | if (agent->var_on_error) { |
| 2522 | struct sample smp; |
| 2523 | |
| 2524 | memset(&smp, 0, sizeof(smp)); |
| 2525 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2526 | smp.data.u.sint = ctx->status_code; |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 2527 | smp.data.type = SMP_T_BOOL; |
| 2528 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2529 | spoe_set_var(ctx, "txn", agent->var_on_error, |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 2530 | strlen(agent->var_on_error), &smp); |
| 2531 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2532 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2533 | " - failed to create process event '%s': code=%u\n", |
| 2534 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2535 | __FUNCTION__, ctx->strm, spoe_event_str[ev], |
| 2536 | ctx->status_code); |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2537 | send_log(ctx->strm->be, LOG_WARNING, |
| 2538 | "SPOE: [%s] failed to process event '%s': code=%u\n", |
| 2539 | agent->id, spoe_event_str[ev], ctx->status_code); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 2540 | |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 2541 | ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR) |
| 2542 | ? SPOE_CTX_ST_READY |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2543 | : SPOE_CTX_ST_NONE); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2544 | ret = 1; |
| 2545 | goto end; |
| 2546 | |
| 2547 | skip: |
| 2548 | ctx->state = SPOE_CTX_ST_READY; |
| 2549 | ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2550 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2551 | end: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2552 | spoe_stop_event_processing(ctx); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2553 | return ret; |
| 2554 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2555 | |
| 2556 | /*************************************************************************** |
| 2557 | * Functions that create/destroy SPOE contexts |
| 2558 | **************************************************************************/ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2559 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2560 | spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2561 | { |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2562 | if (*buf != &buf_empty) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2563 | return 1; |
| 2564 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2565 | if (!LIST_ISEMPTY(&buffer_wait->list)) { |
| 2566 | LIST_DEL(&buffer_wait->list); |
| 2567 | LIST_INIT(&buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2568 | } |
| 2569 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2570 | if (b_alloc_margin(buf, global.tune.reserved_bufs)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2571 | return 1; |
| 2572 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2573 | LIST_ADDQ(&buffer_wq, &buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2574 | return 0; |
| 2575 | } |
| 2576 | |
| 2577 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2578 | spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2579 | { |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2580 | if (!LIST_ISEMPTY(&buffer_wait->list)) { |
| 2581 | LIST_DEL(&buffer_wait->list); |
| 2582 | LIST_INIT(&buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | /* Release the buffer if needed */ |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2586 | if (*buf != &buf_empty) { |
| 2587 | b_free(buf); |
| 2588 | offer_buffers(buffer_wait->target, |
| 2589 | tasks_run_queue + applets_active_queue); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2590 | } |
| 2591 | } |
| 2592 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2593 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2594 | spoe_wakeup_context(struct spoe_context *ctx) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2595 | { |
| 2596 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 2597 | return 1; |
| 2598 | } |
| 2599 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2600 | static struct spoe_context * |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2601 | spoe_create_context(struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2602 | { |
| 2603 | struct spoe_config *conf = FLT_CONF(filter); |
| 2604 | struct spoe_context *ctx; |
| 2605 | |
| 2606 | ctx = pool_alloc_dirty(pool2_spoe_ctx); |
| 2607 | if (ctx == NULL) { |
| 2608 | return NULL; |
| 2609 | } |
| 2610 | memset(ctx, 0, sizeof(*ctx)); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2611 | ctx->filter = filter; |
| 2612 | ctx->state = SPOE_CTX_ST_NONE; |
| 2613 | ctx->status_code = SPOE_CTX_ERR_NONE; |
| 2614 | ctx->flags = 0; |
| 2615 | ctx->messages = conf->agent->messages; |
| 2616 | ctx->buffer = &buf_empty; |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2617 | LIST_INIT(&ctx->buffer_wait.list); |
| 2618 | ctx->buffer_wait.target = ctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2619 | ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2620 | LIST_INIT(&ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2621 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2622 | ctx->stream_id = 0; |
| 2623 | ctx->frame_id = 1; |
| 2624 | ctx->process_exp = TICK_ETERNITY; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2625 | |
| 2626 | return ctx; |
| 2627 | } |
| 2628 | |
| 2629 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2630 | spoe_destroy_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2631 | { |
| 2632 | if (!ctx) |
| 2633 | return; |
| 2634 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2635 | spoe_stop_event_processing(ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2636 | pool_free2(pool2_spoe_ctx, ctx); |
| 2637 | } |
| 2638 | |
| 2639 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2640 | spoe_reset_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2641 | { |
| 2642 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 2643 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2644 | } |
| 2645 | |
| 2646 | |
| 2647 | /*************************************************************************** |
| 2648 | * Hooks that manage the filter lifecycle (init/check/deinit) |
| 2649 | **************************************************************************/ |
| 2650 | /* Signal handler: Do a soft stop, wakeup SPOE applet */ |
| 2651 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2652 | spoe_sig_stop(struct sig_handler *sh) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2653 | { |
| 2654 | struct proxy *p; |
| 2655 | |
| 2656 | p = proxy; |
| 2657 | while (p) { |
| 2658 | struct flt_conf *fconf; |
| 2659 | |
| 2660 | list_for_each_entry(fconf, &p->filter_configs, list) { |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 2661 | struct spoe_config *conf; |
| 2662 | struct spoe_agent *agent; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2663 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2664 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 2665 | if (fconf->id != spoe_filter_id) |
| 2666 | continue; |
| 2667 | |
| 2668 | conf = fconf->conf; |
| 2669 | agent = conf->agent; |
| 2670 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2671 | list_for_each_entry(spoe_appctx, &agent->applets, list) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2672 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2673 | } |
| 2674 | } |
| 2675 | p = p->next; |
| 2676 | } |
| 2677 | } |
| 2678 | |
| 2679 | |
| 2680 | /* Initialize the SPOE filter. Returns -1 on error, else 0. */ |
| 2681 | static int |
| 2682 | spoe_init(struct proxy *px, struct flt_conf *fconf) |
| 2683 | { |
| 2684 | struct spoe_config *conf = fconf->conf; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2685 | |
| 2686 | memset(&conf->agent_fe, 0, sizeof(conf->agent_fe)); |
| 2687 | init_new_proxy(&conf->agent_fe); |
| 2688 | conf->agent_fe.parent = conf->agent; |
| 2689 | conf->agent_fe.last_change = now.tv_sec; |
| 2690 | conf->agent_fe.id = conf->agent->id; |
| 2691 | conf->agent_fe.cap = PR_CAP_FE; |
| 2692 | conf->agent_fe.mode = PR_MODE_TCP; |
| 2693 | conf->agent_fe.maxconn = 0; |
| 2694 | conf->agent_fe.options2 |= PR_O2_INDEPSTR; |
| 2695 | conf->agent_fe.conn_retries = CONN_RETRIES; |
| 2696 | conf->agent_fe.accept = frontend_accept; |
| 2697 | conf->agent_fe.srv = NULL; |
| 2698 | conf->agent_fe.timeout.client = TICK_ETERNITY; |
| 2699 | conf->agent_fe.default_target = &spoe_applet.obj_type; |
| 2700 | conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES; |
| 2701 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2702 | if (!sighandler_registered) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2703 | signal_register_fct(0, spoe_sig_stop, 0); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2704 | sighandler_registered = 1; |
| 2705 | } |
| 2706 | |
| 2707 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | /* Free ressources allocated by the SPOE filter. */ |
| 2711 | static void |
| 2712 | spoe_deinit(struct proxy *px, struct flt_conf *fconf) |
| 2713 | { |
| 2714 | struct spoe_config *conf = fconf->conf; |
| 2715 | |
| 2716 | if (conf) { |
| 2717 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2718 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2719 | spoe_release_agent(agent); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2720 | free(conf); |
| 2721 | } |
| 2722 | fconf->conf = NULL; |
| 2723 | } |
| 2724 | |
| 2725 | /* Check configuration of a SPOE filter for a specified proxy. |
| 2726 | * Return 1 on error, else 0. */ |
| 2727 | static int |
| 2728 | spoe_check(struct proxy *px, struct flt_conf *fconf) |
| 2729 | { |
| 2730 | struct spoe_config *conf = fconf->conf; |
| 2731 | struct proxy *target; |
| 2732 | |
| 2733 | target = proxy_be_by_name(conf->agent->b.name); |
| 2734 | if (target == NULL) { |
| 2735 | Alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'" |
| 2736 | " declared at %s:%d.\n", |
| 2737 | px->id, conf->agent->b.name, conf->agent->id, |
| 2738 | conf->agent->conf.file, conf->agent->conf.line); |
| 2739 | return 1; |
| 2740 | } |
| 2741 | if (target->mode != PR_MODE_TCP) { |
| 2742 | Alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared" |
| 2743 | " at %s:%d does not support HTTP mode.\n", |
| 2744 | px->id, target->id, conf->agent->id, |
| 2745 | conf->agent->conf.file, conf->agent->conf.line); |
| 2746 | return 1; |
| 2747 | } |
| 2748 | |
| 2749 | free(conf->agent->b.name); |
| 2750 | conf->agent->b.name = NULL; |
| 2751 | conf->agent->b.be = target; |
| 2752 | return 0; |
| 2753 | } |
| 2754 | |
| 2755 | /************************************************************************** |
| 2756 | * Hooks attached to a stream |
| 2757 | *************************************************************************/ |
| 2758 | /* Called when a filter instance is created and attach to a stream. It creates |
| 2759 | * the context that will be used to process this stream. */ |
| 2760 | static int |
| 2761 | spoe_start(struct stream *s, struct filter *filter) |
| 2762 | { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2763 | struct spoe_config *conf = FLT_CONF(filter); |
| 2764 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2765 | struct spoe_context *ctx; |
| 2766 | |
| 2767 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2768 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2769 | __FUNCTION__, s); |
| 2770 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2771 | ctx = spoe_create_context(filter); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2772 | if (ctx == NULL) { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2773 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2774 | " - failed to create SPOE context\n", |
| 2775 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2776 | __FUNCTION__, ctx->strm); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2777 | send_log(s->be, LOG_EMERG, |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2778 | "SPOE: [%s] failed to create SPOE context\n", |
| 2779 | agent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2780 | return 0; |
| 2781 | } |
| 2782 | |
| 2783 | ctx->strm = s; |
| 2784 | ctx->state = SPOE_CTX_ST_READY; |
| 2785 | filter->ctx = ctx; |
| 2786 | |
| 2787 | if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_REQ_FE])) |
| 2788 | filter->pre_analyzers |= AN_REQ_INSPECT_FE; |
| 2789 | |
| 2790 | if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_REQ_BE])) |
| 2791 | filter->pre_analyzers |= AN_REQ_INSPECT_BE; |
| 2792 | |
| 2793 | if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_RSP])) |
| 2794 | filter->pre_analyzers |= AN_RES_INSPECT; |
| 2795 | |
| 2796 | if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_REQ_FE])) |
| 2797 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE; |
| 2798 | |
| 2799 | if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_REQ_BE])) |
| 2800 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE; |
| 2801 | |
| 2802 | if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_RSP])) |
| 2803 | filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE; |
| 2804 | |
| 2805 | return 1; |
| 2806 | } |
| 2807 | |
| 2808 | /* Called when a filter instance is detached from a stream. It release the |
| 2809 | * attached SPOE context. */ |
| 2810 | static void |
| 2811 | spoe_stop(struct stream *s, struct filter *filter) |
| 2812 | { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2813 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
| 2814 | (int)now.tv_sec, (int)now.tv_usec, |
| 2815 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 2816 | __FUNCTION__, s); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2817 | spoe_destroy_context(filter->ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2818 | } |
| 2819 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2820 | |
| 2821 | /* |
| 2822 | * Called when the stream is woken up because of expired timer. |
| 2823 | */ |
| 2824 | static void |
| 2825 | spoe_check_timeouts(struct stream *s, struct filter *filter) |
| 2826 | { |
| 2827 | struct spoe_context *ctx = filter->ctx; |
| 2828 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2829 | if (tick_is_expired(ctx->process_exp, now_ms)) { |
| 2830 | s->pending_events |= TASK_WOKEN_MSG; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2831 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2832 | } |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2833 | } |
| 2834 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2835 | /* Called when we are ready to filter data on a channel */ |
| 2836 | static int |
| 2837 | spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 2838 | { |
| 2839 | struct spoe_context *ctx = filter->ctx; |
| 2840 | int ret = 1; |
| 2841 | |
| 2842 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 2843 | " - ctx-flags=0x%08x\n", |
| 2844 | (int)now.tv_sec, (int)now.tv_usec, |
| 2845 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 2846 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 2847 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2848 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 2849 | goto out; |
| 2850 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2851 | if (!(chn->flags & CF_ISRESP)) { |
| 2852 | if (filter->pre_analyzers & AN_REQ_INSPECT_FE) |
| 2853 | chn->analysers |= AN_REQ_INSPECT_FE; |
| 2854 | if (filter->pre_analyzers & AN_REQ_INSPECT_BE) |
| 2855 | chn->analysers |= AN_REQ_INSPECT_BE; |
| 2856 | |
| 2857 | if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED) |
| 2858 | goto out; |
| 2859 | |
| 2860 | ctx->stream_id = s->uniq_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2861 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2862 | if (!ret) |
| 2863 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2864 | ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED; |
| 2865 | } |
| 2866 | else { |
| 2867 | if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP) |
| 2868 | chn->analysers |= AN_RES_INSPECT; |
| 2869 | |
| 2870 | if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED) |
| 2871 | goto out; |
| 2872 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2873 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2874 | if (!ret) { |
| 2875 | channel_dont_read(chn); |
| 2876 | channel_dont_close(chn); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2877 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2878 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2879 | ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2880 | } |
| 2881 | |
| 2882 | out: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2883 | return ret; |
| 2884 | } |
| 2885 | |
| 2886 | /* Called before a processing happens on a given channel */ |
| 2887 | static int |
| 2888 | spoe_chn_pre_analyze(struct stream *s, struct filter *filter, |
| 2889 | struct channel *chn, unsigned an_bit) |
| 2890 | { |
| 2891 | struct spoe_context *ctx = filter->ctx; |
| 2892 | int ret = 1; |
| 2893 | |
| 2894 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 2895 | " - ctx-flags=0x%08x - ana=0x%08x\n", |
| 2896 | (int)now.tv_sec, (int)now.tv_usec, |
| 2897 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 2898 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2899 | ctx->flags, an_bit); |
| 2900 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2901 | if (ctx->state == SPOE_CTX_ST_NONE) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2902 | goto out; |
| 2903 | |
| 2904 | switch (an_bit) { |
| 2905 | case AN_REQ_INSPECT_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2906 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2907 | break; |
| 2908 | case AN_REQ_INSPECT_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2909 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2910 | break; |
| 2911 | case AN_RES_INSPECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2912 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2913 | break; |
| 2914 | case AN_REQ_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2915 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2916 | break; |
| 2917 | case AN_REQ_HTTP_PROCESS_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2918 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2919 | break; |
| 2920 | case AN_RES_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2921 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2922 | break; |
| 2923 | } |
| 2924 | |
| 2925 | out: |
| 2926 | if (!ret) { |
| 2927 | channel_dont_read(chn); |
| 2928 | channel_dont_close(chn); |
| 2929 | } |
| 2930 | return ret; |
| 2931 | } |
| 2932 | |
| 2933 | /* Called when the filtering on the channel ends. */ |
| 2934 | static int |
| 2935 | spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 2936 | { |
| 2937 | struct spoe_context *ctx = filter->ctx; |
| 2938 | |
| 2939 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 2940 | " - ctx-flags=0x%08x\n", |
| 2941 | (int)now.tv_sec, (int)now.tv_usec, |
| 2942 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 2943 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 2944 | |
| 2945 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2946 | spoe_reset_context(ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | return 1; |
| 2950 | } |
| 2951 | |
| 2952 | /******************************************************************** |
| 2953 | * Functions that manage the filter initialization |
| 2954 | ********************************************************************/ |
| 2955 | struct flt_ops spoe_ops = { |
| 2956 | /* Manage SPOE filter, called for each filter declaration */ |
| 2957 | .init = spoe_init, |
| 2958 | .deinit = spoe_deinit, |
| 2959 | .check = spoe_check, |
| 2960 | |
| 2961 | /* Handle start/stop of SPOE */ |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2962 | .attach = spoe_start, |
| 2963 | .detach = spoe_stop, |
| 2964 | .check_timeouts = spoe_check_timeouts, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2965 | |
| 2966 | /* Handle channels activity */ |
| 2967 | .channel_start_analyze = spoe_start_analyze, |
| 2968 | .channel_pre_analyze = spoe_chn_pre_analyze, |
| 2969 | .channel_end_analyze = spoe_end_analyze, |
| 2970 | }; |
| 2971 | |
| 2972 | |
| 2973 | static int |
| 2974 | cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm) |
| 2975 | { |
| 2976 | const char *err; |
| 2977 | int i, err_code = 0; |
| 2978 | |
| 2979 | if ((cfg_scope == NULL && curengine != NULL) || |
| 2980 | (cfg_scope != NULL && curengine == NULL) || |
| 2981 | strcmp(curengine, cfg_scope)) |
| 2982 | goto out; |
| 2983 | |
| 2984 | if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */ |
| 2985 | if (!*args[1]) { |
| 2986 | Alert("parsing [%s:%d] : missing name for spoe-agent section.\n", |
| 2987 | file, linenum); |
| 2988 | err_code |= ERR_ALERT | ERR_ABORT; |
| 2989 | goto out; |
| 2990 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 2991 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 2992 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2993 | goto out; |
| 2994 | } |
| 2995 | |
| 2996 | err = invalid_char(args[1]); |
| 2997 | if (err) { |
| 2998 | Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 2999 | file, linenum, *err, args[0], args[1]); |
| 3000 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3001 | goto out; |
| 3002 | } |
| 3003 | |
| 3004 | if (curagent != NULL) { |
| 3005 | Alert("parsing [%s:%d] : another spoe-agent section previously defined.\n", |
| 3006 | file, linenum); |
| 3007 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3008 | goto out; |
| 3009 | } |
| 3010 | if ((curagent = calloc(1, sizeof(*curagent))) == NULL) { |
| 3011 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3012 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3013 | goto out; |
| 3014 | } |
| 3015 | |
| 3016 | curagent->id = strdup(args[1]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3017 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3018 | curagent->conf.file = strdup(file); |
| 3019 | curagent->conf.line = linenum; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3020 | |
| 3021 | curagent->timeout.hello = TICK_ETERNITY; |
| 3022 | curagent->timeout.idle = TICK_ETERNITY; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3023 | curagent->timeout.processing = TICK_ETERNITY; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3024 | |
| 3025 | curagent->engine_id = NULL; |
| 3026 | curagent->var_pfx = NULL; |
| 3027 | curagent->var_on_error = NULL; |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 3028 | curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_ASYNC | SPOE_FL_SND_FRAGMENTATION); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3029 | curagent->cps_max = 0; |
| 3030 | curagent->eps_max = 0; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 3031 | curagent->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3032 | curagent->min_applets = 0; |
| 3033 | curagent->max_fpa = 100; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3034 | |
| 3035 | for (i = 0; i < SPOE_EV_EVENTS; ++i) |
| 3036 | LIST_INIT(&curagent->messages[i]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3037 | |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 3038 | curagent->frame_size = curagent->max_frame_size; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3039 | curagent->applets_act = 0; |
| 3040 | curagent->applets_idle = 0; |
| 3041 | curagent->sending_rate = 0; |
| 3042 | |
| 3043 | LIST_INIT(&curagent->applets); |
| 3044 | LIST_INIT(&curagent->sending_queue); |
| 3045 | LIST_INIT(&curagent->waiting_queue); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3046 | } |
| 3047 | else if (!strcmp(args[0], "use-backend")) { |
| 3048 | if (!*args[1]) { |
| 3049 | Alert("parsing [%s:%d] : '%s' expects a backend name.\n", |
| 3050 | file, linenum, args[0]); |
| 3051 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3052 | goto out; |
| 3053 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3054 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3055 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3056 | free(curagent->b.name); |
| 3057 | curagent->b.name = strdup(args[1]); |
| 3058 | } |
| 3059 | else if (!strcmp(args[0], "messages")) { |
| 3060 | int cur_arg = 1; |
| 3061 | while (*args[cur_arg]) { |
| 3062 | struct spoe_msg_placeholder *mp = NULL; |
| 3063 | |
| 3064 | list_for_each_entry(mp, &curmps, list) { |
| 3065 | if (!strcmp(mp->id, args[cur_arg])) { |
| 3066 | Alert("parsing [%s:%d]: spoe-message message '%s' already declared.\n", |
| 3067 | file, linenum, args[cur_arg]); |
| 3068 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3069 | goto out; |
| 3070 | } |
| 3071 | } |
| 3072 | |
| 3073 | if ((mp = calloc(1, sizeof(*mp))) == NULL) { |
| 3074 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3075 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3076 | goto out; |
| 3077 | } |
| 3078 | mp->id = strdup(args[cur_arg]); |
| 3079 | LIST_ADDQ(&curmps, &mp->list); |
| 3080 | cur_arg++; |
| 3081 | } |
| 3082 | } |
| 3083 | else if (!strcmp(args[0], "timeout")) { |
| 3084 | unsigned int *tv = NULL; |
| 3085 | const char *res; |
| 3086 | unsigned timeout; |
| 3087 | |
| 3088 | if (!*args[1]) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3089 | Alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3090 | file, linenum); |
| 3091 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3092 | goto out; |
| 3093 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3094 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3095 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3096 | if (!strcmp(args[1], "hello")) |
| 3097 | tv = &curagent->timeout.hello; |
| 3098 | else if (!strcmp(args[1], "idle")) |
| 3099 | tv = &curagent->timeout.idle; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3100 | else if (!strcmp(args[1], "processing")) |
| 3101 | tv = &curagent->timeout.processing; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3102 | else { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3103 | Alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3104 | file, linenum, args[1]); |
| 3105 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3106 | goto out; |
| 3107 | } |
| 3108 | if (!*args[2]) { |
| 3109 | Alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n", |
| 3110 | file, linenum, args[1]); |
| 3111 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3112 | goto out; |
| 3113 | } |
| 3114 | res = parse_time_err(args[2], &timeout, TIME_UNIT_MS); |
| 3115 | if (res) { |
| 3116 | Alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n", |
| 3117 | file, linenum, *res, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3118 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3119 | goto out; |
| 3120 | } |
| 3121 | *tv = MS_TO_TICKS(timeout); |
| 3122 | } |
| 3123 | else if (!strcmp(args[0], "option")) { |
| 3124 | if (!*args[1]) { |
| 3125 | Alert("parsing [%s:%d]: '%s' expects an option name.\n", |
| 3126 | file, linenum, args[0]); |
| 3127 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3128 | goto out; |
| 3129 | } |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3130 | |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3131 | if (!strcmp(args[1], "pipelining")) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3132 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3133 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3134 | if (kwm == 1) |
| 3135 | curagent->flags &= ~SPOE_FL_PIPELINING; |
| 3136 | else |
| 3137 | curagent->flags |= SPOE_FL_PIPELINING; |
| 3138 | goto out; |
| 3139 | } |
| 3140 | else if (!strcmp(args[1], "async")) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3141 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3142 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3143 | if (kwm == 1) |
| 3144 | curagent->flags &= ~SPOE_FL_ASYNC; |
| 3145 | else |
| 3146 | curagent->flags |= SPOE_FL_ASYNC; |
| 3147 | goto out; |
| 3148 | } |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 3149 | else if (!strcmp(args[1], "send-frag-payload")) { |
| 3150 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3151 | goto out; |
| 3152 | if (kwm == 1) |
| 3153 | curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION; |
| 3154 | else |
| 3155 | curagent->flags |= SPOE_FL_SND_FRAGMENTATION; |
| 3156 | goto out; |
| 3157 | } |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3158 | |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3159 | /* Following options does not support negation */ |
| 3160 | if (kwm == 1) { |
| 3161 | Alert("parsing [%s:%d]: negation is not supported for option '%s'.\n", |
| 3162 | file, linenum, args[1]); |
| 3163 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3164 | goto out; |
| 3165 | } |
| 3166 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3167 | if (!strcmp(args[1], "var-prefix")) { |
| 3168 | char *tmp; |
| 3169 | |
| 3170 | if (!*args[2]) { |
| 3171 | Alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3172 | file, linenum, args[0], |
| 3173 | args[1]); |
| 3174 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3175 | goto out; |
| 3176 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3177 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3178 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3179 | tmp = args[2]; |
| 3180 | while (*tmp) { |
| 3181 | if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') { |
| 3182 | Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n", |
| 3183 | file, linenum, args[0], args[1]); |
| 3184 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3185 | goto out; |
| 3186 | } |
| 3187 | tmp++; |
| 3188 | } |
| 3189 | curagent->var_pfx = strdup(args[2]); |
| 3190 | } |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3191 | else if (!strcmp(args[1], "continue-on-error")) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3192 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3193 | goto out; |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3194 | curagent->flags |= SPOE_FL_CONT_ON_ERR; |
| 3195 | } |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3196 | else if (!strcmp(args[1], "set-on-error")) { |
| 3197 | char *tmp; |
| 3198 | |
| 3199 | if (!*args[2]) { |
| 3200 | Alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3201 | file, linenum, args[0], |
| 3202 | args[1]); |
| 3203 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3204 | goto out; |
| 3205 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3206 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3207 | goto out; |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3208 | tmp = args[2]; |
| 3209 | while (*tmp) { |
| 3210 | if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') { |
| 3211 | Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n", |
| 3212 | file, linenum, args[0], args[1]); |
| 3213 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3214 | goto out; |
| 3215 | } |
| 3216 | tmp++; |
| 3217 | } |
| 3218 | curagent->var_on_error = strdup(args[2]); |
| 3219 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3220 | else { |
| 3221 | Alert("parsing [%s:%d]: option '%s' is not supported.\n", |
| 3222 | file, linenum, args[1]); |
| 3223 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3224 | goto out; |
| 3225 | } |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3226 | } |
| 3227 | else if (!strcmp(args[0], "maxconnrate")) { |
| 3228 | if (!*args[1]) { |
| 3229 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3230 | file, linenum, args[0]); |
| 3231 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3232 | goto out; |
| 3233 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3234 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3235 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3236 | curagent->cps_max = atol(args[1]); |
| 3237 | } |
| 3238 | else if (!strcmp(args[0], "maxerrrate")) { |
| 3239 | if (!*args[1]) { |
| 3240 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3241 | file, linenum, args[0]); |
| 3242 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3243 | goto out; |
| 3244 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3245 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3246 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3247 | curagent->eps_max = atol(args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3248 | } |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3249 | else if (!strcmp(args[0], "max-frame-size")) { |
| 3250 | if (!*args[1]) { |
| 3251 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3252 | file, linenum, args[0]); |
| 3253 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3254 | goto out; |
| 3255 | } |
| 3256 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3257 | goto out; |
| 3258 | curagent->max_frame_size = atol(args[1]); |
| 3259 | if (curagent->max_frame_size < MIN_FRAME_SIZE || |
| 3260 | curagent->max_frame_size > MAX_FRAME_SIZE) { |
| 3261 | Alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n", |
| 3262 | file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE); |
| 3263 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3264 | goto out; |
| 3265 | } |
| 3266 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3267 | else if (*args[0]) { |
| 3268 | Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n", |
| 3269 | file, linenum, args[0]); |
| 3270 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3271 | goto out; |
| 3272 | } |
| 3273 | out: |
| 3274 | return err_code; |
| 3275 | } |
| 3276 | |
| 3277 | static int |
| 3278 | cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm) |
| 3279 | { |
| 3280 | struct spoe_message *msg; |
| 3281 | struct spoe_arg *arg; |
| 3282 | const char *err; |
| 3283 | char *errmsg = NULL; |
| 3284 | int err_code = 0; |
| 3285 | |
| 3286 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3287 | (cfg_scope != NULL && curengine == NULL) || |
| 3288 | strcmp(curengine, cfg_scope)) |
| 3289 | goto out; |
| 3290 | |
| 3291 | if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */ |
| 3292 | if (!*args[1]) { |
| 3293 | Alert("parsing [%s:%d] : missing name for spoe-message section.\n", |
| 3294 | file, linenum); |
| 3295 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3296 | goto out; |
| 3297 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3298 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3299 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3300 | goto out; |
| 3301 | } |
| 3302 | |
| 3303 | err = invalid_char(args[1]); |
| 3304 | if (err) { |
| 3305 | Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3306 | file, linenum, *err, args[0], args[1]); |
| 3307 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3308 | goto out; |
| 3309 | } |
| 3310 | |
| 3311 | list_for_each_entry(msg, &curmsgs, list) { |
| 3312 | if (!strcmp(msg->id, args[1])) { |
| 3313 | Alert("parsing [%s:%d]: spoe-message section '%s' has the same" |
| 3314 | " name as another one declared at %s:%d.\n", |
| 3315 | file, linenum, args[1], msg->conf.file, msg->conf.line); |
| 3316 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3317 | goto out; |
| 3318 | } |
| 3319 | } |
| 3320 | |
| 3321 | if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) { |
| 3322 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3323 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3324 | goto out; |
| 3325 | } |
| 3326 | |
| 3327 | curmsg->id = strdup(args[1]); |
| 3328 | curmsg->id_len = strlen(curmsg->id); |
| 3329 | curmsg->event = SPOE_EV_NONE; |
| 3330 | curmsg->conf.file = strdup(file); |
| 3331 | curmsg->conf.line = linenum; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 3332 | curmsg->nargs = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3333 | LIST_INIT(&curmsg->args); |
| 3334 | LIST_ADDQ(&curmsgs, &curmsg->list); |
| 3335 | } |
| 3336 | else if (!strcmp(args[0], "args")) { |
| 3337 | int cur_arg = 1; |
| 3338 | |
| 3339 | curproxy->conf.args.ctx = ARGC_SPOE; |
| 3340 | curproxy->conf.args.file = file; |
| 3341 | curproxy->conf.args.line = linenum; |
| 3342 | while (*args[cur_arg]) { |
| 3343 | char *delim = strchr(args[cur_arg], '='); |
| 3344 | int idx = 0; |
| 3345 | |
| 3346 | if ((arg = calloc(1, sizeof(*arg))) == NULL) { |
| 3347 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3348 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3349 | goto out; |
| 3350 | } |
| 3351 | |
| 3352 | if (!delim) { |
| 3353 | arg->name = NULL; |
| 3354 | arg->name_len = 0; |
| 3355 | delim = args[cur_arg]; |
| 3356 | } |
| 3357 | else { |
| 3358 | arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]); |
| 3359 | arg->name_len = delim - args[cur_arg]; |
| 3360 | delim++; |
| 3361 | } |
Christopher Faulet | b0b4238 | 2017-02-23 22:41:09 +0100 | [diff] [blame] | 3362 | arg->expr = sample_parse_expr((char*[]){delim, NULL}, |
| 3363 | &idx, file, linenum, &errmsg, |
| 3364 | &curproxy->conf.args); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3365 | if (arg->expr == NULL) { |
| 3366 | Alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg); |
| 3367 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3368 | free(arg->name); |
| 3369 | free(arg); |
| 3370 | goto out; |
| 3371 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 3372 | curmsg->nargs++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3373 | LIST_ADDQ(&curmsg->args, &arg->list); |
| 3374 | cur_arg++; |
| 3375 | } |
| 3376 | curproxy->conf.args.file = NULL; |
| 3377 | curproxy->conf.args.line = 0; |
| 3378 | } |
| 3379 | else if (!strcmp(args[0], "event")) { |
| 3380 | if (!*args[1]) { |
| 3381 | Alert("parsing [%s:%d] : missing event name.\n", file, linenum); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3382 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3383 | goto out; |
| 3384 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3385 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3386 | goto out; |
| 3387 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3388 | if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS])) |
| 3389 | curmsg->event = SPOE_EV_ON_CLIENT_SESS; |
| 3390 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS])) |
| 3391 | curmsg->event = SPOE_EV_ON_SERVER_SESS; |
| 3392 | |
| 3393 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE])) |
| 3394 | curmsg->event = SPOE_EV_ON_TCP_REQ_FE; |
| 3395 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE])) |
| 3396 | curmsg->event = SPOE_EV_ON_TCP_REQ_BE; |
| 3397 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP])) |
| 3398 | curmsg->event = SPOE_EV_ON_TCP_RSP; |
| 3399 | |
| 3400 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE])) |
| 3401 | curmsg->event = SPOE_EV_ON_HTTP_REQ_FE; |
| 3402 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE])) |
| 3403 | curmsg->event = SPOE_EV_ON_HTTP_REQ_BE; |
| 3404 | else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP])) |
| 3405 | curmsg->event = SPOE_EV_ON_HTTP_RSP; |
| 3406 | else { |
| 3407 | Alert("parsing [%s:%d] : unkown event '%s'.\n", |
| 3408 | file, linenum, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3409 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3410 | goto out; |
| 3411 | } |
| 3412 | } |
| 3413 | else if (!*args[0]) { |
| 3414 | Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n", |
| 3415 | file, linenum, args[0]); |
| 3416 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3417 | goto out; |
| 3418 | } |
| 3419 | out: |
| 3420 | free(errmsg); |
| 3421 | return err_code; |
| 3422 | } |
| 3423 | |
| 3424 | /* Return -1 on error, else 0 */ |
| 3425 | static int |
| 3426 | parse_spoe_flt(char **args, int *cur_arg, struct proxy *px, |
| 3427 | struct flt_conf *fconf, char **err, void *private) |
| 3428 | { |
| 3429 | struct list backup_sections; |
| 3430 | struct spoe_config *conf; |
| 3431 | struct spoe_message *msg, *msgback; |
| 3432 | struct spoe_msg_placeholder *mp, *mpback; |
| 3433 | char *file = NULL, *engine = NULL; |
| 3434 | int ret, pos = *cur_arg + 1; |
| 3435 | |
| 3436 | conf = calloc(1, sizeof(*conf)); |
| 3437 | if (conf == NULL) { |
| 3438 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 3439 | goto error; |
| 3440 | } |
| 3441 | conf->proxy = px; |
| 3442 | |
| 3443 | while (*args[pos]) { |
| 3444 | if (!strcmp(args[pos], "config")) { |
| 3445 | if (!*args[pos+1]) { |
| 3446 | memprintf(err, "'%s' : '%s' option without value", |
| 3447 | args[*cur_arg], args[pos]); |
| 3448 | goto error; |
| 3449 | } |
| 3450 | file = args[pos+1]; |
| 3451 | pos += 2; |
| 3452 | } |
| 3453 | else if (!strcmp(args[pos], "engine")) { |
| 3454 | if (!*args[pos+1]) { |
| 3455 | memprintf(err, "'%s' : '%s' option without value", |
| 3456 | args[*cur_arg], args[pos]); |
| 3457 | goto error; |
| 3458 | } |
| 3459 | engine = args[pos+1]; |
| 3460 | pos += 2; |
| 3461 | } |
| 3462 | else { |
| 3463 | memprintf(err, "unknown keyword '%s'", args[pos]); |
| 3464 | goto error; |
| 3465 | } |
| 3466 | } |
| 3467 | if (file == NULL) { |
| 3468 | memprintf(err, "'%s' : missing config file", args[*cur_arg]); |
| 3469 | goto error; |
| 3470 | } |
| 3471 | |
| 3472 | /* backup sections and register SPOE sections */ |
| 3473 | LIST_INIT(&backup_sections); |
| 3474 | cfg_backup_sections(&backup_sections); |
| 3475 | cfg_register_section("spoe-agent", cfg_parse_spoe_agent); |
| 3476 | cfg_register_section("spoe-message", cfg_parse_spoe_message); |
| 3477 | |
| 3478 | /* Parse SPOE filter configuration file */ |
| 3479 | curengine = engine; |
| 3480 | curproxy = px; |
| 3481 | curagent = NULL; |
| 3482 | curmsg = NULL; |
| 3483 | ret = readcfgfile(file); |
| 3484 | curproxy = NULL; |
| 3485 | |
| 3486 | /* unregister SPOE sections and restore previous sections */ |
| 3487 | cfg_unregister_sections(); |
| 3488 | cfg_restore_sections(&backup_sections); |
| 3489 | |
| 3490 | if (ret == -1) { |
| 3491 | memprintf(err, "Could not open configuration file %s : %s", |
| 3492 | file, strerror(errno)); |
| 3493 | goto error; |
| 3494 | } |
| 3495 | if (ret & (ERR_ABORT|ERR_FATAL)) { |
| 3496 | memprintf(err, "Error(s) found in configuration file %s", file); |
| 3497 | goto error; |
| 3498 | } |
| 3499 | |
| 3500 | /* Check SPOE agent */ |
| 3501 | if (curagent == NULL) { |
| 3502 | memprintf(err, "No SPOE agent found in file %s", file); |
| 3503 | goto error; |
| 3504 | } |
| 3505 | if (curagent->b.name == NULL) { |
| 3506 | memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d", |
| 3507 | curagent->id, curagent->conf.file, curagent->conf.line); |
| 3508 | goto error; |
| 3509 | } |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3510 | if (curagent->timeout.hello == TICK_ETERNITY || |
| 3511 | curagent->timeout.idle == TICK_ETERNITY || |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3512 | curagent->timeout.processing == TICK_ETERNITY) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3513 | Warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n" |
| 3514 | " | While not properly invalid, you will certainly encounter various problems\n" |
| 3515 | " | with such a configuration. To fix this, please ensure that all following\n" |
Christopher Faulet | 03a3449 | 2016-11-19 16:47:56 +0100 | [diff] [blame] | 3516 | " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3517 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
| 3518 | } |
| 3519 | if (curagent->var_pfx == NULL) { |
| 3520 | char *tmp = curagent->id; |
| 3521 | |
| 3522 | while (*tmp) { |
| 3523 | if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') { |
| 3524 | memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. " |
| 3525 | "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n", |
| 3526 | curagent->id, curagent->id, curagent->conf.file, curagent->conf.line); |
| 3527 | goto error; |
| 3528 | } |
| 3529 | tmp++; |
| 3530 | } |
| 3531 | curagent->var_pfx = strdup(curagent->id); |
| 3532 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3533 | if (curagent->engine_id == NULL) |
| 3534 | curagent->engine_id = generate_pseudo_uuid(); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3535 | |
| 3536 | if (LIST_ISEMPTY(&curmps)) { |
| 3537 | Warning("Proxy '%s': No message used by SPOE agent '%s' declared at %s:%d.\n", |
| 3538 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
| 3539 | goto finish; |
| 3540 | } |
| 3541 | |
| 3542 | list_for_each_entry_safe(mp, mpback, &curmps, list) { |
| 3543 | list_for_each_entry_safe(msg, msgback, &curmsgs, list) { |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 3544 | struct spoe_arg *arg; |
| 3545 | unsigned int where; |
| 3546 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3547 | if (!strcmp(msg->id, mp->id)) { |
| 3548 | if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) { |
| 3549 | if (msg->event == SPOE_EV_ON_TCP_REQ_BE) |
| 3550 | msg->event = SPOE_EV_ON_TCP_REQ_FE; |
| 3551 | if (msg->event == SPOE_EV_ON_HTTP_REQ_BE) |
| 3552 | msg->event = SPOE_EV_ON_HTTP_REQ_FE; |
| 3553 | } |
| 3554 | if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS || |
| 3555 | msg->event == SPOE_EV_ON_TCP_REQ_FE || |
| 3556 | msg->event == SPOE_EV_ON_HTTP_REQ_FE)) { |
| 3557 | Warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n", |
| 3558 | px->id, msg->conf.file, msg->conf.line); |
| 3559 | goto next; |
| 3560 | } |
| 3561 | if (msg->event == SPOE_EV_NONE) { |
| 3562 | Warning("Proxy '%s': Ignore SPOE message without event at %s:%d.\n", |
| 3563 | px->id, msg->conf.file, msg->conf.line); |
| 3564 | goto next; |
| 3565 | } |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 3566 | |
| 3567 | where = 0; |
| 3568 | switch (msg->event) { |
| 3569 | case SPOE_EV_ON_CLIENT_SESS: |
| 3570 | where |= SMP_VAL_FE_CON_ACC; |
| 3571 | break; |
| 3572 | |
| 3573 | case SPOE_EV_ON_TCP_REQ_FE: |
| 3574 | where |= SMP_VAL_FE_REQ_CNT; |
| 3575 | break; |
| 3576 | |
| 3577 | case SPOE_EV_ON_HTTP_REQ_FE: |
| 3578 | where |= SMP_VAL_FE_HRQ_HDR; |
| 3579 | break; |
| 3580 | |
| 3581 | case SPOE_EV_ON_TCP_REQ_BE: |
| 3582 | if (px->cap & PR_CAP_FE) |
| 3583 | where |= SMP_VAL_FE_REQ_CNT; |
| 3584 | if (px->cap & PR_CAP_BE) |
| 3585 | where |= SMP_VAL_BE_REQ_CNT; |
| 3586 | break; |
| 3587 | |
| 3588 | case SPOE_EV_ON_HTTP_REQ_BE: |
| 3589 | if (px->cap & PR_CAP_FE) |
| 3590 | where |= SMP_VAL_FE_HRQ_HDR; |
| 3591 | if (px->cap & PR_CAP_BE) |
| 3592 | where |= SMP_VAL_BE_HRQ_HDR; |
| 3593 | break; |
| 3594 | |
| 3595 | case SPOE_EV_ON_SERVER_SESS: |
| 3596 | where |= SMP_VAL_BE_SRV_CON; |
| 3597 | break; |
| 3598 | |
| 3599 | case SPOE_EV_ON_TCP_RSP: |
| 3600 | if (px->cap & PR_CAP_FE) |
| 3601 | where |= SMP_VAL_FE_RES_CNT; |
| 3602 | if (px->cap & PR_CAP_BE) |
| 3603 | where |= SMP_VAL_BE_RES_CNT; |
| 3604 | break; |
| 3605 | |
| 3606 | case SPOE_EV_ON_HTTP_RSP: |
| 3607 | if (px->cap & PR_CAP_FE) |
| 3608 | where |= SMP_VAL_FE_HRS_HDR; |
| 3609 | if (px->cap & PR_CAP_BE) |
| 3610 | where |= SMP_VAL_BE_HRS_HDR; |
| 3611 | break; |
| 3612 | |
| 3613 | default: |
| 3614 | break; |
| 3615 | } |
| 3616 | |
| 3617 | list_for_each_entry(arg, &msg->args, list) { |
| 3618 | if (!(arg->expr->fetch->val & where)) { |
| 3619 | Warning("Proxy '%s': Ignore SPOE message at %s:%d: " |
| 3620 | "some args extract information from '%s', " |
| 3621 | "none of which is available here ('%s').\n", |
| 3622 | px->id, msg->conf.file, msg->conf.line, |
| 3623 | sample_ckp_names(arg->expr->fetch->use), |
| 3624 | sample_ckp_names(where)); |
| 3625 | goto next; |
| 3626 | } |
| 3627 | } |
| 3628 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3629 | msg->agent = curagent; |
| 3630 | LIST_DEL(&msg->list); |
| 3631 | LIST_ADDQ(&curagent->messages[msg->event], &msg->list); |
| 3632 | goto next; |
| 3633 | } |
| 3634 | } |
| 3635 | memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d", |
| 3636 | curagent->id, mp->id, curagent->conf.file, curagent->conf.line); |
| 3637 | goto error; |
| 3638 | next: |
| 3639 | continue; |
| 3640 | } |
| 3641 | |
| 3642 | finish: |
| 3643 | conf->agent = curagent; |
| 3644 | list_for_each_entry_safe(mp, mpback, &curmps, list) { |
| 3645 | LIST_DEL(&mp->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3646 | spoe_release_msg_placeholder(mp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3647 | } |
| 3648 | list_for_each_entry_safe(msg, msgback, &curmsgs, list) { |
| 3649 | Warning("Proxy '%s': Ignore unused SPOE messages '%s' declared at %s:%d.\n", |
| 3650 | px->id, msg->id, msg->conf.file, msg->conf.line); |
| 3651 | LIST_DEL(&msg->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3652 | spoe_release_message(msg); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3653 | } |
| 3654 | |
| 3655 | *cur_arg = pos; |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 3656 | fconf->id = spoe_filter_id; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3657 | fconf->ops = &spoe_ops; |
| 3658 | fconf->conf = conf; |
| 3659 | return 0; |
| 3660 | |
| 3661 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3662 | spoe_release_agent(curagent); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3663 | list_for_each_entry_safe(mp, mpback, &curmps, list) { |
| 3664 | LIST_DEL(&mp->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3665 | spoe_release_msg_placeholder(mp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3666 | } |
| 3667 | list_for_each_entry_safe(msg, msgback, &curmsgs, list) { |
| 3668 | LIST_DEL(&msg->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3669 | spoe_release_message(msg); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3670 | } |
| 3671 | free(conf); |
| 3672 | return -1; |
| 3673 | } |
| 3674 | |
| 3675 | |
| 3676 | /* Declare the filter parser for "spoe" keyword */ |
| 3677 | static struct flt_kw_list flt_kws = { "SPOE", { }, { |
| 3678 | { "spoe", parse_spoe_flt, NULL }, |
| 3679 | { NULL, NULL, NULL }, |
| 3680 | } |
| 3681 | }; |
| 3682 | |
| 3683 | __attribute__((constructor)) |
| 3684 | static void __spoe_init(void) |
| 3685 | { |
| 3686 | flt_register_keywords(&flt_kws); |
| 3687 | |
| 3688 | LIST_INIT(&curmsgs); |
| 3689 | LIST_INIT(&curmps); |
| 3690 | pool2_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 3691 | pool2_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3692 | } |
| 3693 | |
| 3694 | __attribute__((destructor)) |
| 3695 | static void |
| 3696 | __spoe_deinit(void) |
| 3697 | { |
| 3698 | pool_destroy2(pool2_spoe_ctx); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 3699 | pool_destroy2(pool2_spoe_appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3700 | } |