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