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