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