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