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