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