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