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