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