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