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