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 | |
Willy Tarreau | dcc048a | 2020-06-04 19:11:43 +0200 | [diff] [blame] | 15 | #include <haproxy/acl.h> |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 16 | #include <haproxy/action-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 17 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 18 | #include <haproxy/arg.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 19 | #include <haproxy/cfgparse.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 20 | #include <haproxy/filters.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 21 | #include <haproxy/freq_ctr.h> |
Willy Tarreau | 762d7a5 | 2020-06-04 11:23:07 +0200 | [diff] [blame] | 22 | #include <haproxy/frontend.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 23 | #include <haproxy/global.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 24 | #include <haproxy/http_rules.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 25 | #include <haproxy/log.h> |
Willy Tarreau | d0ef439 | 2020-06-02 09:38:52 +0200 | [diff] [blame] | 26 | #include <haproxy/pool.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 27 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/sample.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 29 | #include <haproxy/session.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 30 | #include <haproxy/signal.h> |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 31 | #include <haproxy/sink.h> |
Willy Tarreau | 6c58ab0 | 2020-06-04 22:35:49 +0200 | [diff] [blame] | 32 | #include <haproxy/spoe.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 33 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 34 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 35 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 36 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 37 | #include <haproxy/thread.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 38 | #include <haproxy/time.h> |
Willy Tarreau | e16ada1 | 2021-05-08 12:57:17 +0200 | [diff] [blame] | 39 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 40 | #include <haproxy/vars.h> |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 41 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 42 | |
| 43 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 44 | #define SPOE_PRINTF(x...) fprintf(x) |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 45 | #define SPOE_DEBUG_STMT(statement) statement |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 46 | #else |
| 47 | #define SPOE_PRINTF(x...) |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 48 | #define SPOE_DEBUG_STMT(statement) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 49 | #endif |
| 50 | |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 51 | /* Reserved 4 bytes to the frame size. So a frame and its size can be written |
| 52 | * together in a buffer */ |
| 53 | #define MAX_FRAME_SIZE global.tune.bufsize - 4 |
| 54 | |
| 55 | /* The minimum size for a frame */ |
| 56 | #define MIN_FRAME_SIZE 256 |
| 57 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 58 | /* Reserved for the metadata and the frame type. |
| 59 | * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */ |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 60 | #define FRAME_HDR_SIZE 32 |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 61 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 62 | /* Helper to get SPOE ctx inside an appctx */ |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 63 | #define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr)) |
| 64 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 65 | /* SPOE filter id. Used to identify SPOE filters */ |
| 66 | const char *spoe_filter_id = "SPOE filter"; |
| 67 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 68 | /* Set if the handle on SIGUSR1 is registered */ |
| 69 | static int sighandler_registered = 0; |
| 70 | |
| 71 | /* proxy used during the parsing */ |
| 72 | struct proxy *curproxy = NULL; |
| 73 | |
| 74 | /* The name of the SPOE engine, used during the parsing */ |
| 75 | char *curengine = NULL; |
| 76 | |
| 77 | /* SPOE agent used during the parsing */ |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 78 | /* SPOE agent/group/message used during the parsing */ |
| 79 | struct spoe_agent *curagent = NULL; |
| 80 | struct spoe_group *curgrp = NULL; |
| 81 | struct spoe_message *curmsg = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 82 | |
| 83 | /* list of SPOE messages and placeholders used during the parsing */ |
| 84 | struct list curmsgs; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 85 | struct list curgrps; |
| 86 | struct list curmphs; |
| 87 | struct list curgphs; |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 88 | struct list curvars; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 89 | |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 90 | /* list of log servers used during the parsing */ |
| 91 | struct list curlogsrvs; |
| 92 | |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 93 | /* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */ |
| 94 | int curpxopts; |
| 95 | int curpxopts2; |
| 96 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 97 | /* Pools used to allocate SPOE structs */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 98 | DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context)); |
| 99 | 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] | 100 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 101 | struct flt_ops spoe_ops; |
| 102 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 103 | static int spoe_queue_context(struct spoe_context *ctx); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 104 | static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait); |
| 105 | static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait); |
Christopher Faulet | 4d350af | 2021-08-02 17:53:56 +0200 | [diff] [blame] | 106 | static struct appctx *spoe_create_appctx(struct spoe_config *conf); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 107 | |
| 108 | /******************************************************************** |
| 109 | * helper functions/globals |
| 110 | ********************************************************************/ |
| 111 | static void |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 112 | spoe_release_placeholder(struct spoe_placeholder *ph) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 113 | { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 114 | if (!ph) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 115 | return; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 116 | free(ph->id); |
| 117 | free(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 118 | } |
| 119 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 120 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 121 | spoe_release_message(struct spoe_message *msg) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 122 | { |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 123 | struct spoe_arg *arg, *argback; |
| 124 | struct acl *acl, *aclback; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 125 | |
| 126 | if (!msg) |
| 127 | return; |
| 128 | free(msg->id); |
| 129 | free(msg->conf.file); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 130 | list_for_each_entry_safe(arg, argback, &msg->args, list) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 131 | release_sample_expr(arg->expr); |
| 132 | free(arg->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 133 | LIST_DELETE(&arg->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 134 | free(arg); |
| 135 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 136 | list_for_each_entry_safe(acl, aclback, &msg->acls, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 137 | LIST_DELETE(&acl->list); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 138 | prune_acl(acl); |
| 139 | free(acl); |
| 140 | } |
| 141 | if (msg->cond) { |
| 142 | prune_acl_cond(msg->cond); |
| 143 | free(msg->cond); |
| 144 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 145 | free(msg); |
| 146 | } |
| 147 | |
| 148 | static void |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 149 | spoe_release_group(struct spoe_group *grp) |
| 150 | { |
| 151 | if (!grp) |
| 152 | return; |
| 153 | free(grp->id); |
| 154 | free(grp->conf.file); |
| 155 | free(grp); |
| 156 | } |
| 157 | |
| 158 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 159 | spoe_release_agent(struct spoe_agent *agent) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 160 | { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 161 | struct spoe_message *msg, *msgback; |
| 162 | struct spoe_group *grp, *grpback; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 163 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 164 | |
| 165 | if (!agent) |
| 166 | return; |
| 167 | free(agent->id); |
| 168 | free(agent->conf.file); |
| 169 | free(agent->var_pfx); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 170 | free(agent->var_on_error); |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 171 | free(agent->var_t_process); |
| 172 | free(agent->var_t_total); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 173 | list_for_each_entry_safe(msg, msgback, &agent->messages, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 174 | LIST_DELETE(&msg->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 175 | spoe_release_message(msg); |
| 176 | } |
| 177 | list_for_each_entry_safe(grp, grpback, &agent->groups, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 178 | LIST_DELETE(&grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 179 | spoe_release_group(grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 180 | } |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 181 | if (agent->rt) { |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 182 | for (i = 0; i < global.nbthread; ++i) { |
| 183 | free(agent->rt[i].engine_id); |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 184 | HA_SPIN_DESTROY(&agent->rt[i].lock); |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 185 | } |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 186 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 187 | free(agent->rt); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 188 | free(agent); |
| 189 | } |
| 190 | |
| 191 | static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 192 | [SPOE_FRM_ERR_NONE] = "normal", |
| 193 | [SPOE_FRM_ERR_IO] = "I/O error", |
| 194 | [SPOE_FRM_ERR_TOUT] = "a timeout occurred", |
| 195 | [SPOE_FRM_ERR_TOO_BIG] = "frame is too big", |
| 196 | [SPOE_FRM_ERR_INVALID] = "invalid frame received", |
| 197 | [SPOE_FRM_ERR_NO_VSN] = "version value not found", |
| 198 | [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found", |
| 199 | [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found", |
| 200 | [SPOE_FRM_ERR_BAD_VSN] = "unsupported version", |
| 201 | [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small", |
| 202 | [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported", |
| 203 | [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames", |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 204 | [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 205 | [SPOE_FRM_ERR_RES] = "resource allocation error", |
| 206 | [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | static const char *spoe_event_str[SPOE_EV_EVENTS] = { |
| 210 | [SPOE_EV_ON_CLIENT_SESS] = "on-client-session", |
| 211 | [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request", |
| 212 | [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request", |
| 213 | [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request", |
| 214 | [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request", |
| 215 | |
| 216 | [SPOE_EV_ON_SERVER_SESS] = "on-server-session", |
| 217 | [SPOE_EV_ON_TCP_RSP] = "on-tcp-response", |
| 218 | [SPOE_EV_ON_HTTP_RSP] = "on-http-response", |
| 219 | }; |
| 220 | |
| 221 | |
| 222 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 223 | |
| 224 | static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 225 | [SPOE_CTX_ST_NONE] = "NONE", |
| 226 | [SPOE_CTX_ST_READY] = "READY", |
| 227 | [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS", |
| 228 | [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS", |
| 229 | [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK", |
| 230 | [SPOE_CTX_ST_DONE] = "DONE", |
| 231 | [SPOE_CTX_ST_ERROR] = "ERROR", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 235 | [SPOE_APPCTX_ST_CONNECT] = "CONNECT", |
| 236 | [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING", |
| 237 | [SPOE_APPCTX_ST_IDLE] = "IDLE", |
| 238 | [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING", |
| 239 | [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY", |
| 240 | [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK", |
| 241 | [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT", |
| 242 | [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING", |
| 243 | [SPOE_APPCTX_ST_EXIT] = "EXIT", |
| 244 | [SPOE_APPCTX_ST_END] = "END", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | #endif |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 248 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 249 | /* Used to generates a unique id for an engine. On success, it returns a |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 250 | * allocated string. So it is the caller's responsibility to release it. If the |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 251 | * allocation failed, it returns NULL. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 252 | static char * |
| 253 | generate_pseudo_uuid() |
| 254 | { |
Willy Tarreau | ee3bcdd | 2020-03-08 17:48:17 +0100 | [diff] [blame] | 255 | ha_generate_uuid(&trash); |
Kevin Zhu | 079f808 | 2020-03-13 10:39:51 +0800 | [diff] [blame] | 256 | return my_strndup(trash.area, trash.data); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 259 | |
| 260 | static inline void |
| 261 | spoe_update_stat_time(struct timeval *tv, long *t) |
| 262 | { |
| 263 | if (*t == -1) |
| 264 | *t = tv_ms_elapsed(tv, &now); |
| 265 | else |
| 266 | *t += tv_ms_elapsed(tv, &now); |
| 267 | tv_zero(tv); |
| 268 | } |
| 269 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 270 | /******************************************************************** |
| 271 | * Functions that encode/decode SPOE frames |
| 272 | ********************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 273 | /* Helper to get static string length, excluding the terminating null byte */ |
| 274 | #define SLEN(str) (sizeof(str)-1) |
| 275 | |
| 276 | /* Predefined key used in HELLO/DISCONNECT frames */ |
| 277 | #define SUPPORTED_VERSIONS_KEY "supported-versions" |
| 278 | #define VERSION_KEY "version" |
| 279 | #define MAX_FRAME_SIZE_KEY "max-frame-size" |
| 280 | #define CAPABILITIES_KEY "capabilities" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 281 | #define ENGINE_ID_KEY "engine-id" |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 282 | #define HEALTHCHECK_KEY "healthcheck" |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 283 | #define STATUS_CODE_KEY "status-code" |
| 284 | #define MSG_KEY "message" |
| 285 | |
| 286 | struct spoe_version { |
| 287 | char *str; |
| 288 | int min; |
| 289 | int max; |
| 290 | }; |
| 291 | |
| 292 | /* All supported versions */ |
| 293 | static struct spoe_version supported_versions[] = { |
Christopher Faulet | 6381650 | 2018-05-31 14:56:42 +0200 | [diff] [blame] | 294 | /* 1.0 is now unsupported because of a bug about frame's flags*/ |
| 295 | {"2.0", 2000, 2000}, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 296 | {NULL, 0, 0} |
| 297 | }; |
| 298 | |
| 299 | /* Comma-separated list of supported versions */ |
Christopher Faulet | 6381650 | 2018-05-31 14:56:42 +0200 | [diff] [blame] | 300 | #define SUPPORTED_VERSIONS_VAL "2.0" |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 301 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 302 | /* Convert a string to a SPOE version value. The string must follow the format |
| 303 | * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR). |
| 304 | * If an error occurred, -1 is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 305 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 306 | spoe_str_to_vsn(const char *str, size_t len) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 307 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 308 | const char *p, *end; |
| 309 | int maj, min, vsn; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 310 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 311 | p = str; |
| 312 | end = str+len; |
| 313 | maj = min = 0; |
| 314 | vsn = -1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 315 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 316 | /* skip leading spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 317 | while (p < end && isspace((unsigned char)*p)) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 318 | p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 319 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 320 | /* parse Major number, until the '.' */ |
| 321 | while (*p != '.') { |
| 322 | if (p >= end || *p < '0' || *p > '9') |
| 323 | goto out; |
| 324 | maj *= 10; |
| 325 | maj += (*p - '0'); |
| 326 | p++; |
| 327 | } |
| 328 | |
| 329 | /* check Major version */ |
| 330 | if (!maj) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 331 | goto out; |
| 332 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 333 | p++; /* skip the '.' */ |
| 334 | if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */ |
| 335 | goto out; |
| 336 | |
| 337 | /* Parse Minor number */ |
| 338 | while (p < end) { |
| 339 | if (*p < '0' || *p > '9') |
| 340 | break; |
| 341 | min *= 10; |
| 342 | min += (*p - '0'); |
| 343 | p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 344 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 345 | |
| 346 | /* check Minor number */ |
| 347 | if (min > 999) |
| 348 | goto out; |
| 349 | |
| 350 | /* skip trailing spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 351 | while (p < end && isspace((unsigned char)*p)) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 352 | p++; |
| 353 | if (p != end) |
| 354 | goto out; |
| 355 | |
| 356 | vsn = maj * 1000 + min; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 357 | out: |
| 358 | return vsn; |
| 359 | } |
| 360 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 361 | /* 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] | 362 | * 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] | 363 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 364 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 365 | spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 366 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 367 | struct buffer *chk; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 368 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 369 | char *p, *end; |
| 370 | unsigned int flags = SPOE_FRM_FL_FIN; |
| 371 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 372 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 373 | p = frame; |
| 374 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 375 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 376 | /* Set Frame type */ |
| 377 | *p++ = SPOE_FRM_T_HAPROXY_HELLO; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 378 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 379 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 380 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 381 | memcpy(p, (char *)&flags, 4); |
| 382 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 383 | |
| 384 | /* No stream-id and frame-id for HELLO frames */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 385 | *p++ = 0; *p++ = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 386 | |
| 387 | /* There are 3 mandatory items: "supported-versions", "max-frame-size" |
| 388 | * and "capabilities" */ |
| 389 | |
| 390 | /* "supported-versions" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 391 | sz = SLEN(SUPPORTED_VERSIONS_KEY); |
| 392 | if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1) |
| 393 | goto too_big; |
| 394 | |
| 395 | *p++ = SPOE_DATA_T_STR; |
| 396 | sz = SLEN(SUPPORTED_VERSIONS_VAL); |
| 397 | if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1) |
| 398 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 399 | |
| 400 | /* "max-fram-size" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 401 | sz = SLEN(MAX_FRAME_SIZE_KEY); |
| 402 | if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1) |
| 403 | goto too_big; |
| 404 | |
| 405 | *p++ = SPOE_DATA_T_UINT32; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 406 | if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 407 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 408 | |
| 409 | /* "capabilities" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 410 | sz = SLEN(CAPABILITIES_KEY); |
| 411 | if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1) |
| 412 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 413 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 414 | *p++ = SPOE_DATA_T_STR; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 415 | chk = get_trash_chunk(); |
| 416 | if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 417 | memcpy(chk->area, "pipelining", 10); |
| 418 | chk->data += 10; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 419 | } |
| 420 | if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 421 | if (chk->data) chk->area[chk->data++] = ','; |
| 422 | memcpy(chk->area+chk->data, "async", 5); |
| 423 | chk->data += 5; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 424 | } |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 425 | if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 426 | if (chk->data) chk->area[chk->data++] = ','; |
| 427 | memcpy(chk->area+chk->data, "fragmentation", 13); |
Miroslav Zagorac | 6b3690b | 2019-01-13 16:55:01 +0100 | [diff] [blame] | 428 | chk->data += 13; |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 429 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 430 | if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 431 | goto too_big; |
| 432 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 433 | /* (optional) "engine-id" K/V item, if present */ |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 434 | if (agent != NULL && agent->rt[tid].engine_id != NULL) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 435 | sz = SLEN(ENGINE_ID_KEY); |
| 436 | if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1) |
| 437 | goto too_big; |
| 438 | |
| 439 | *p++ = SPOE_DATA_T_STR; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 440 | sz = strlen(agent->rt[tid].engine_id); |
| 441 | 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] | 442 | goto too_big; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 443 | } |
| 444 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 445 | return (p - frame); |
| 446 | |
| 447 | too_big: |
| 448 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 449 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 450 | } |
| 451 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 452 | /* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of |
| 453 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
| 454 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 455 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 456 | spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 457 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 458 | const char *reason; |
| 459 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 460 | unsigned int flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 461 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 462 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 463 | p = frame; |
| 464 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 465 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 466 | /* Set Frame type */ |
| 467 | *p++ = SPOE_FRM_T_HAPROXY_DISCON; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 468 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 469 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 470 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 471 | memcpy(p, (char *)&flags, 4); |
| 472 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 473 | |
| 474 | /* No stream-id and frame-id for DISCONNECT frames */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 475 | *p++ = 0; *p++ = 0; |
| 476 | |
| 477 | if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS) |
| 478 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 479 | |
| 480 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 481 | |
| 482 | /* "status-code" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 483 | sz = SLEN(STATUS_CODE_KEY); |
| 484 | if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1) |
| 485 | goto too_big; |
| 486 | |
| 487 | *p++ = SPOE_DATA_T_UINT32; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 488 | if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 489 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 490 | |
| 491 | /* "message" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 492 | sz = SLEN(MSG_KEY); |
| 493 | if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1) |
| 494 | goto too_big; |
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 | /*Get the message corresponding to the status code */ |
| 497 | reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]; |
| 498 | |
| 499 | *p++ = SPOE_DATA_T_STR; |
| 500 | sz = strlen(reason); |
| 501 | if (spoe_encode_buffer(reason, sz, &p, end) == -1) |
| 502 | goto too_big; |
| 503 | |
| 504 | return (p - frame); |
| 505 | |
| 506 | too_big: |
| 507 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 508 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 509 | } |
| 510 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 511 | /* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of |
| 512 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
| 513 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 514 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 515 | spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 516 | char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 517 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 518 | char *p, *end; |
| 519 | unsigned int stream_id, frame_id; |
| 520 | unsigned int flags = SPOE_FRM_FL_FIN; |
| 521 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 522 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 523 | p = frame; |
| 524 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 525 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 526 | stream_id = ctx->stream_id; |
| 527 | frame_id = ctx->frame_id; |
| 528 | |
| 529 | if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) { |
| 530 | /* The fragmentation is not supported by the applet */ |
| 531 | if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) { |
| 532 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
| 533 | return -1; |
| 534 | } |
| 535 | flags = ctx->frag_ctx.flags; |
| 536 | } |
| 537 | |
| 538 | /* Set Frame type */ |
| 539 | *p++ = SPOE_FRM_T_HAPROXY_NOTIFY; |
| 540 | |
| 541 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 542 | flags = htonl(flags); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 543 | memcpy(p, (char *)&flags, 4); |
| 544 | p += 4; |
| 545 | |
| 546 | /* Set stream-id and frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 547 | if (encode_varint(stream_id, &p, end) == -1) |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 548 | goto too_big; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 549 | if (encode_varint(frame_id, &p, end) == -1) |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 550 | goto too_big; |
| 551 | |
| 552 | /* Copy encoded messages, if possible */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 553 | sz = b_data(&ctx->buffer); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 554 | if (p + sz >= end) |
| 555 | goto too_big; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 556 | memcpy(p, b_head(&ctx->buffer), sz); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 557 | p += sz; |
| 558 | |
| 559 | return (p - frame); |
| 560 | |
| 561 | too_big: |
| 562 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | /* Encode next part of a fragmented frame sent by HAProxy to an agent. It |
| 567 | * returns the number of encoded bytes in the frame on success, 0 if an encoding |
| 568 | * error occurred and -1 if a fatal error occurred. */ |
| 569 | static int |
| 570 | spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx, |
| 571 | char *frame, size_t size) |
| 572 | { |
| 573 | char *p, *end; |
| 574 | unsigned int stream_id, frame_id; |
| 575 | unsigned int flags; |
| 576 | size_t sz; |
| 577 | |
| 578 | p = frame; |
| 579 | end = frame+size; |
| 580 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 581 | /* <ctx> is null when the stream has aborted the processing of a |
| 582 | * fragmented frame. In this case, we must notify the corresponding |
| 583 | * agent using ids stored in <frag_ctx>. */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 584 | if (ctx == NULL) { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 585 | flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 586 | stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid; |
| 587 | frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid; |
| 588 | } |
| 589 | else { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 590 | flags = ctx->frag_ctx.flags; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 591 | stream_id = ctx->stream_id; |
| 592 | frame_id = ctx->frame_id; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 593 | } |
| 594 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 595 | /* Set Frame type */ |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 596 | *p++ = SPOE_FRM_T_UNSET; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 597 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 598 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 599 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 600 | memcpy(p, (char *)&flags, 4); |
| 601 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 602 | |
| 603 | /* Set stream-id and frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 604 | if (encode_varint(stream_id, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 605 | goto too_big; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 606 | if (encode_varint(frame_id, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 607 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 608 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 609 | if (ctx == NULL) |
| 610 | goto end; |
| 611 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 612 | /* Copy encoded messages, if possible */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 613 | sz = b_data(&ctx->buffer); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 614 | if (p + sz >= end) |
| 615 | goto too_big; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 616 | memcpy(p, b_head(&ctx->buffer), sz); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 617 | p += sz; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 618 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 619 | end: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 620 | return (p - frame); |
| 621 | |
| 622 | too_big: |
| 623 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 624 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 625 | } |
| 626 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 627 | /* Decode and process the HELLO frame sent by an agent. It returns the number of |
| 628 | * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal |
| 629 | * error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 630 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 631 | spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 632 | { |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 633 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
| 634 | char *p, *end; |
| 635 | int vsn, max_frame_size; |
| 636 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 637 | |
| 638 | p = frame; |
| 639 | end = frame + size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 640 | |
| 641 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 642 | if (*p++ != SPOE_FRM_T_AGENT_HELLO) { |
| 643 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 644 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 645 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 646 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 647 | if (size < 7 /* TYPE + METADATA */) { |
| 648 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 649 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 652 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 653 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 654 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 655 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 656 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 657 | /* Fragmentation is not supported for HELLO frame */ |
| 658 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 659 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 660 | return -1; |
| 661 | } |
| 662 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 663 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 664 | if (*p != 0 || *(p+1) != 0) { |
| 665 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 666 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 667 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 668 | p += 2; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 669 | |
| 670 | /* There are 3 mandatory items: "version", "max-frame-size" and |
| 671 | * "capabilities" */ |
| 672 | |
| 673 | /* Loop on K/V items */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 674 | vsn = max_frame_size = flags = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 675 | while (p < end) { |
| 676 | char *str; |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 677 | uint64_t sz; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 678 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 679 | |
| 680 | /* Decode the item key */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 681 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 682 | if (ret == -1 || !sz) { |
| 683 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 684 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 685 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 686 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 687 | /* Check "version" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 688 | if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 689 | int i, type = *p++; |
| 690 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 691 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 692 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 693 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 694 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 695 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 696 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) { |
| 697 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 698 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 699 | } |
| 700 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 701 | vsn = spoe_str_to_vsn(str, sz); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 702 | if (vsn == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 703 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 704 | return -1; |
| 705 | } |
| 706 | for (i = 0; supported_versions[i].str != NULL; ++i) { |
| 707 | if (vsn >= supported_versions[i].min && |
| 708 | vsn <= supported_versions[i].max) |
| 709 | break; |
| 710 | } |
| 711 | if (supported_versions[i].str == NULL) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 712 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 713 | return -1; |
| 714 | } |
| 715 | } |
| 716 | /* Check "max-frame-size" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 717 | 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] | 718 | int type = *p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 719 | |
| 720 | /* The value must be integer */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 721 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 722 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 723 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 724 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 725 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 726 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 727 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 728 | if (decode_varint(&p, end, &sz) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 729 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 730 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 731 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 732 | if (sz < MIN_FRAME_SIZE || |
| 733 | sz > SPOE_APPCTX(appctx)->max_frame_size) { |
| 734 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 735 | return -1; |
| 736 | } |
| 737 | max_frame_size = sz; |
| 738 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 739 | /* Check "capabilities" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 740 | 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] | 741 | int type = *p++; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 742 | |
| 743 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 744 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 745 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 746 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 747 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 748 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) { |
| 749 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 750 | return 0; |
| 751 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 752 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 753 | while (sz) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 754 | char *delim; |
| 755 | |
| 756 | /* Skip leading spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 757 | for (; isspace((unsigned char)*str) && sz; str++, sz--); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 758 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 759 | if (sz >= 10 && !strncmp(str, "pipelining", 10)) { |
| 760 | str += 10; sz -= 10; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 761 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 762 | flags |= SPOE_APPCTX_FL_PIPELINING; |
| 763 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 764 | else if (sz >= 5 && !strncmp(str, "async", 5)) { |
| 765 | str += 5; sz -= 5; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 766 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 767 | flags |= SPOE_APPCTX_FL_ASYNC; |
| 768 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 769 | else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) { |
| 770 | str += 13; sz -= 13; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 771 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 772 | flags |= SPOE_APPCTX_FL_FRAGMENTATION; |
| 773 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 774 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 775 | /* Get the next comma or break */ |
| 776 | if (!sz || (delim = memchr(str, ',', sz)) == NULL) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 777 | break; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 778 | delim++; |
| 779 | sz -= (delim - str); |
| 780 | str = delim; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 781 | } |
| 782 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 783 | else { |
| 784 | /* Silently ignore unknown item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 785 | if (spoe_skip_data(&p, end) == -1) { |
| 786 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 787 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 788 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
| 792 | /* Final checks */ |
| 793 | if (!vsn) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 794 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 795 | return -1; |
| 796 | } |
| 797 | if (!max_frame_size) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 798 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 799 | return -1; |
| 800 | } |
Christopher Faulet | 1138901 | 2019-02-07 16:13:26 +0100 | [diff] [blame] | 801 | if (!agent) |
| 802 | flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC); |
| 803 | else { |
| 804 | if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING)) |
| 805 | flags &= ~SPOE_APPCTX_FL_PIPELINING; |
| 806 | if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC)) |
| 807 | flags &= ~SPOE_APPCTX_FL_ASYNC; |
| 808 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 809 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 810 | SPOE_APPCTX(appctx)->version = (unsigned int)vsn; |
| 811 | SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size; |
| 812 | SPOE_APPCTX(appctx)->flags |= flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 813 | |
| 814 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | /* Decode DISCONNECT frame sent by an agent. It returns the number of by read |
| 818 | * bytes on success, 0 if the frame can be ignored and -1 if an error |
| 819 | * occurred. */ |
| 820 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 821 | spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 822 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 823 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 824 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 825 | |
| 826 | p = frame; |
| 827 | end = frame + size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 828 | |
| 829 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 830 | if (*p++ != SPOE_FRM_T_AGENT_DISCON) { |
| 831 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 832 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 833 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 834 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 835 | if (size < 7 /* TYPE + METADATA */) { |
| 836 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 837 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 838 | } |
| 839 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 840 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 841 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 842 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 843 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 844 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 845 | /* Fragmentation is not supported for DISCONNECT frame */ |
| 846 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 847 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 848 | return -1; |
| 849 | } |
| 850 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 851 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 852 | if (*p != 0 || *(p+1) != 0) { |
| 853 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 854 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 855 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 856 | p += 2; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 857 | |
| 858 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 859 | |
| 860 | /* Loop on K/V items */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 861 | while (p < end) { |
| 862 | char *str; |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 863 | uint64_t sz; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 864 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 865 | |
| 866 | /* Decode the item key */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 867 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 868 | if (ret == -1 || !sz) { |
| 869 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 870 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | /* Check "status-code" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 874 | 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] | 875 | int type = *p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 876 | |
| 877 | /* The value must be an integer */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 878 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 879 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 880 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 881 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 882 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 883 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 884 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 885 | if (decode_varint(&p, end, &sz) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 886 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 887 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 888 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 889 | SPOE_APPCTX(appctx)->status_code = sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | /* Check "message" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 893 | 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] | 894 | int type = *p++; |
| 895 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 896 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 897 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 898 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 899 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 900 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 901 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 902 | if (ret == -1 || sz > 255) { |
| 903 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 904 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 905 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 906 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 907 | SPOE_APPCTX(appctx)->reason = str; |
| 908 | SPOE_APPCTX(appctx)->rlen = sz; |
| 909 | #endif |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 910 | } |
| 911 | else { |
| 912 | /* Silently ignore unknown item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 913 | if (spoe_skip_data(&p, end) == -1) { |
| 914 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 915 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 916 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 920 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 924 | /* 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] | 925 | * success, 0 if the frame can be ignored and -1 if an error occurred. */ |
| 926 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 927 | spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 928 | char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 929 | { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 930 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 931 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 932 | uint64_t stream_id, frame_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 933 | int len; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 934 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 935 | |
| 936 | p = frame; |
| 937 | end = frame + size; |
| 938 | *ctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 939 | |
| 940 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 941 | if (*p++ != SPOE_FRM_T_AGENT_ACK) { |
| 942 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 943 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 944 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 945 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 946 | if (size < 7 /* TYPE + METADATA */) { |
| 947 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 948 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 949 | } |
| 950 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 951 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 952 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 953 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 954 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 955 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 956 | /* Fragmentation is not supported for now */ |
| 957 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 958 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 959 | return -1; |
| 960 | } |
| 961 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 962 | /* Get the stream-id and the frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 963 | if (decode_varint(&p, end, &stream_id) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 964 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 965 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 966 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 967 | if (decode_varint(&p, end, &frame_id) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 968 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 969 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 970 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 971 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 972 | /* Try to find the corresponding SPOE context */ |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 973 | if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 974 | list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 975 | if ((*ctx)->stream_id == (unsigned int)stream_id && |
| 976 | (*ctx)->frame_id == (unsigned int)frame_id) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 977 | goto found; |
| 978 | } |
| 979 | } |
| 980 | else { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 981 | list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) { |
| 982 | if ((*ctx)->stream_id == (unsigned int)stream_id && |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 983 | (*ctx)->frame_id == (unsigned int)frame_id) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 984 | goto found; |
| 985 | } |
| 986 | } |
| 987 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 988 | if (SPOE_APPCTX(appctx)->frag_ctx.ctx && |
| 989 | SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id && |
| 990 | SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) { |
| 991 | |
| 992 | /* ABRT bit is set for an unfinished fragmented frame */ |
| 993 | if (flags & SPOE_FRM_FL_ABRT) { |
| 994 | *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx; |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 995 | (*ctx)->state = SPOE_CTX_ST_ERROR; |
| 996 | (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT; |
| 997 | /* Ignore the payload */ |
| 998 | goto end; |
| 999 | } |
| 1000 | /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */ |
| 1001 | /* For now, we ignore the ack */ |
| 1002 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 1003 | return 0; |
| 1004 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1005 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1006 | /* No Stream found, ignore the frame */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1007 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1008 | " - Ignore ACK frame" |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1009 | " - stream-id=%u - frame-id=%u\n", |
| 1010 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1011 | __FUNCTION__, appctx, |
| 1012 | (unsigned int)stream_id, (unsigned int)frame_id); |
| 1013 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1014 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND; |
Christopher Faulet | c7ba910 | 2020-11-10 14:31:39 +0100 | [diff] [blame] | 1015 | if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) { |
| 1016 | /* Report an error if we are waiting the ack for another frame, |
| 1017 | * but not if there is no longer frame waiting for a ack |
| 1018 | * (timeout) |
| 1019 | */ |
| 1020 | if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) || |
| 1021 | SPOE_APPCTX(appctx)->frag_ctx.ctx) |
| 1022 | return -1; |
| 1023 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1024 | SPOE_APPCTX(appctx)->cur_fpa = 0; |
| 1025 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1026 | return 0; |
| 1027 | |
| 1028 | found: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1029 | if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer, |
| 1030 | &SPOE_APPCTX(appctx)->buffer_wait)) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1031 | *ctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1032 | return 1; /* Retry later */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1033 | } |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1034 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1035 | /* Copy encoded actions */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1036 | len = (end - p); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1037 | memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len); |
| 1038 | b_set_data(&SPOE_APPCTX(appctx)->buffer, len); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1039 | p += len; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1040 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1041 | /* Transfer the buffer ownership to the SPOE context */ |
| 1042 | (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1043 | SPOE_APPCTX(appctx)->buffer = BUF_NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1044 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1045 | (*ctx)->state = SPOE_CTX_ST_DONE; |
| 1046 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1047 | end: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1048 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1049 | " - ACK frame received" |
| 1050 | " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1051 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1052 | __FUNCTION__, appctx, *ctx, (*ctx)->stream_id, |
| 1053 | (*ctx)->frame_id, flags); |
| 1054 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1055 | } |
| 1056 | |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1057 | /* This function is used in cfgparse.c and declared in proto/checks.h. It |
| 1058 | * prepare the request to send to agents during a healthcheck. It returns 0 on |
| 1059 | * success and -1 if an error occurred. */ |
| 1060 | int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1061 | spoe_prepare_healthcheck_request(char **req, int *len) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1062 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1063 | struct appctx appctx; |
| 1064 | struct spoe_appctx spoe_appctx; |
| 1065 | char *frame, *end, buf[MAX_FRAME_SIZE+4]; |
| 1066 | size_t sz; |
| 1067 | int ret; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1068 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1069 | memset(&appctx, 0, sizeof(appctx)); |
| 1070 | memset(&spoe_appctx, 0, sizeof(spoe_appctx)); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1071 | memset(buf, 0, sizeof(buf)); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1072 | |
| 1073 | appctx.ctx.spoe.ptr = &spoe_appctx; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1074 | SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1075 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1076 | frame = buf+4; /* Reserved the 4 first bytes for the frame size */ |
| 1077 | end = frame + MAX_FRAME_SIZE; |
| 1078 | |
| 1079 | ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE); |
| 1080 | if (ret <= 0) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1081 | return -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1082 | frame += ret; |
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 | /* Add "healthcheck" K/V item */ |
| 1085 | sz = SLEN(HEALTHCHECK_KEY); |
| 1086 | if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1) |
| 1087 | return -1; |
| 1088 | *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1089 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1090 | *len = frame - buf; |
| 1091 | sz = htonl(*len - 4); |
| 1092 | memcpy(buf, (char *)&sz, 4); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1093 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1094 | if ((*req = malloc(*len)) == NULL) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1095 | return -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1096 | memcpy(*req, buf, *len); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | /* This function is used in checks.c and declared in proto/checks.h. It decode |
| 1101 | * the response received from an agent during a healthcheck. It returns 0 on |
| 1102 | * success and -1 if an error occurred. */ |
| 1103 | int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1104 | 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] | 1105 | { |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1106 | struct appctx appctx; |
| 1107 | struct spoe_appctx spoe_appctx; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1108 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1109 | memset(&appctx, 0, sizeof(appctx)); |
| 1110 | memset(&spoe_appctx, 0, sizeof(spoe_appctx)); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1111 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1112 | appctx.ctx.spoe.ptr = &spoe_appctx; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1113 | SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1114 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1115 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1116 | spoe_handle_agentdiscon_frame(&appctx, frame, size); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1117 | goto error; |
| 1118 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1119 | if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0) |
| 1120 | goto error; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1121 | |
| 1122 | return 0; |
| 1123 | |
| 1124 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1125 | if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS) |
| 1126 | SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN; |
| 1127 | strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1128 | return -1; |
| 1129 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1130 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1131 | /* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1132 | * the frame can be ignored, 1 to retry later, and the frame length on |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1133 | * success. */ |
| 1134 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1135 | spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1136 | { |
| 1137 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1138 | int ret; |
| 1139 | uint32_t netint; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1140 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1141 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1142 | * length. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1143 | netint = htonl(framesz); |
| 1144 | memcpy(buf, (char *)&netint, 4); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1145 | ret = ci_putblk(si_ic(si), buf, framesz+4); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1146 | if (ret <= 0) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1147 | if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1148 | si_rx_room_blk(si); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1149 | return 1; /* retry */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1150 | } |
| 1151 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1152 | return -1; /* error */ |
| 1153 | } |
| 1154 | return framesz; |
| 1155 | } |
| 1156 | |
| 1157 | /* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0 |
| 1158 | * when the frame can be ignored, 1 to retry later and the frame length on |
| 1159 | * success. */ |
| 1160 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1161 | spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1162 | { |
| 1163 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1164 | int ret; |
| 1165 | uint32_t netint; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1166 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1167 | ret = co_getblk(si_oc(si), (char *)&netint, 4, 0); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1168 | if (ret > 0) { |
| 1169 | framesz = ntohl(netint); |
Christopher Faulet | 06bf59b | 2024-03-18 08:02:32 +0100 | [diff] [blame] | 1170 | if (framesz < 7) { |
| 1171 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 1172 | return -1; |
| 1173 | } |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1174 | if (framesz > SPOE_APPCTX(appctx)->max_frame_size) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1175 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1176 | return -1; |
| 1177 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1178 | ret = co_getblk(si_oc(si), buf, framesz, 4); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1179 | } |
| 1180 | if (ret <= 0) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1181 | if (ret == 0) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1182 | return 1; /* retry */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1183 | } |
| 1184 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1185 | return -1; /* error */ |
| 1186 | } |
| 1187 | return framesz; |
| 1188 | } |
| 1189 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1190 | /******************************************************************** |
| 1191 | * Functions that manage the SPOE applet |
| 1192 | ********************************************************************/ |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1193 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1194 | spoe_wakeup_appctx(struct appctx *appctx) |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1195 | { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1196 | si_want_get(appctx->owner); |
Willy Tarreau | 8bb2ffb | 2018-11-14 17:54:13 +0100 | [diff] [blame] | 1197 | si_rx_endp_more(appctx->owner); |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1198 | appctx_wakeup(appctx); |
| 1199 | return 1; |
| 1200 | } |
| 1201 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1202 | /* Callback function that catches applet timeouts. If a timeout occurred, we set |
| 1203 | * <appctx->st1> flag and the SPOE applet is woken up. */ |
| 1204 | static struct task * |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1205 | spoe_process_appctx(struct task * task, void *context, unsigned int state) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1206 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 1207 | struct appctx *appctx = context; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1208 | |
| 1209 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1210 | if (tick_is_expired(task->expire, now_ms)) { |
| 1211 | task->expire = TICK_ETERNITY; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1212 | appctx->st1 = SPOE_APPCTX_ERR_TOUT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1213 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1214 | spoe_wakeup_appctx(appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1215 | return task; |
| 1216 | } |
| 1217 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1218 | /* Callback function that releases a SPOE applet. This happens when the |
| 1219 | * connection with the agent is closed. */ |
| 1220 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1221 | spoe_release_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1222 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1223 | struct stream_interface *si = appctx->owner; |
| 1224 | struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx); |
| 1225 | struct spoe_agent *agent; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1226 | struct spoe_context *ctx, *back; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1227 | |
| 1228 | if (spoe_appctx == NULL) |
| 1229 | return; |
| 1230 | |
| 1231 | appctx->ctx.spoe.ptr = NULL; |
| 1232 | agent = spoe_appctx->agent; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1233 | |
| 1234 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n", |
| 1235 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1236 | __FUNCTION__, appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1237 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1238 | /* Remove applet from the list of running applets */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1239 | _HA_ATOMIC_DEC(&agent->counters.applets); |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1240 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1241 | if (!LIST_ISEMPTY(&spoe_appctx->list)) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1242 | LIST_DELETE(&spoe_appctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1243 | LIST_INIT(&spoe_appctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1244 | } |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1245 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1246 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1247 | /* Shutdown the server connection, if needed */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1248 | if (appctx->st0 != SPOE_APPCTX_ST_END) { |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1249 | if (appctx->st0 == SPOE_APPCTX_ST_IDLE) { |
| 1250 | eb32_delete(&spoe_appctx->node); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1251 | _HA_ATOMIC_DEC(&agent->counters.idles); |
Christopher Faulet | d1e1aa0 | 2023-04-26 15:56:59 +0200 | [diff] [blame] | 1252 | agent->rt[tid].idles--; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1253 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1254 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1255 | appctx->st0 = SPOE_APPCTX_ST_END; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1256 | if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE) |
| 1257 | spoe_appctx->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1258 | |
| 1259 | si_shutw(si); |
| 1260 | si_shutr(si); |
| 1261 | si_ic(si)->flags |= CF_READ_NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1262 | } |
| 1263 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1264 | /* Destroy the task attached to this applet */ |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 1265 | task_destroy(spoe_appctx->task); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1266 | |
Christopher Faulet | ee9b4a6 | 2022-08-25 18:50:18 +0200 | [diff] [blame] | 1267 | /* Report an error to all streams in the appctx waiting queue */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1268 | list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1269 | LIST_DELETE(&ctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1270 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1271 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1272 | spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting); |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1273 | ctx->spoe_appctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1274 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1275 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1276 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1277 | } |
| 1278 | |
Christopher Faulet | ee9b4a6 | 2022-08-25 18:50:18 +0200 | [diff] [blame] | 1279 | /* If the applet was processing a fragmented frame, report an error to |
| 1280 | * the corresponding stream. */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1281 | if (spoe_appctx->frag_ctx.ctx) { |
| 1282 | ctx = spoe_appctx->frag_ctx.ctx; |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1283 | ctx->spoe_appctx = NULL; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1284 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1285 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1286 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1287 | } |
| 1288 | |
Christopher Faulet | ee9b4a6 | 2022-08-25 18:50:18 +0200 | [diff] [blame] | 1289 | if (!LIST_ISEMPTY(&agent->rt[tid].applets)) { |
| 1290 | /* If there are still some running applets, remove reference on |
| 1291 | * the current one from streams in the async waiting queue. In |
| 1292 | * async mode, the ACK may be received from another appctx. |
| 1293 | */ |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1294 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) { |
| 1295 | if (ctx->spoe_appctx == spoe_appctx) |
| 1296 | ctx->spoe_appctx = NULL; |
| 1297 | } |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1298 | goto end; |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1299 | } |
Christopher Faulet | 4d350af | 2021-08-02 17:53:56 +0200 | [diff] [blame] | 1300 | else { |
Christopher Faulet | ee9b4a6 | 2022-08-25 18:50:18 +0200 | [diff] [blame] | 1301 | /* It is the last running applet and the sending and async |
| 1302 | * waiting queues are not empty. So try to start a new applet if |
| 1303 | * HAproxy is not stopping. On success, we remove reference on |
| 1304 | * the current appctx from streams in the async waiting queue. |
| 1305 | * In async mode, the ACK may be received from another appctx. |
Christopher Faulet | 4d350af | 2021-08-02 17:53:56 +0200 | [diff] [blame] | 1306 | */ |
| 1307 | if (!stopping && |
| 1308 | (!LIST_ISEMPTY(&agent->rt[tid].sending_queue) || !LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) && |
Christopher Faulet | ee9b4a6 | 2022-08-25 18:50:18 +0200 | [diff] [blame] | 1309 | spoe_create_appctx(agent->spoe_conf)) { |
| 1310 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) { |
| 1311 | if (ctx->spoe_appctx == spoe_appctx) |
| 1312 | ctx->spoe_appctx = NULL; |
| 1313 | } |
Christopher Faulet | 4d350af | 2021-08-02 17:53:56 +0200 | [diff] [blame] | 1314 | goto end; |
Christopher Faulet | ee9b4a6 | 2022-08-25 18:50:18 +0200 | [diff] [blame] | 1315 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1316 | |
Christopher Faulet | ee9b4a6 | 2022-08-25 18:50:18 +0200 | [diff] [blame] | 1317 | /* Otherwise, report an error to all streams in the sending and |
| 1318 | * async waiting queues. |
| 1319 | */ |
Christopher Faulet | 4d350af | 2021-08-02 17:53:56 +0200 | [diff] [blame] | 1320 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) { |
| 1321 | LIST_DELETE(&ctx->list); |
| 1322 | LIST_INIT(&ctx->list); |
| 1323 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
| 1324 | spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue); |
| 1325 | ctx->spoe_appctx = NULL; |
| 1326 | ctx->state = SPOE_CTX_ST_ERROR; |
| 1327 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
| 1328 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1329 | } |
| 1330 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) { |
| 1331 | LIST_DELETE(&ctx->list); |
| 1332 | LIST_INIT(&ctx->list); |
| 1333 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
| 1334 | spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting); |
| 1335 | ctx->spoe_appctx = NULL; |
| 1336 | ctx->state = SPOE_CTX_ST_ERROR; |
| 1337 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
| 1338 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1339 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1340 | } |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1341 | |
| 1342 | end: |
Christopher Faulet | 55ae8a6 | 2019-05-23 22:47:48 +0200 | [diff] [blame] | 1343 | /* Release allocated memory */ |
| 1344 | spoe_release_buffer(&spoe_appctx->buffer, |
| 1345 | &spoe_appctx->buffer_wait); |
| 1346 | pool_free(pool_head_spoe_appctx, spoe_appctx); |
| 1347 | |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1348 | /* Update runtinme agent info */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1349 | agent->rt[tid].frame_size = agent->max_frame_size; |
| 1350 | list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list) |
| 1351 | 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] | 1352 | } |
| 1353 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1354 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1355 | spoe_handle_connect_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1356 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1357 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1358 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1359 | char *frame, *buf; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1360 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1361 | |
Willy Tarreau | 7ab22adb | 2019-06-05 14:53:22 +0200 | [diff] [blame] | 1362 | if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) { |
| 1363 | /* closed */ |
| 1364 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
| 1365 | goto exit; |
| 1366 | } |
| 1367 | |
Willy Tarreau | 4f283fa | 2019-06-05 14:34:03 +0200 | [diff] [blame] | 1368 | if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) { |
Willy Tarreau | 7ab22adb | 2019-06-05 14:53:22 +0200 | [diff] [blame] | 1369 | /* not connected yet */ |
Willy Tarreau | 8bb2ffb | 2018-11-14 17:54:13 +0100 | [diff] [blame] | 1370 | si_rx_endp_more(si); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1371 | task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG); |
| 1372 | goto stop; |
| 1373 | } |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1374 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1375 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1376 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1377 | " - Connection timed out\n", |
| 1378 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1379 | __FUNCTION__, appctx); |
| 1380 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1381 | goto exit; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1382 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1383 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1384 | if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1385 | SPOE_APPCTX(appctx)->task->expire = |
| 1386 | tick_add_ifset(now_ms, agent->timeout.hello); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1387 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1388 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1389 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1390 | buf = trash.area; frame = buf+4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1391 | ret = spoe_prepare_hahello_frame(appctx, frame, |
| 1392 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1393 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1394 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1395 | |
| 1396 | switch (ret) { |
| 1397 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1398 | case 0: /* ignore => an error, cannot be ignored */ |
| 1399 | goto exit; |
| 1400 | |
| 1401 | case 1: /* retry later */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1402 | goto stop; |
| 1403 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1404 | default: |
| 1405 | /* HELLO frame successfully sent, now wait for the |
| 1406 | * reply. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1407 | appctx->st0 = SPOE_APPCTX_ST_CONNECTING; |
| 1408 | goto next; |
| 1409 | } |
| 1410 | |
| 1411 | next: |
| 1412 | return 0; |
| 1413 | stop: |
| 1414 | return 1; |
| 1415 | exit: |
| 1416 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1417 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1418 | } |
| 1419 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1420 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1421 | spoe_handle_connecting_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1422 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1423 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1424 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1425 | char *frame; |
| 1426 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1427 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1428 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1429 | 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] | 1430 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1431 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1432 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1433 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1434 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1435 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1436 | " - Connection timed out\n", |
| 1437 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1438 | __FUNCTION__, appctx); |
| 1439 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1440 | goto exit; |
| 1441 | } |
| 1442 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1443 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1444 | ret = spoe_recv_frame(appctx, frame, |
| 1445 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1446 | if (ret > 1) { |
| 1447 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1448 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1449 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1450 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1451 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1452 | ret = spoe_handle_agenthello_frame(appctx, frame, ret); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1453 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1454 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1455 | switch (ret) { |
| 1456 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1457 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1458 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1459 | goto next; |
| 1460 | |
| 1461 | case 1: /* retry later */ |
| 1462 | goto stop; |
| 1463 | |
| 1464 | default: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1465 | _HA_ATOMIC_INC(&agent->counters.idles); |
Christopher Faulet | d1e1aa0 | 2023-04-26 15:56:59 +0200 | [diff] [blame] | 1466 | agent->rt[tid].idles++; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1467 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1468 | SPOE_APPCTX(appctx)->node.key = 0; |
| 1469 | eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node); |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1470 | |
| 1471 | /* Update runtinme agent info */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1472 | 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] | 1473 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1474 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1475 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1476 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1477 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1478 | if (trash.data) |
| 1479 | co_skip(si_oc(si), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1480 | |
| 1481 | SPOE_APPCTX(appctx)->task->expire = |
| 1482 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1483 | return 0; |
| 1484 | stop: |
| 1485 | return 1; |
| 1486 | exit: |
| 1487 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1488 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1489 | } |
| 1490 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1491 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1492 | static int |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1493 | spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1494 | { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1495 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
| 1496 | struct spoe_context *ctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1497 | char *frame, *buf; |
| 1498 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1499 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1500 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1501 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1502 | buf = trash.area; frame = buf+4; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1503 | |
| 1504 | if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) { |
| 1505 | ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx; |
| 1506 | ret = spoe_prepare_hafrag_frame(appctx, ctx, frame, |
| 1507 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1508 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1509 | else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1510 | *skip = 1; |
| 1511 | ret = 1; |
| 1512 | goto end; |
| 1513 | } |
| 1514 | else { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1515 | ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1516 | ret = spoe_prepare_hanotify_frame(appctx, ctx, frame, |
| 1517 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1518 | |
| 1519 | } |
| 1520 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1521 | if (ret > 1) |
| 1522 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1523 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1524 | switch (ret) { |
| 1525 | case -1: /* error */ |
| 1526 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1527 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1528 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1529 | case 0: /* ignore */ |
| 1530 | if (ctx == NULL) |
| 1531 | goto abort_frag_frame; |
| 1532 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1533 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1534 | LIST_DELETE(&ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1535 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1536 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1537 | spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue); |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1538 | ctx->spoe_appctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1539 | ctx->state = SPOE_CTX_ST_ERROR; |
| 1540 | ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100); |
| 1541 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1542 | *skip = 1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1543 | break; |
| 1544 | |
| 1545 | case 1: /* retry */ |
| 1546 | *skip = 1; |
| 1547 | break; |
| 1548 | |
| 1549 | default: |
| 1550 | if (ctx == NULL) |
| 1551 | goto abort_frag_frame; |
| 1552 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1553 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1554 | LIST_DELETE(&ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1555 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1556 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1557 | spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue); |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1558 | ctx->spoe_appctx = SPOE_APPCTX(appctx); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1559 | if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) || |
| 1560 | (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN)) |
| 1561 | goto no_frag_frame_sent; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1562 | else |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1563 | goto frag_frame_sent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1564 | } |
| 1565 | goto end; |
| 1566 | |
| 1567 | frag_frame_sent: |
| 1568 | appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1569 | *skip = 1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1570 | SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx; |
| 1571 | SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id; |
| 1572 | SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1573 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
| 1574 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1575 | goto end; |
| 1576 | |
| 1577 | no_frag_frame_sent: |
| 1578 | if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) { |
| 1579 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1580 | LIST_APPEND(&agent->rt[tid].waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1581 | } |
| 1582 | else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) { |
| 1583 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1584 | LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1585 | } |
| 1586 | else { |
| 1587 | appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1588 | *skip = 1; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1589 | LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1590 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1591 | _HA_ATOMIC_INC(&agent->counters.nb_waiting); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1592 | ctx->stats.tv_wait = now; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1593 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1594 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1595 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1596 | SPOE_APPCTX(appctx)->cur_fpa++; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1597 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1598 | ctx->state = SPOE_CTX_ST_WAITING_ACK; |
| 1599 | goto end; |
| 1600 | |
| 1601 | abort_frag_frame: |
| 1602 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1603 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1604 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1605 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1606 | goto end; |
| 1607 | |
| 1608 | end: |
| 1609 | return ret; |
| 1610 | } |
| 1611 | |
| 1612 | static int |
| 1613 | spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip) |
| 1614 | { |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 1615 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1616 | struct spoe_context *ctx = NULL; |
| 1617 | char *frame; |
| 1618 | int ret; |
| 1619 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1620 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1621 | ret = spoe_recv_frame(appctx, frame, |
| 1622 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1623 | if (ret > 1) { |
| 1624 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1625 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1626 | ret = -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1627 | goto end; |
| 1628 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1629 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1630 | ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret); |
| 1631 | } |
| 1632 | switch (ret) { |
| 1633 | case -1: /* error */ |
| 1634 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1635 | break; |
| 1636 | |
| 1637 | case 0: /* ignore */ |
| 1638 | break; |
| 1639 | |
| 1640 | case 1: /* retry */ |
| 1641 | *skip = 1; |
| 1642 | break; |
| 1643 | |
| 1644 | default: |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1645 | LIST_DELETE(&ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1646 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1647 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1648 | spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting); |
| 1649 | ctx->stats.tv_response = now; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1650 | if (ctx->spoe_appctx) { |
| 1651 | ctx->spoe_appctx->cur_fpa--; |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1652 | ctx->spoe_appctx = NULL; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1653 | } |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1654 | if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY && |
| 1655 | ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) { |
| 1656 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1657 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1658 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1659 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1660 | } |
| 1661 | else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) |
| 1662 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1663 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1664 | break; |
| 1665 | } |
| 1666 | |
| 1667 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1668 | if (trash.data) |
| 1669 | co_skip(si_oc(appctx->owner), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1670 | end: |
| 1671 | return ret; |
| 1672 | } |
| 1673 | |
| 1674 | static int |
| 1675 | spoe_handle_processing_appctx(struct appctx *appctx) |
| 1676 | { |
| 1677 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | f028bb6 | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1678 | struct server *srv = objt_server(si_strm(si)->target); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1679 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | f028bb6 | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1680 | int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0, close_asap = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1681 | |
| 1682 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) { |
| 1683 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
| 1684 | goto exit; |
| 1685 | } |
| 1686 | |
| 1687 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
| 1688 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
| 1689 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1690 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1691 | goto next; |
| 1692 | } |
| 1693 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1694 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1695 | " - 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] | 1696 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1697 | __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa, |
| 1698 | agent->max_fpa, spoe_appctx_state_str[appctx->st0], |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1699 | SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1700 | |
Christopher Faulet | f028bb6 | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1701 | |
| 1702 | /* Close the applet ASAP because some sessions are waiting for a free |
| 1703 | * connection slot. It is only an issue in multithreaded mode. |
| 1704 | */ |
| 1705 | close_asap = (global.nbthread > 1 && |
| 1706 | (agent->b.be->nbpend || |
| 1707 | (srv && (srv->nbpend || (srv->maxconn && srv->served >= srv_dynamic_maxconn(srv)))))); |
| 1708 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1709 | /* receiving_frame loop */ |
| 1710 | while (!skip_receiving) { |
| 1711 | ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving); |
| 1712 | switch (ret) { |
| 1713 | case -1: /* error */ |
| 1714 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1715 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1716 | case 0: /* ignore */ |
| 1717 | active_r = 1; |
| 1718 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1719 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1720 | case 1: /* retry */ |
| 1721 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1722 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1723 | default: |
| 1724 | active_r = 1; |
| 1725 | break; |
| 1726 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1727 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1728 | |
Christopher Faulet | 887d1c4 | 2023-06-05 08:15:59 +0200 | [diff] [blame] | 1729 | /* Don"t try to send new frame we are waiting for at lease a ack, in |
| 1730 | * sync mode or if applet must be closed ASAP |
| 1731 | */ |
| 1732 | if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK || (close_asap && SPOE_APPCTX(appctx)->cur_fpa)) |
| 1733 | skip_sending = 1; |
| 1734 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1735 | /* send_frame loop */ |
| 1736 | while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) { |
| 1737 | ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending); |
| 1738 | switch (ret) { |
| 1739 | case -1: /* error */ |
| 1740 | goto next; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1741 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1742 | case 0: /* ignore */ |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1743 | if (SPOE_APPCTX(appctx)->node.key) |
| 1744 | SPOE_APPCTX(appctx)->node.key--; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1745 | active_s++; |
| 1746 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1747 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1748 | case 1: /* retry */ |
| 1749 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1750 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1751 | default: |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1752 | if (SPOE_APPCTX(appctx)->node.key) |
| 1753 | SPOE_APPCTX(appctx)->node.key--; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1754 | active_s++; |
| 1755 | break; |
| 1756 | } |
Christopher Faulet | f028bb6 | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1757 | |
| 1758 | /* if applet must be close ASAP, don't send more than a frame */ |
| 1759 | if (close_asap) |
| 1760 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1761 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1762 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1763 | if (active_s || active_r) { |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1764 | update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s); |
| 1765 | SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle); |
| 1766 | } |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1767 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1768 | if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) { |
Christopher Faulet | f028bb6 | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1769 | /* If applet must be closed, don't switch it in IDLE state and |
| 1770 | * close it when the last waiting frame is acknowledged. |
Christopher Faulet | 9e647e5 | 2021-03-01 15:01:14 +0100 | [diff] [blame] | 1771 | */ |
Christopher Faulet | f028bb6 | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1772 | if (close_asap) { |
| 1773 | if (SPOE_APPCTX(appctx)->cur_fpa) |
| 1774 | goto out; |
Christopher Faulet | 9e647e5 | 2021-03-01 15:01:14 +0100 | [diff] [blame] | 1775 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
| 1776 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1777 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1778 | goto next; |
| 1779 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1780 | _HA_ATOMIC_INC(&agent->counters.idles); |
Christopher Faulet | d1e1aa0 | 2023-04-26 15:56:59 +0200 | [diff] [blame] | 1781 | agent->rt[tid].idles++; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1782 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1783 | eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1784 | } |
Christopher Faulet | f028bb6 | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1785 | |
| 1786 | out: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1787 | return 1; |
| 1788 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1789 | next: |
| 1790 | SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle); |
| 1791 | return 0; |
| 1792 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1793 | exit: |
| 1794 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1795 | return 0; |
| 1796 | } |
| 1797 | |
| 1798 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1799 | spoe_handle_disconnect_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1800 | { |
| 1801 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1802 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1803 | char *frame, *buf; |
| 1804 | int ret; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1805 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1806 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) |
| 1807 | goto exit; |
| 1808 | |
| 1809 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) |
| 1810 | goto exit; |
| 1811 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1812 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1813 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1814 | buf = trash.area; frame = buf+4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1815 | ret = spoe_prepare_hadiscon_frame(appctx, frame, |
| 1816 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1817 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1818 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1819 | |
| 1820 | switch (ret) { |
| 1821 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1822 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1823 | goto exit; |
| 1824 | |
| 1825 | case 1: /* retry */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1826 | goto stop; |
| 1827 | |
| 1828 | default: |
| 1829 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1830 | " - disconnected by HAProxy (%d): %s\n", |
| 1831 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1832 | __FUNCTION__, appctx, |
| 1833 | SPOE_APPCTX(appctx)->status_code, |
| 1834 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1835 | |
| 1836 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1837 | goto next; |
| 1838 | } |
| 1839 | |
| 1840 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1841 | SPOE_APPCTX(appctx)->task->expire = |
| 1842 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1843 | return 0; |
| 1844 | stop: |
| 1845 | return 1; |
| 1846 | exit: |
| 1847 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1848 | return 0; |
| 1849 | } |
| 1850 | |
| 1851 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1852 | spoe_handle_disconnecting_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1853 | { |
| 1854 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1855 | char *frame; |
| 1856 | int ret; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1857 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1858 | 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] | 1859 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1860 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1861 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1862 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1863 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1864 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1865 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1866 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1867 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1868 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1869 | ret = spoe_recv_frame(appctx, frame, |
| 1870 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1871 | if (ret > 1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1872 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1873 | ret = spoe_handle_agentdiscon_frame(appctx, frame, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | switch (ret) { |
| 1877 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1878 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1879 | " - error on frame (%s)\n", |
| 1880 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1881 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1882 | __FUNCTION__, appctx, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1883 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1884 | goto exit; |
| 1885 | |
| 1886 | case 0: /* ignore */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1887 | goto next; |
| 1888 | |
| 1889 | case 1: /* retry */ |
| 1890 | goto stop; |
| 1891 | |
| 1892 | default: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1893 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1894 | " - disconnected by peer (%d): %.*s\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1895 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1896 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1897 | __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code, |
| 1898 | SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1899 | goto exit; |
| 1900 | } |
| 1901 | |
| 1902 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1903 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1904 | if (trash.data) |
| 1905 | co_skip(si_oc(appctx->owner), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1906 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1907 | return 0; |
| 1908 | stop: |
| 1909 | return 1; |
| 1910 | exit: |
| 1911 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1912 | return 0; |
| 1913 | } |
| 1914 | |
| 1915 | /* I/O Handler processing messages exchanged with the agent */ |
| 1916 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1917 | spoe_handle_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1918 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1919 | struct stream_interface *si = appctx->owner; |
| 1920 | struct spoe_agent *agent; |
| 1921 | |
| 1922 | if (SPOE_APPCTX(appctx) == NULL) |
| 1923 | return; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1924 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1925 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
| 1926 | agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1927 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1928 | switchstate: |
| 1929 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1930 | " - appctx-state=%s\n", |
| 1931 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1932 | __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]); |
| 1933 | |
| 1934 | switch (appctx->st0) { |
| 1935 | case SPOE_APPCTX_ST_CONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1936 | if (spoe_handle_connect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1937 | goto out; |
| 1938 | goto switchstate; |
| 1939 | |
| 1940 | case SPOE_APPCTX_ST_CONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1941 | if (spoe_handle_connecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1942 | goto out; |
| 1943 | goto switchstate; |
| 1944 | |
| 1945 | case SPOE_APPCTX_ST_IDLE: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1946 | _HA_ATOMIC_DEC(&agent->counters.idles); |
Christopher Faulet | d1e1aa0 | 2023-04-26 15:56:59 +0200 | [diff] [blame] | 1947 | agent->rt[tid].idles--; |
Christopher Faulet | 7d9f1ba | 2018-02-28 13:33:26 +0100 | [diff] [blame] | 1948 | eb32_delete(&SPOE_APPCTX(appctx)->node); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1949 | if (stopping && |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1950 | LIST_ISEMPTY(&agent->rt[tid].sending_queue) && |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1951 | LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1952 | SPOE_APPCTX(appctx)->task->expire = |
| 1953 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1954 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1955 | goto switchstate; |
| 1956 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1957 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1958 | /* fall through */ |
| 1959 | |
| 1960 | case SPOE_APPCTX_ST_PROCESSING: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1961 | case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY: |
| 1962 | case SPOE_APPCTX_ST_WAITING_SYNC_ACK: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1963 | if (spoe_handle_processing_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1964 | goto out; |
| 1965 | goto switchstate; |
| 1966 | |
| 1967 | case SPOE_APPCTX_ST_DISCONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1968 | if (spoe_handle_disconnect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1969 | goto out; |
| 1970 | goto switchstate; |
| 1971 | |
| 1972 | case SPOE_APPCTX_ST_DISCONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1973 | if (spoe_handle_disconnecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1974 | goto out; |
| 1975 | goto switchstate; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1976 | |
| 1977 | case SPOE_APPCTX_ST_EXIT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1978 | appctx->st0 = SPOE_APPCTX_ST_END; |
| 1979 | SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY; |
| 1980 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1981 | si_shutw(si); |
| 1982 | si_shutr(si); |
| 1983 | si_ic(si)->flags |= CF_READ_NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1984 | /* fall through */ |
| 1985 | |
| 1986 | case SPOE_APPCTX_ST_END: |
Christopher Faulet | 06bf59b | 2024-03-18 08:02:32 +0100 | [diff] [blame] | 1987 | co_skip(si_oc(si), co_data(si_oc(si))); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1988 | return; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1989 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1990 | out: |
Christopher Faulet | f15ac82 | 2024-03-14 10:49:01 +0100 | [diff] [blame] | 1991 | if (stopping && appctx->st0 == SPOE_APPCTX_ST_IDLE) |
| 1992 | task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_MSG); |
| 1993 | else if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY) |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1994 | task_queue(SPOE_APPCTX(appctx)->task); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | struct applet spoe_applet = { |
| 1998 | .obj_type = OBJ_TYPE_APPLET, |
| 1999 | .name = "<SPOE>", /* used for logging */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2000 | .fct = spoe_handle_appctx, |
| 2001 | .release = spoe_release_appctx, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2002 | }; |
| 2003 | |
| 2004 | /* Create a SPOE applet. On success, the created applet is returned, else |
| 2005 | * NULL. */ |
| 2006 | static struct appctx * |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2007 | spoe_create_appctx(struct spoe_config *conf) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2008 | { |
Christopher Faulet | e3df8b2 | 2024-01-05 17:06:48 +0100 | [diff] [blame] | 2009 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2010 | struct appctx *appctx; |
| 2011 | struct session *sess; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2012 | struct stream *strm; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2013 | |
Christopher Faulet | e3df8b2 | 2024-01-05 17:06:48 +0100 | [diff] [blame] | 2014 | /* Do not try to create a new applet if there is no server up for the |
| 2015 | * agent's backend. */ |
| 2016 | if (!agent->b.be->srv_act && !agent->b.be->srv_bck) { |
| 2017 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: don't create SPOE appctx: no server up\n", |
| 2018 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__); |
| 2019 | goto out; |
| 2020 | } |
| 2021 | |
| 2022 | /* Do not try to create a new applet if we have reached the maximum of |
| 2023 | * connection per seconds */ |
| 2024 | if (agent->cps_max > 0) { |
| 2025 | if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) { |
| 2026 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: don't create SPOE appctx: max CPS reached\n", |
| 2027 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__); |
| 2028 | goto out; |
| 2029 | } |
| 2030 | } |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 2031 | if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2032 | goto out_error; |
| 2033 | |
Willy Tarreau | c9ef9bc | 2021-03-22 21:04:50 +0100 | [diff] [blame] | 2034 | appctx->ctx.spoe.ptr = pool_zalloc(pool_head_spoe_appctx); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2035 | if (SPOE_APPCTX(appctx) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2036 | goto out_free_appctx; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2037 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2038 | appctx->st0 = SPOE_APPCTX_ST_CONNECT; |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2039 | if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL) |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2040 | goto out_free_spoe_appctx; |
| 2041 | |
| 2042 | SPOE_APPCTX(appctx)->owner = appctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2043 | SPOE_APPCTX(appctx)->task->process = spoe_process_appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2044 | SPOE_APPCTX(appctx)->task->context = appctx; |
Christopher Faulet | e3df8b2 | 2024-01-05 17:06:48 +0100 | [diff] [blame] | 2045 | SPOE_APPCTX(appctx)->agent = agent; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2046 | SPOE_APPCTX(appctx)->version = 0; |
Christopher Faulet | e3df8b2 | 2024-01-05 17:06:48 +0100 | [diff] [blame] | 2047 | SPOE_APPCTX(appctx)->max_frame_size = agent->max_frame_size; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2048 | SPOE_APPCTX(appctx)->flags = 0; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2049 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2050 | SPOE_APPCTX(appctx)->buffer = BUF_NULL; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 2051 | SPOE_APPCTX(appctx)->cur_fpa = 0; |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2052 | |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2053 | LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list); |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2054 | SPOE_APPCTX(appctx)->buffer_wait.target = appctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2055 | SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2056 | |
| 2057 | LIST_INIT(&SPOE_APPCTX(appctx)->list); |
| 2058 | LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2059 | |
Willy Tarreau | 5820a36 | 2016-12-22 15:59:02 +0100 | [diff] [blame] | 2060 | sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2061 | if (!sess) |
| 2062 | goto out_free_spoe; |
| 2063 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2064 | if ((strm = stream_new(sess, &appctx->obj_type, &BUF_NULL)) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2065 | goto out_free_sess; |
| 2066 | |
Christopher Faulet | e3df8b2 | 2024-01-05 17:06:48 +0100 | [diff] [blame] | 2067 | stream_set_backend(strm, agent->b.be); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2068 | |
| 2069 | /* applet is waiting for data */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2070 | si_cant_get(&strm->si[0]); |
Christopher Faulet | e3df8b2 | 2024-01-05 17:06:48 +0100 | [diff] [blame] | 2071 | |
| 2072 | /* Increase the per-process number of cumulated connections */ |
| 2073 | if (agent->cps_max > 0) |
| 2074 | update_freq_ctr(&agent->rt[tid].conn_per_sec, 1); |
| 2075 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2076 | appctx_wakeup(appctx); |
| 2077 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2078 | strm->do_log = NULL; |
| 2079 | strm->res.flags |= CF_READ_DONTWAIT; |
| 2080 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2081 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2082 | LIST_APPEND(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2083 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2084 | _HA_ATOMIC_INC(&conf->agent->counters.applets); |
Emeric Brun | 5f77fef | 2017-05-29 15:26:51 +0200 | [diff] [blame] | 2085 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 2086 | task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT); |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2087 | task_wakeup(strm->task, TASK_WOKEN_INIT); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2088 | return appctx; |
| 2089 | |
| 2090 | /* Error unrolling */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2091 | out_free_sess: |
| 2092 | session_free(sess); |
| 2093 | out_free_spoe: |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 2094 | task_destroy(SPOE_APPCTX(appctx)->task); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2095 | out_free_spoe_appctx: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2096 | pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx)); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2097 | out_free_appctx: |
| 2098 | appctx_free(appctx); |
| 2099 | out_error: |
Christopher Faulet | e3df8b2 | 2024-01-05 17:06:48 +0100 | [diff] [blame] | 2100 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: failed to create SPOE appctx\n", |
| 2101 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__); |
| 2102 | send_log(&conf->agent_fe, LOG_EMERG, "SPOE: [%s] failed to create SPOE applet\n", agent->id); |
| 2103 | out: |
| 2104 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2105 | return NULL; |
| 2106 | } |
| 2107 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2108 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2109 | spoe_queue_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2110 | { |
| 2111 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2112 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2113 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2114 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2115 | /* Check if we need to create a new SPOE applet or not. */ |
Christopher Faulet | d1e1aa0 | 2023-04-26 15:56:59 +0200 | [diff] [blame] | 2116 | if (agent->rt[tid].processing < agent->rt[tid].idles || |
| 2117 | 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] | 2118 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2119 | |
| 2120 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2121 | " - try to create new SPOE appctx\n", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2122 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__, |
| 2123 | ctx->strm); |
| 2124 | |
Christopher Faulet | e3df8b2 | 2024-01-05 17:06:48 +0100 | [diff] [blame] | 2125 | spoe_create_appctx(conf); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2126 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2127 | end: |
| 2128 | /* 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] | 2129 | if (LIST_ISEMPTY(&agent->rt[tid].applets)) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2130 | ctx->status_code = SPOE_CTX_ERR_RES; |
| 2131 | return -1; |
| 2132 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2133 | |
Christopher Faulet | 3e86cec | 2019-04-10 14:02:12 +0200 | [diff] [blame] | 2134 | /* Add the SPOE context in the sending queue if the stream has no applet |
| 2135 | * already assigned and wakeup all idle applets. Otherwise, don't queue |
| 2136 | * it. */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2137 | _HA_ATOMIC_INC(&agent->counters.nb_sending); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2138 | spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request); |
| 2139 | ctx->stats.tv_queue = now; |
Christopher Faulet | 3e86cec | 2019-04-10 14:02:12 +0200 | [diff] [blame] | 2140 | if (ctx->spoe_appctx) |
| 2141 | return 1; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2142 | LIST_APPEND(&agent->rt[tid].sending_queue, &ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2143 | |
| 2144 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2145 | " - Add stream in sending queue" |
Christopher Faulet | 68db023 | 2018-04-06 11:34:12 +0200 | [diff] [blame] | 2146 | " - applets=%u - idles=%u - processing=%u\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2147 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__, |
Christopher Faulet | 68db023 | 2018-04-06 11:34:12 +0200 | [diff] [blame] | 2148 | ctx->strm, agent->counters.applets, agent->counters.idles, |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2149 | agent->rt[tid].processing); |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2150 | |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 2151 | /* Finally try to wakeup an IDLE applet. */ |
| 2152 | if (!eb_is_empty(&agent->rt[tid].idle_applets)) { |
| 2153 | struct eb32_node *node; |
| 2154 | |
| 2155 | node = eb32_first(&agent->rt[tid].idle_applets); |
| 2156 | spoe_appctx = eb32_entry(node, struct spoe_appctx, node); |
| 2157 | if (node && spoe_appctx) { |
| 2158 | eb32_delete(&spoe_appctx->node); |
| 2159 | spoe_appctx->node.key++; |
| 2160 | eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node); |
| 2161 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2162 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2163 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2164 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | /*************************************************************************** |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2168 | * Functions that encode SPOE messages |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2169 | **************************************************************************/ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2170 | /* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle |
| 2171 | * fragmented_content. If the next message can be processed, it returns 0. If |
| 2172 | * the message is too big, it returns -1.*/ |
| 2173 | static int |
| 2174 | spoe_encode_message(struct stream *s, struct spoe_context *ctx, |
| 2175 | struct spoe_message *msg, int dir, |
| 2176 | char **buf, char *end) |
| 2177 | { |
| 2178 | struct sample *smp; |
| 2179 | struct spoe_arg *arg; |
| 2180 | int ret; |
| 2181 | |
| 2182 | if (msg->cond) { |
| 2183 | ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2184 | ret = acl_pass(ret); |
| 2185 | if (msg->cond->pol == ACL_COND_UNLESS) |
| 2186 | ret = !ret; |
| 2187 | |
| 2188 | /* the rule does not match */ |
| 2189 | if (!ret) |
| 2190 | goto next; |
| 2191 | } |
| 2192 | |
| 2193 | /* Resume encoding of a SPOE argument */ |
| 2194 | if (ctx->frag_ctx.curarg != NULL) { |
| 2195 | arg = ctx->frag_ctx.curarg; |
| 2196 | goto encode_argument; |
| 2197 | } |
| 2198 | |
| 2199 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2200 | goto encode_msg_payload; |
| 2201 | |
| 2202 | /* Check if there is enough space for the message name and the |
| 2203 | * number of arguments. It implies <msg->id_len> is encoded on 2 |
| 2204 | * bytes, at most (< 2288). */ |
| 2205 | if (*buf + 2 + msg->id_len + 1 > end) |
| 2206 | goto too_big; |
| 2207 | |
| 2208 | /* Encode the message name */ |
| 2209 | if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1) |
| 2210 | goto too_big; |
| 2211 | |
| 2212 | /* Set the number of arguments for this message */ |
| 2213 | **buf = msg->nargs; |
| 2214 | (*buf)++; |
| 2215 | |
| 2216 | ctx->frag_ctx.curoff = 0; |
| 2217 | encode_msg_payload: |
| 2218 | |
| 2219 | /* Loop on arguments */ |
| 2220 | list_for_each_entry(arg, &msg->args, list) { |
| 2221 | ctx->frag_ctx.curarg = arg; |
| 2222 | ctx->frag_ctx.curoff = UINT_MAX; |
Kevin Zhu | f7f5428 | 2019-04-26 14:00:01 +0800 | [diff] [blame] | 2223 | ctx->frag_ctx.curlen = 0; |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2224 | |
| 2225 | encode_argument: |
| 2226 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2227 | goto encode_arg_value; |
| 2228 | |
Joseph Herlant | f7f6031 | 2018-11-15 13:46:49 -0800 | [diff] [blame] | 2229 | /* Encode the argument name as a string. It can by NULL */ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2230 | if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1) |
| 2231 | goto too_big; |
| 2232 | |
| 2233 | ctx->frag_ctx.curoff = 0; |
| 2234 | encode_arg_value: |
| 2235 | |
Joseph Herlant | f7f6031 | 2018-11-15 13:46:49 -0800 | [diff] [blame] | 2236 | /* Fetch the argument value */ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2237 | 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] | 2238 | if (smp) { |
| 2239 | smp->ctx.a[0] = &ctx->frag_ctx.curlen; |
| 2240 | smp->ctx.a[1] = &ctx->frag_ctx.curoff; |
| 2241 | } |
Christopher Faulet | 85db321 | 2019-04-26 14:30:15 +0200 | [diff] [blame] | 2242 | ret = spoe_encode_data(smp, buf, end); |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2243 | if (ret == -1 || ctx->frag_ctx.curoff) |
| 2244 | goto too_big; |
| 2245 | } |
| 2246 | |
| 2247 | next: |
| 2248 | return 0; |
| 2249 | |
| 2250 | too_big: |
| 2251 | return -1; |
| 2252 | } |
| 2253 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2254 | /* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to |
| 2255 | * handle fragmented content. On success it returns 1. If an error occurred, -1 |
| 2256 | * is returned. If nothing has been encoded, it returns 0 (this is only possible |
| 2257 | * for unfragmented payload). */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2258 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2259 | spoe_encode_messages(struct stream *s, struct spoe_context *ctx, |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2260 | struct list *messages, int dir, int type) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2261 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2262 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2263 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2264 | struct spoe_message *msg; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2265 | char *p, *end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2266 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2267 | p = b_head(&ctx->buffer); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2268 | end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE; |
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 | if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */ |
| 2271 | /* Resume encoding of a SPOE message */ |
| 2272 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2273 | msg = ctx->frag_ctx.curmsg; |
| 2274 | goto encode_evt_message; |
| 2275 | } |
| 2276 | |
| 2277 | list_for_each_entry(msg, messages, by_evt) { |
| 2278 | ctx->frag_ctx.curmsg = msg; |
| 2279 | ctx->frag_ctx.curarg = NULL; |
| 2280 | ctx->frag_ctx.curoff = UINT_MAX; |
| 2281 | |
| 2282 | encode_evt_message: |
| 2283 | if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1) |
| 2284 | goto too_big; |
| 2285 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2286 | } |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2287 | else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */ |
| 2288 | /* Resume encoding of a SPOE message */ |
| 2289 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2290 | msg = ctx->frag_ctx.curmsg; |
| 2291 | goto encode_grp_message; |
| 2292 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2293 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2294 | list_for_each_entry(msg, messages, by_grp) { |
| 2295 | ctx->frag_ctx.curmsg = msg; |
| 2296 | ctx->frag_ctx.curarg = NULL; |
| 2297 | ctx->frag_ctx.curoff = UINT_MAX; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2298 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2299 | encode_grp_message: |
| 2300 | if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1) |
| 2301 | goto too_big; |
| 2302 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2303 | } |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2304 | else |
| 2305 | goto skip; |
| 2306 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2307 | |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2308 | /* nothing has been encoded for an unfragmented payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2309 | if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer)) |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2310 | goto skip; |
| 2311 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2312 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2313 | " - encode %s messages - spoe_appctx=%p" |
| 2314 | "- max_size=%u - encoded=%ld\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2315 | (int)now.tv_sec, (int)now.tv_usec, |
| 2316 | agent->id, __FUNCTION__, s, |
| 2317 | ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"), |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2318 | ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE), |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2319 | p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2320 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2321 | b_set_data(&ctx->buffer, p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2322 | ctx->frag_ctx.curmsg = NULL; |
| 2323 | ctx->frag_ctx.curarg = NULL; |
| 2324 | ctx->frag_ctx.curoff = 0; |
| 2325 | ctx->frag_ctx.flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2326 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2327 | return 1; |
| 2328 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2329 | too_big: |
Christopher Faulet | a715ea8 | 2019-04-10 14:21:51 +0200 | [diff] [blame] | 2330 | /* Return an error if fragmentation is unsupported or if nothing has |
| 2331 | * been encoded because its too big and not splittable. */ |
| 2332 | if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) { |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 2333 | ctx->status_code = SPOE_CTX_ERR_TOO_BIG; |
| 2334 | return -1; |
| 2335 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2336 | |
| 2337 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2338 | " - encode fragmented messages - spoe_appctx=%p" |
| 2339 | " - curmsg=%p - curarg=%p - curoff=%u" |
| 2340 | " - max_size=%u - encoded=%ld\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2341 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2342 | agent->id, __FUNCTION__, s, ctx->spoe_appctx, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2343 | ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff, |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2344 | (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] | 2345 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2346 | b_set_data(&ctx->buffer, p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2347 | ctx->flags |= SPOE_CTX_FL_FRAGMENTED; |
| 2348 | ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN; |
| 2349 | return 1; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2350 | |
| 2351 | skip: |
| 2352 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2353 | " - skip the frame because nothing has been encoded\n", |
| 2354 | (int)now.tv_sec, (int)now.tv_usec, |
| 2355 | agent->id, __FUNCTION__, s); |
| 2356 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2357 | } |
| 2358 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2359 | |
| 2360 | /*************************************************************************** |
| 2361 | * Functions that handle SPOE actions |
| 2362 | **************************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2363 | /* Helper function to set a variable */ |
| 2364 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2365 | 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] | 2366 | struct sample *smp) |
| 2367 | { |
| 2368 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2369 | struct spoe_agent *agent = conf->agent; |
| 2370 | char varname[64]; |
| 2371 | |
| 2372 | memset(varname, 0, sizeof(varname)); |
| 2373 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2374 | scope, agent->var_pfx, len, name); |
Etienne Carriere | aec8989 | 2017-12-14 09:36:40 +0000 | [diff] [blame] | 2375 | if (agent->flags & SPOE_FL_FORCE_SET_VAR) |
| 2376 | vars_set_by_name(varname, len, smp); |
| 2377 | else |
| 2378 | vars_set_by_name_ifexist(varname, len, smp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2379 | } |
| 2380 | |
| 2381 | /* Helper function to unset a variable */ |
| 2382 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2383 | 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] | 2384 | struct sample *smp) |
| 2385 | { |
| 2386 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2387 | struct spoe_agent *agent = conf->agent; |
| 2388 | char varname[64]; |
| 2389 | |
| 2390 | memset(varname, 0, sizeof(varname)); |
| 2391 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2392 | scope, agent->var_pfx, len, name); |
| 2393 | vars_unset_by_name_ifexist(varname, len, smp); |
| 2394 | } |
| 2395 | |
| 2396 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2397 | static inline int |
| 2398 | spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx, |
| 2399 | char **buf, char *end, int dir) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2400 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2401 | char *str, *scope, *p = *buf; |
| 2402 | struct sample smp; |
| 2403 | uint64_t sz; |
| 2404 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2405 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2406 | if (p + 2 >= end) |
| 2407 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2408 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2409 | /* SET-VAR requires 3 arguments */ |
| 2410 | if (*p++ != 3) |
| 2411 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2412 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2413 | switch (*p++) { |
| 2414 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2415 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2416 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2417 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2418 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2419 | default: goto skip; |
| 2420 | } |
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 (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2423 | goto skip; |
| 2424 | memset(&smp, 0, sizeof(smp)); |
| 2425 | 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] | 2426 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2427 | if (spoe_decode_data(&p, end, &smp) == -1) |
| 2428 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2429 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2430 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2431 | " - set-var '%s.%s.%.*s'\n", |
| 2432 | (int)now.tv_sec, (int)now.tv_usec, |
| 2433 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2434 | __FUNCTION__, s, scope, |
| 2435 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2436 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2437 | |
Christopher Faulet | 2469eba | 2020-10-15 16:08:30 +0200 | [diff] [blame] | 2438 | if (smp.data.type == SMP_T_ANY) |
| 2439 | spoe_unset_var(ctx, scope, str, sz, &smp); |
| 2440 | else |
| 2441 | spoe_set_var(ctx, scope, str, sz, &smp); |
Christopher Faulet | b5cff60 | 2016-11-24 14:53:22 +0100 | [diff] [blame] | 2442 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2443 | ret = (p - *buf); |
| 2444 | *buf = p; |
| 2445 | return ret; |
| 2446 | skip: |
| 2447 | return 0; |
| 2448 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2449 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2450 | static inline int |
| 2451 | spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx, |
| 2452 | char **buf, char *end, int dir) |
| 2453 | { |
| 2454 | char *str, *scope, *p = *buf; |
| 2455 | struct sample smp; |
| 2456 | uint64_t sz; |
| 2457 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2458 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2459 | if (p + 2 >= end) |
| 2460 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2461 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2462 | /* UNSET-VAR requires 2 arguments */ |
| 2463 | if (*p++ != 2) |
| 2464 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2465 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2466 | switch (*p++) { |
| 2467 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2468 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2469 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2470 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2471 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2472 | default: goto skip; |
| 2473 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2474 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2475 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2476 | goto skip; |
| 2477 | memset(&smp, 0, sizeof(smp)); |
| 2478 | 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] | 2479 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2480 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2481 | " - unset-var '%s.%s.%.*s'\n", |
| 2482 | (int)now.tv_sec, (int)now.tv_usec, |
| 2483 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2484 | __FUNCTION__, s, scope, |
| 2485 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2486 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2487 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2488 | spoe_unset_var(ctx, scope, str, sz, &smp); |
| 2489 | |
| 2490 | ret = (p - *buf); |
| 2491 | *buf = p; |
| 2492 | return ret; |
| 2493 | skip: |
| 2494 | return 0; |
| 2495 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2496 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2497 | /* Process SPOE actions for a specific event. It returns 1 on success. If an |
| 2498 | * error occurred, 0 is returned. */ |
| 2499 | static int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2500 | spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2501 | { |
| 2502 | char *p, *end; |
| 2503 | int ret; |
| 2504 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2505 | p = b_head(&ctx->buffer); |
| 2506 | end = p + b_data(&ctx->buffer); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2507 | |
| 2508 | while (p < end) { |
| 2509 | enum spoe_action_type type; |
| 2510 | |
| 2511 | type = *p++; |
| 2512 | switch (type) { |
| 2513 | case SPOE_ACT_T_SET_VAR: |
| 2514 | ret = spoe_decode_action_set_var(s, ctx, &p, end, dir); |
| 2515 | if (!ret) |
| 2516 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2517 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2518 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2519 | case SPOE_ACT_T_UNSET_VAR: |
| 2520 | ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir); |
| 2521 | if (!ret) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2522 | goto skip; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2523 | break; |
| 2524 | |
| 2525 | default: |
| 2526 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2527 | } |
| 2528 | } |
| 2529 | |
| 2530 | return 1; |
| 2531 | skip: |
| 2532 | return 0; |
| 2533 | } |
| 2534 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2535 | /*************************************************************************** |
| 2536 | * Functions that process SPOE events |
| 2537 | **************************************************************************/ |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2538 | static void |
| 2539 | spoe_update_stats(struct stream *s, struct spoe_agent *agent, |
| 2540 | struct spoe_context *ctx, int dir) |
| 2541 | { |
| 2542 | if (!tv_iszero(&ctx->stats.tv_start)) { |
| 2543 | spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process); |
| 2544 | ctx->stats.t_total += ctx->stats.t_process; |
| 2545 | tv_zero(&ctx->stats.tv_request); |
| 2546 | tv_zero(&ctx->stats.tv_queue); |
| 2547 | tv_zero(&ctx->stats.tv_wait); |
| 2548 | tv_zero(&ctx->stats.tv_response); |
| 2549 | } |
| 2550 | |
| 2551 | if (agent->var_t_process) { |
| 2552 | struct sample smp; |
| 2553 | |
| 2554 | memset(&smp, 0, sizeof(smp)); |
| 2555 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2556 | smp.data.u.sint = ctx->stats.t_process; |
| 2557 | smp.data.type = SMP_T_SINT; |
| 2558 | |
| 2559 | spoe_set_var(ctx, "txn", agent->var_t_process, |
| 2560 | strlen(agent->var_t_process), &smp); |
| 2561 | } |
| 2562 | |
| 2563 | if (agent->var_t_total) { |
| 2564 | struct sample smp; |
| 2565 | |
| 2566 | memset(&smp, 0, sizeof(smp)); |
| 2567 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2568 | smp.data.u.sint = ctx->stats.t_total; |
| 2569 | smp.data.type = SMP_T_SINT; |
| 2570 | |
| 2571 | spoe_set_var(ctx, "txn", agent->var_t_total, |
| 2572 | strlen(agent->var_t_total), &smp); |
| 2573 | } |
| 2574 | } |
| 2575 | |
| 2576 | static void |
| 2577 | spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent, |
| 2578 | struct spoe_context *ctx, int dir) |
| 2579 | { |
| 2580 | if (agent->eps_max > 0) |
| 2581 | update_freq_ctr(&agent->rt[tid].err_per_sec, 1); |
| 2582 | |
| 2583 | if (agent->var_on_error) { |
| 2584 | struct sample smp; |
| 2585 | |
| 2586 | memset(&smp, 0, sizeof(smp)); |
| 2587 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2588 | smp.data.u.sint = ctx->status_code; |
| 2589 | smp.data.type = SMP_T_BOOL; |
| 2590 | |
| 2591 | spoe_set_var(ctx, "txn", agent->var_on_error, |
| 2592 | strlen(agent->var_on_error), &smp); |
| 2593 | } |
| 2594 | |
| 2595 | ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR) |
| 2596 | ? SPOE_CTX_ST_READY |
| 2597 | : SPOE_CTX_ST_NONE); |
| 2598 | } |
| 2599 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2600 | static inline int |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2601 | 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] | 2602 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2603 | /* If a process is already started for this SPOE context, retry |
| 2604 | * later. */ |
| 2605 | if (ctx->flags & SPOE_CTX_FL_PROCESS) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2606 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2607 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2608 | agent->rt[tid].processing++; |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2609 | ctx->stats.tv_start = now; |
| 2610 | ctx->stats.tv_request = now; |
| 2611 | ctx->stats.t_request = -1; |
| 2612 | ctx->stats.t_queue = -1; |
| 2613 | ctx->stats.t_waiting = -1; |
| 2614 | ctx->stats.t_response = -1; |
| 2615 | ctx->stats.t_process = -1; |
| 2616 | |
| 2617 | ctx->status_code = 0; |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2618 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2619 | /* Set the right flag to prevent request and response processing |
| 2620 | * in same time. */ |
| 2621 | ctx->flags |= ((dir == SMP_OPT_DIR_REQ) |
| 2622 | ? SPOE_CTX_FL_REQ_PROCESS |
| 2623 | : SPOE_CTX_FL_RSP_PROCESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2624 | return 1; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2625 | } |
| 2626 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2627 | static inline void |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2628 | spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2629 | { |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2630 | struct spoe_appctx *sa = ctx->spoe_appctx; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2631 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2632 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) |
| 2633 | return; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2634 | _HA_ATOMIC_INC(&agent->counters.nb_processed); |
Christopher Faulet | 879dca9 | 2018-03-23 11:53:24 +0100 | [diff] [blame] | 2635 | if (sa) { |
| 2636 | if (sa->frag_ctx.ctx == ctx) { |
| 2637 | sa->frag_ctx.ctx = NULL; |
| 2638 | spoe_wakeup_appctx(sa->owner); |
| 2639 | } |
| 2640 | else |
| 2641 | sa->cur_fpa--; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2642 | } |
| 2643 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2644 | /* Reset the flag to allow next processing */ |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2645 | agent->rt[tid].processing--; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2646 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2647 | |
| 2648 | /* Reset processing timer */ |
| 2649 | ctx->process_exp = TICK_ETERNITY; |
Christopher Faulet | a894261 | 2024-03-14 10:21:56 +0100 | [diff] [blame] | 2650 | ctx->strm->req.analyse_exp = TICK_ETERNITY; |
| 2651 | ctx->strm->res.analyse_exp = TICK_ETERNITY; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2652 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2653 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2654 | |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2655 | ctx->spoe_appctx = NULL; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2656 | ctx->frag_ctx.curmsg = NULL; |
| 2657 | ctx->frag_ctx.curarg = NULL; |
| 2658 | ctx->frag_ctx.curoff = 0; |
| 2659 | ctx->frag_ctx.flags = 0; |
| 2660 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2661 | if (!LIST_ISEMPTY(&ctx->list)) { |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2662 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2663 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2664 | else |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2665 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2666 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2667 | LIST_DELETE(&ctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2668 | LIST_INIT(&ctx->list); |
| 2669 | } |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2670 | } |
| 2671 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2672 | /* Process a list of SPOE messages. First, this functions will process messages |
| 2673 | * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame |
| 2674 | * to process corresponding actions. During all the processing, it returns 0 |
| 2675 | * and it returns 1 when the processing is finished. If an error occurred, -1 |
| 2676 | * is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2677 | static int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2678 | spoe_process_messages(struct stream *s, struct spoe_context *ctx, |
| 2679 | struct list *messages, int dir, int type) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2680 | { |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2681 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2682 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2683 | int ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2684 | |
| 2685 | if (ctx->state == SPOE_CTX_ST_ERROR) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2686 | goto end; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2687 | |
| 2688 | if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) { |
| 2689 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2690 | " - failed to process messages: timeout\n", |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2691 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2692 | agent->id, __FUNCTION__, s); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2693 | ctx->status_code = SPOE_CTX_ERR_TOUT; |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2694 | goto end; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2695 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2696 | |
| 2697 | if (ctx->state == SPOE_CTX_ST_READY) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2698 | if (agent->eps_max > 0) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2699 | 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] | 2700 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2701 | " - skip processing of messages: max EPS reached\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2702 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2703 | agent->id, __FUNCTION__, s); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2704 | goto skip; |
| 2705 | } |
| 2706 | } |
| 2707 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2708 | if (!tick_isset(ctx->process_exp)) { |
| 2709 | ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing); |
Christopher Faulet | a894261 | 2024-03-14 10:21:56 +0100 | [diff] [blame] | 2710 | if (dir == SMP_OPT_DIR_REQ) |
| 2711 | s->req.analyse_exp = ctx->process_exp; |
| 2712 | else |
| 2713 | s->res.analyse_exp = ctx->process_exp; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2714 | } |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2715 | ret = spoe_start_processing(agent, ctx, dir); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2716 | if (!ret) |
| 2717 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2718 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2719 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2720 | /* fall through */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2721 | } |
| 2722 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2723 | if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) { |
Christopher Faulet | 63263e5 | 2019-04-10 14:47:18 +0200 | [diff] [blame] | 2724 | if (tv_iszero(&ctx->stats.tv_request)) |
| 2725 | ctx->stats.tv_request = now; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2726 | if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait)) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2727 | goto out; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2728 | ret = spoe_encode_messages(s, ctx, messages, dir, type); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2729 | if (ret < 0) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2730 | goto end; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2731 | if (!ret) |
| 2732 | goto skip; |
Christopher Faulet | 333694d | 2018-01-12 10:45:47 +0100 | [diff] [blame] | 2733 | if (spoe_queue_context(ctx) < 0) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2734 | goto end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2735 | ctx->state = SPOE_CTX_ST_SENDING_MSGS; |
| 2736 | } |
| 2737 | |
| 2738 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) { |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2739 | if (ctx->spoe_appctx) |
| 2740 | spoe_wakeup_appctx(ctx->spoe_appctx->owner); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2741 | ret = 0; |
| 2742 | goto out; |
| 2743 | } |
| 2744 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2745 | if (ctx->state == SPOE_CTX_ST_WAITING_ACK) { |
| 2746 | ret = 0; |
| 2747 | goto out; |
| 2748 | } |
| 2749 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2750 | if (ctx->state == SPOE_CTX_ST_DONE) { |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2751 | spoe_process_actions(s, ctx, dir); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2752 | ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2753 | ctx->frame_id++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2754 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2755 | spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2756 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | out: |
| 2760 | return ret; |
| 2761 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2762 | skip: |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2763 | tv_zero(&ctx->stats.tv_start); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2764 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2765 | spoe_stop_processing(agent, ctx); |
| 2766 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2767 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2768 | end: |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2769 | spoe_update_stats(s, agent, ctx, dir); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2770 | spoe_stop_processing(agent, ctx); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2771 | if (ctx->status_code) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2772 | _HA_ATOMIC_INC(&agent->counters.nb_errors); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2773 | spoe_handle_processing_error(s, agent, ctx, dir); |
| 2774 | ret = 1; |
| 2775 | } |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2776 | return ret; |
| 2777 | } |
| 2778 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2779 | /* Process a SPOE group, ie the list of messages attached to the group <grp>. |
| 2780 | * See spoe_process_message for details. */ |
| 2781 | static int |
| 2782 | spoe_process_group(struct stream *s, struct spoe_context *ctx, |
| 2783 | struct spoe_group *group, int dir) |
| 2784 | { |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2785 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2786 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2787 | int ret; |
| 2788 | |
| 2789 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2790 | " - ctx-state=%s - Process messages for group=%s\n", |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2791 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2792 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2793 | group->id); |
| 2794 | |
| 2795 | if (LIST_ISEMPTY(&group->messages)) |
| 2796 | return 1; |
| 2797 | |
| 2798 | 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] | 2799 | if (ret && ctx->stats.t_process != -1) { |
| 2800 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2801 | " - <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] | 2802 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2803 | __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code, |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2804 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2805 | ctx->stats.t_response, ctx->stats.t_process, |
| 2806 | agent->counters.idles, agent->counters.applets, |
| 2807 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2808 | agent->counters.nb_errors, agent->counters.nb_processed, |
| 2809 | agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec)); |
| 2810 | if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM)) |
| 2811 | send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING), |
| 2812 | "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n", |
| 2813 | agent->id, group->id, s->uniq_id, ctx->status_code, |
| 2814 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
| 2815 | ctx->stats.t_response, ctx->stats.t_process, |
| 2816 | agent->counters.idles, agent->counters.applets, |
| 2817 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2818 | agent->counters.nb_errors, agent->counters.nb_processed); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2819 | } |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2820 | return ret; |
| 2821 | } |
| 2822 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2823 | /* Process a SPOE event, ie the list of messages attached to the event <ev>. |
| 2824 | * See spoe_process_message for details. */ |
| 2825 | static int |
| 2826 | spoe_process_event(struct stream *s, struct spoe_context *ctx, |
| 2827 | enum spoe_event ev) |
| 2828 | { |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2829 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2830 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2831 | int dir, ret; |
| 2832 | |
| 2833 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2834 | " - ctx-state=%s - Process messages for event=%s\n", |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2835 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2836 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2837 | spoe_event_str[ev]); |
| 2838 | |
| 2839 | dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 2840 | |
| 2841 | if (LIST_ISEMPTY(&(ctx->events[ev]))) |
| 2842 | return 1; |
| 2843 | |
| 2844 | 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] | 2845 | if (ret && ctx->stats.t_process != -1) { |
| 2846 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2847 | " - <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] | 2848 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2849 | __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code, |
| 2850 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2851 | ctx->stats.t_response, ctx->stats.t_process, |
| 2852 | agent->counters.idles, agent->counters.applets, |
| 2853 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2854 | agent->counters.nb_errors, agent->counters.nb_processed, |
| 2855 | agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec)); |
| 2856 | if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM)) |
| 2857 | send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING), |
| 2858 | "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n", |
| 2859 | agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code, |
| 2860 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
| 2861 | ctx->stats.t_response, ctx->stats.t_process, |
| 2862 | agent->counters.idles, agent->counters.applets, |
| 2863 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2864 | agent->counters.nb_errors, agent->counters.nb_processed); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2865 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2866 | return ret; |
| 2867 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2868 | |
| 2869 | /*************************************************************************** |
| 2870 | * Functions that create/destroy SPOE contexts |
| 2871 | **************************************************************************/ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2872 | static int |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2873 | spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2874 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2875 | if (buf->size) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2876 | return 1; |
| 2877 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2878 | if (LIST_INLIST(&buffer_wait->list)) |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2879 | LIST_DEL_INIT(&buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2880 | |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 2881 | if (b_alloc(buf)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2882 | return 1; |
| 2883 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2884 | LIST_APPEND(&ti->buffer_wq, &buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2885 | return 0; |
| 2886 | } |
| 2887 | |
| 2888 | static void |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2889 | spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2890 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2891 | if (LIST_INLIST(&buffer_wait->list)) |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2892 | LIST_DEL_INIT(&buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2893 | |
| 2894 | /* Release the buffer if needed */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2895 | if (buf->size) { |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2896 | b_free(buf); |
Willy Tarreau | 4d77bbf | 2021-02-20 12:02:46 +0100 | [diff] [blame] | 2897 | offer_buffers(buffer_wait->target, 1); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2898 | } |
| 2899 | } |
| 2900 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2901 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2902 | spoe_wakeup_context(struct spoe_context *ctx) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2903 | { |
| 2904 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 2905 | return 1; |
| 2906 | } |
| 2907 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2908 | static struct spoe_context * |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2909 | spoe_create_context(struct stream *s, struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2910 | { |
| 2911 | struct spoe_config *conf = FLT_CONF(filter); |
| 2912 | struct spoe_context *ctx; |
| 2913 | |
Willy Tarreau | c9ef9bc | 2021-03-22 21:04:50 +0100 | [diff] [blame] | 2914 | ctx = pool_zalloc(pool_head_spoe_ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2915 | if (ctx == NULL) { |
| 2916 | return NULL; |
| 2917 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2918 | ctx->filter = filter; |
| 2919 | ctx->state = SPOE_CTX_ST_NONE; |
| 2920 | ctx->status_code = SPOE_CTX_ERR_NONE; |
| 2921 | ctx->flags = 0; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2922 | ctx->events = conf->agent->events; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 2923 | ctx->groups = &conf->agent->groups; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2924 | ctx->buffer = BUF_NULL; |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2925 | LIST_INIT(&ctx->buffer_wait.list); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2926 | ctx->buffer_wait.target = ctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2927 | ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2928 | LIST_INIT(&ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2929 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2930 | ctx->stream_id = 0; |
| 2931 | ctx->frame_id = 1; |
| 2932 | ctx->process_exp = TICK_ETERNITY; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2933 | |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2934 | tv_zero(&ctx->stats.tv_start); |
| 2935 | tv_zero(&ctx->stats.tv_request); |
| 2936 | tv_zero(&ctx->stats.tv_queue); |
| 2937 | tv_zero(&ctx->stats.tv_wait); |
| 2938 | tv_zero(&ctx->stats.tv_response); |
| 2939 | ctx->stats.t_request = -1; |
| 2940 | ctx->stats.t_queue = -1; |
| 2941 | ctx->stats.t_waiting = -1; |
| 2942 | ctx->stats.t_response = -1; |
| 2943 | ctx->stats.t_process = -1; |
| 2944 | ctx->stats.t_total = 0; |
| 2945 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2946 | ctx->strm = s; |
| 2947 | ctx->state = SPOE_CTX_ST_READY; |
| 2948 | filter->ctx = ctx; |
| 2949 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2950 | return ctx; |
| 2951 | } |
| 2952 | |
| 2953 | static void |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2954 | spoe_destroy_context(struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2955 | { |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2956 | struct spoe_config *conf = FLT_CONF(filter); |
| 2957 | struct spoe_context *ctx = filter->ctx; |
| 2958 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2959 | if (!ctx) |
| 2960 | return; |
| 2961 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2962 | spoe_stop_processing(conf->agent, ctx); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2963 | pool_free(pool_head_spoe_ctx, ctx); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2964 | filter->ctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2968 | spoe_reset_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2969 | { |
| 2970 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 2971 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2972 | |
| 2973 | tv_zero(&ctx->stats.tv_start); |
| 2974 | tv_zero(&ctx->stats.tv_request); |
| 2975 | tv_zero(&ctx->stats.tv_queue); |
| 2976 | tv_zero(&ctx->stats.tv_wait); |
| 2977 | tv_zero(&ctx->stats.tv_response); |
| 2978 | ctx->stats.t_request = -1; |
| 2979 | ctx->stats.t_queue = -1; |
| 2980 | ctx->stats.t_waiting = -1; |
| 2981 | ctx->stats.t_response = -1; |
| 2982 | ctx->stats.t_process = -1; |
| 2983 | ctx->stats.t_total = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2984 | } |
| 2985 | |
| 2986 | |
| 2987 | /*************************************************************************** |
| 2988 | * Hooks that manage the filter lifecycle (init/check/deinit) |
| 2989 | **************************************************************************/ |
| 2990 | /* Signal handler: Do a soft stop, wakeup SPOE applet */ |
| 2991 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2992 | spoe_sig_stop(struct sig_handler *sh) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2993 | { |
| 2994 | struct proxy *p; |
| 2995 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 2996 | p = proxies_list; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2997 | while (p) { |
| 2998 | struct flt_conf *fconf; |
| 2999 | |
Christopher Faulet | 2702630 | 2023-05-11 09:08:28 +0200 | [diff] [blame] | 3000 | /* SPOE filter are not initialized for disabled proxoes. Move to |
| 3001 | * the next one |
| 3002 | */ |
| 3003 | if (p->disabled) { |
| 3004 | p = p->next; |
| 3005 | continue; |
| 3006 | } |
| 3007 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3008 | list_for_each_entry(fconf, &p->filter_configs, list) { |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 3009 | struct spoe_config *conf; |
| 3010 | struct spoe_agent *agent; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 3011 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3012 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3013 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 3014 | if (fconf->id != spoe_filter_id) |
| 3015 | continue; |
| 3016 | |
| 3017 | conf = fconf->conf; |
| 3018 | agent = conf->agent; |
| 3019 | |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3020 | for (i = 0; i < global.nbthread; ++i) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3021 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3022 | list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list) |
| 3023 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3024 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3025 | } |
| 3026 | } |
| 3027 | p = p->next; |
| 3028 | } |
| 3029 | } |
| 3030 | |
| 3031 | |
| 3032 | /* Initialize the SPOE filter. Returns -1 on error, else 0. */ |
| 3033 | static int |
| 3034 | spoe_init(struct proxy *px, struct flt_conf *fconf) |
| 3035 | { |
| 3036 | struct spoe_config *conf = fconf->conf; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3037 | |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3038 | /* conf->agent_fe was already initialized during the config |
| 3039 | * parsing. Finish initialization. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3040 | conf->agent_fe.last_change = now.tv_sec; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3041 | conf->agent_fe.cap = PR_CAP_FE; |
| 3042 | conf->agent_fe.mode = PR_MODE_TCP; |
| 3043 | conf->agent_fe.maxconn = 0; |
| 3044 | conf->agent_fe.options2 |= PR_O2_INDEPSTR; |
| 3045 | conf->agent_fe.conn_retries = CONN_RETRIES; |
| 3046 | conf->agent_fe.accept = frontend_accept; |
| 3047 | conf->agent_fe.srv = NULL; |
| 3048 | conf->agent_fe.timeout.client = TICK_ETERNITY; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3049 | conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES; |
| 3050 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3051 | if (!sighandler_registered) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3052 | signal_register_fct(0, spoe_sig_stop, 0); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3053 | sighandler_registered = 1; |
| 3054 | } |
| 3055 | |
Christopher Faulet | 0029235 | 2019-01-09 15:05:10 +0100 | [diff] [blame] | 3056 | fconf->flags |= FLT_CFG_FL_HTX; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3057 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3058 | } |
| 3059 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3060 | /* Free resources allocated by the SPOE filter. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3061 | static void |
| 3062 | spoe_deinit(struct proxy *px, struct flt_conf *fconf) |
| 3063 | { |
| 3064 | struct spoe_config *conf = fconf->conf; |
| 3065 | |
| 3066 | if (conf) { |
| 3067 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3068 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3069 | spoe_release_agent(agent); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3070 | free(conf->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3071 | free(conf); |
| 3072 | } |
| 3073 | fconf->conf = NULL; |
| 3074 | } |
| 3075 | |
| 3076 | /* Check configuration of a SPOE filter for a specified proxy. |
| 3077 | * Return 1 on error, else 0. */ |
| 3078 | static int |
| 3079 | spoe_check(struct proxy *px, struct flt_conf *fconf) |
| 3080 | { |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3081 | struct flt_conf *f; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3082 | struct spoe_config *conf = fconf->conf; |
| 3083 | struct proxy *target; |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3084 | struct logsrv *logsrv; |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3085 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3086 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3087 | /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names |
| 3088 | * are uniq */ |
| 3089 | list_for_each_entry(f, &px->filter_configs, list) { |
| 3090 | struct spoe_config *c = f->conf; |
| 3091 | |
| 3092 | /* This is not an SPOE filter */ |
| 3093 | if (f->id != spoe_filter_id) |
| 3094 | continue; |
| 3095 | /* This is the current SPOE filter */ |
| 3096 | if (f == fconf) |
| 3097 | continue; |
| 3098 | |
| 3099 | /* Check engine Id. It should be uniq */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3100 | if (strcmp(conf->id, c->id) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3101 | ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n", |
| 3102 | px->id, conf->id); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3103 | return 1; |
| 3104 | } |
| 3105 | } |
| 3106 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3107 | target = proxy_be_by_name(conf->agent->b.name); |
| 3108 | if (target == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3109 | ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'" |
| 3110 | " declared at %s:%d.\n", |
| 3111 | px->id, conf->agent->b.name, conf->agent->id, |
| 3112 | conf->agent->conf.file, conf->agent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3113 | return 1; |
| 3114 | } |
| 3115 | if (target->mode != PR_MODE_TCP) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3116 | ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared" |
| 3117 | " at %s:%d does not support HTTP mode.\n", |
| 3118 | px->id, target->id, conf->agent->id, |
| 3119 | conf->agent->conf.file, conf->agent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3120 | return 1; |
| 3121 | } |
| 3122 | |
Willy Tarreau | 2bdcfde | 2019-02-05 13:37:19 +0100 | [diff] [blame] | 3123 | if (px->bind_proc & ~target->bind_proc) { |
| 3124 | ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared" |
| 3125 | " at %s:%d does not cover all of its processes.\n", |
| 3126 | px->id, target->id, conf->agent->id, |
| 3127 | conf->agent->conf.file, conf->agent->conf.line); |
| 3128 | return 1; |
| 3129 | } |
| 3130 | |
Christopher Faulet | fe26155 | 2019-03-18 13:57:42 +0100 | [diff] [blame] | 3131 | if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) { |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3132 | ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n", |
| 3133 | px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line); |
| 3134 | return 1; |
| 3135 | } |
| 3136 | for (i = 0; i < global.nbthread; ++i) { |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3137 | conf->agent->rt[i].engine_id = NULL; |
Christopher Faulet | fe26155 | 2019-03-18 13:57:42 +0100 | [diff] [blame] | 3138 | conf->agent->rt[i].frame_size = conf->agent->max_frame_size; |
| 3139 | conf->agent->rt[i].processing = 0; |
Christopher Faulet | d1e1aa0 | 2023-04-26 15:56:59 +0200 | [diff] [blame] | 3140 | conf->agent->rt[i].idles = 0; |
Christopher Faulet | fe26155 | 2019-03-18 13:57:42 +0100 | [diff] [blame] | 3141 | LIST_INIT(&conf->agent->rt[i].applets); |
| 3142 | LIST_INIT(&conf->agent->rt[i].sending_queue); |
| 3143 | LIST_INIT(&conf->agent->rt[i].waiting_queue); |
| 3144 | HA_SPIN_INIT(&conf->agent->rt[i].lock); |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3145 | } |
| 3146 | |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3147 | list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) { |
| 3148 | if (logsrv->type == LOG_TARGET_BUFFER) { |
| 3149 | struct sink *sink = sink_find(logsrv->ring_name); |
| 3150 | |
| 3151 | if (!sink || sink->type != SINK_TYPE_BUFFER) { |
| 3152 | ha_alert("Proxy %s : log server used by SPOE agent '%s' declared" |
Ilya Shipitsin | d7a988c | 2021-03-04 23:26:15 +0500 | [diff] [blame] | 3153 | " at %s:%d uses unknown ring named '%s'.\n", |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3154 | px->id, conf->agent->id, conf->agent->conf.file, |
| 3155 | conf->agent->conf.line, logsrv->ring_name); |
| 3156 | return 1; |
| 3157 | } |
| 3158 | logsrv->sink = sink; |
| 3159 | } |
| 3160 | } |
| 3161 | |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3162 | ha_free(&conf->agent->b.name); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3163 | conf->agent->b.be = target; |
| 3164 | return 0; |
| 3165 | } |
| 3166 | |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3167 | /* Initializes the SPOE filter for a proxy for a specific thread. |
| 3168 | * Returns a negative value if an error occurs. */ |
| 3169 | static int |
| 3170 | spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf) |
| 3171 | { |
| 3172 | struct spoe_config *conf = fconf->conf; |
| 3173 | struct spoe_agent *agent = conf->agent; |
| 3174 | |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3175 | agent->rt[tid].engine_id = generate_pseudo_uuid(); |
| 3176 | if (agent->rt[tid].engine_id == NULL) |
| 3177 | return -1; |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3178 | return 0; |
| 3179 | } |
| 3180 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3181 | /************************************************************************** |
| 3182 | * Hooks attached to a stream |
| 3183 | *************************************************************************/ |
| 3184 | /* Called when a filter instance is created and attach to a stream. It creates |
| 3185 | * the context that will be used to process this stream. */ |
| 3186 | static int |
| 3187 | spoe_start(struct stream *s, struct filter *filter) |
| 3188 | { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3189 | struct spoe_config *conf = FLT_CONF(filter); |
| 3190 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3191 | struct spoe_context *ctx; |
| 3192 | |
| 3193 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3194 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3195 | __FUNCTION__, s); |
| 3196 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 3197 | if ((ctx = spoe_create_context(s, filter)) == NULL) { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3198 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 3199 | " - failed to create SPOE context\n", |
| 3200 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | ccbc3fd | 2017-09-15 11:51:18 +0200 | [diff] [blame] | 3201 | __FUNCTION__, s); |
Christopher Faulet | 3b8e349 | 2018-03-26 17:20:58 +0200 | [diff] [blame] | 3202 | send_log(&conf->agent_fe, LOG_EMERG, |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3203 | "SPOE: [%s] failed to create SPOE context\n", |
| 3204 | agent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3205 | return 0; |
| 3206 | } |
| 3207 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3208 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3209 | filter->pre_analyzers |= AN_REQ_INSPECT_FE; |
| 3210 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3211 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3212 | filter->pre_analyzers |= AN_REQ_INSPECT_BE; |
| 3213 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3214 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3215 | filter->pre_analyzers |= AN_RES_INSPECT; |
| 3216 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3217 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3218 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE; |
| 3219 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3220 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3221 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE; |
| 3222 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3223 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3224 | filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE; |
| 3225 | |
| 3226 | return 1; |
| 3227 | } |
| 3228 | |
| 3229 | /* Called when a filter instance is detached from a stream. It release the |
| 3230 | * attached SPOE context. */ |
| 3231 | static void |
| 3232 | spoe_stop(struct stream *s, struct filter *filter) |
| 3233 | { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3234 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
| 3235 | (int)now.tv_sec, (int)now.tv_usec, |
| 3236 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3237 | __FUNCTION__, s); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 3238 | spoe_destroy_context(filter); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3239 | } |
| 3240 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3241 | |
| 3242 | /* |
| 3243 | * Called when the stream is woken up because of expired timer. |
| 3244 | */ |
| 3245 | static void |
| 3246 | spoe_check_timeouts(struct stream *s, struct filter *filter) |
| 3247 | { |
| 3248 | struct spoe_context *ctx = filter->ctx; |
| 3249 | |
Christopher Faulet | ac58060 | 2018-03-20 16:09:20 +0100 | [diff] [blame] | 3250 | if (tick_is_expired(ctx->process_exp, now_ms)) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3251 | s->pending_events |= TASK_WOKEN_MSG; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3252 | } |
| 3253 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3254 | /* Called when we are ready to filter data on a channel */ |
| 3255 | static int |
| 3256 | spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 3257 | { |
| 3258 | struct spoe_context *ctx = filter->ctx; |
| 3259 | int ret = 1; |
| 3260 | |
| 3261 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3262 | " - ctx-flags=0x%08x\n", |
| 3263 | (int)now.tv_sec, (int)now.tv_usec, |
| 3264 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3265 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 3266 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3267 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 3268 | goto out; |
| 3269 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3270 | if (!(chn->flags & CF_ISRESP)) { |
| 3271 | if (filter->pre_analyzers & AN_REQ_INSPECT_FE) |
| 3272 | chn->analysers |= AN_REQ_INSPECT_FE; |
| 3273 | if (filter->pre_analyzers & AN_REQ_INSPECT_BE) |
| 3274 | chn->analysers |= AN_REQ_INSPECT_BE; |
| 3275 | |
| 3276 | if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED) |
| 3277 | goto out; |
| 3278 | |
| 3279 | ctx->stream_id = s->uniq_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3280 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3281 | if (!ret) |
| 3282 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3283 | ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED; |
| 3284 | } |
| 3285 | else { |
Miroslav Zagorac | 8840326 | 2020-06-19 22:17:17 +0200 | [diff] [blame] | 3286 | if (filter->pre_analyzers & AN_RES_INSPECT) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3287 | chn->analysers |= AN_RES_INSPECT; |
| 3288 | |
| 3289 | if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED) |
| 3290 | goto out; |
| 3291 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3292 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3293 | if (!ret) { |
| 3294 | channel_dont_read(chn); |
| 3295 | channel_dont_close(chn); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3296 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3297 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3298 | ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3299 | } |
| 3300 | |
| 3301 | out: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3302 | return ret; |
| 3303 | } |
| 3304 | |
| 3305 | /* Called before a processing happens on a given channel */ |
| 3306 | static int |
| 3307 | spoe_chn_pre_analyze(struct stream *s, struct filter *filter, |
| 3308 | struct channel *chn, unsigned an_bit) |
| 3309 | { |
| 3310 | struct spoe_context *ctx = filter->ctx; |
| 3311 | int ret = 1; |
| 3312 | |
| 3313 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3314 | " - ctx-flags=0x%08x - ana=0x%08x\n", |
| 3315 | (int)now.tv_sec, (int)now.tv_usec, |
| 3316 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3317 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 3318 | ctx->flags, an_bit); |
| 3319 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3320 | if (ctx->state == SPOE_CTX_ST_NONE) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3321 | goto out; |
| 3322 | |
| 3323 | switch (an_bit) { |
| 3324 | case AN_REQ_INSPECT_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3325 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3326 | break; |
| 3327 | case AN_REQ_INSPECT_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3328 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3329 | break; |
| 3330 | case AN_RES_INSPECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3331 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3332 | break; |
| 3333 | case AN_REQ_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3334 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3335 | break; |
| 3336 | case AN_REQ_HTTP_PROCESS_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3337 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3338 | break; |
| 3339 | case AN_RES_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3340 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3341 | break; |
| 3342 | } |
| 3343 | |
| 3344 | out: |
Christopher Faulet | 9cdca97 | 2018-02-01 08:45:45 +0100 | [diff] [blame] | 3345 | if (!ret && (chn->flags & CF_ISRESP)) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3346 | channel_dont_read(chn); |
| 3347 | channel_dont_close(chn); |
| 3348 | } |
| 3349 | return ret; |
| 3350 | } |
| 3351 | |
| 3352 | /* Called when the filtering on the channel ends. */ |
| 3353 | static int |
| 3354 | spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 3355 | { |
| 3356 | struct spoe_context *ctx = filter->ctx; |
| 3357 | |
| 3358 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3359 | " - ctx-flags=0x%08x\n", |
| 3360 | (int)now.tv_sec, (int)now.tv_usec, |
| 3361 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3362 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 3363 | |
| 3364 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3365 | spoe_reset_context(ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3366 | } |
| 3367 | |
| 3368 | return 1; |
| 3369 | } |
| 3370 | |
| 3371 | /******************************************************************** |
| 3372 | * Functions that manage the filter initialization |
| 3373 | ********************************************************************/ |
| 3374 | struct flt_ops spoe_ops = { |
| 3375 | /* Manage SPOE filter, called for each filter declaration */ |
| 3376 | .init = spoe_init, |
| 3377 | .deinit = spoe_deinit, |
| 3378 | .check = spoe_check, |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3379 | .init_per_thread = spoe_init_per_thread, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3380 | |
| 3381 | /* Handle start/stop of SPOE */ |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3382 | .attach = spoe_start, |
| 3383 | .detach = spoe_stop, |
| 3384 | .check_timeouts = spoe_check_timeouts, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3385 | |
| 3386 | /* Handle channels activity */ |
| 3387 | .channel_start_analyze = spoe_start_analyze, |
| 3388 | .channel_pre_analyze = spoe_chn_pre_analyze, |
| 3389 | .channel_end_analyze = spoe_end_analyze, |
| 3390 | }; |
| 3391 | |
| 3392 | |
| 3393 | static int |
| 3394 | cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm) |
| 3395 | { |
| 3396 | const char *err; |
| 3397 | int i, err_code = 0; |
| 3398 | |
| 3399 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3400 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3401 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3402 | goto out; |
| 3403 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3404 | if (strcmp(args[0], "spoe-agent") == 0) { /* new spoe-agent section */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3405 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3406 | ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n", |
| 3407 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3408 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3409 | goto out; |
| 3410 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3411 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3412 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3413 | goto out; |
| 3414 | } |
| 3415 | |
| 3416 | err = invalid_char(args[1]); |
| 3417 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3418 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3419 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3420 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3421 | goto out; |
| 3422 | } |
| 3423 | |
| 3424 | if (curagent != NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3425 | ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n", |
| 3426 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3427 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3428 | goto out; |
| 3429 | } |
| 3430 | if ((curagent = calloc(1, sizeof(*curagent))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3431 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3432 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3433 | goto out; |
| 3434 | } |
| 3435 | |
| 3436 | curagent->id = strdup(args[1]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3437 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3438 | curagent->conf.file = strdup(file); |
| 3439 | curagent->conf.line = linenum; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3440 | |
| 3441 | curagent->timeout.hello = TICK_ETERNITY; |
| 3442 | curagent->timeout.idle = TICK_ETERNITY; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3443 | curagent->timeout.processing = TICK_ETERNITY; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3444 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3445 | curagent->var_pfx = NULL; |
| 3446 | curagent->var_on_error = NULL; |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3447 | curagent->var_t_process = NULL; |
| 3448 | curagent->var_t_total = NULL; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3449 | curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3450 | curagent->cps_max = 0; |
| 3451 | curagent->eps_max = 0; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 3452 | curagent->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 3453 | curagent->max_fpa = 20; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3454 | |
| 3455 | for (i = 0; i < SPOE_EV_EVENTS; ++i) |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3456 | LIST_INIT(&curagent->events[i]); |
| 3457 | LIST_INIT(&curagent->groups); |
| 3458 | LIST_INIT(&curagent->messages); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3459 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3460 | else if (strcmp(args[0], "use-backend") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3461 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3462 | ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n", |
| 3463 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3464 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3465 | goto out; |
| 3466 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3467 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3468 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3469 | free(curagent->b.name); |
| 3470 | curagent->b.name = strdup(args[1]); |
| 3471 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3472 | else if (strcmp(args[0], "messages") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3473 | int cur_arg = 1; |
| 3474 | while (*args[cur_arg]) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3475 | struct spoe_placeholder *ph = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3476 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3477 | list_for_each_entry(ph, &curmphs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3478 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3479 | ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3480 | file, linenum, args[cur_arg]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3481 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3482 | goto out; |
| 3483 | } |
| 3484 | } |
| 3485 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3486 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3487 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3488 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3489 | goto out; |
| 3490 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3491 | ph->id = strdup(args[cur_arg]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3492 | LIST_APPEND(&curmphs, &ph->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3493 | cur_arg++; |
| 3494 | } |
| 3495 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3496 | else if (strcmp(args[0], "groups") == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3497 | int cur_arg = 1; |
| 3498 | while (*args[cur_arg]) { |
| 3499 | struct spoe_placeholder *ph = NULL; |
| 3500 | |
| 3501 | list_for_each_entry(ph, &curgphs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3502 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3503 | ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n", |
| 3504 | file, linenum, args[cur_arg]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3505 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3506 | goto out; |
| 3507 | } |
| 3508 | } |
| 3509 | |
| 3510 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3511 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3512 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3513 | goto out; |
| 3514 | } |
| 3515 | ph->id = strdup(args[cur_arg]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3516 | LIST_APPEND(&curgphs, &ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3517 | cur_arg++; |
| 3518 | } |
| 3519 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3520 | else if (strcmp(args[0], "timeout") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3521 | unsigned int *tv = NULL; |
| 3522 | const char *res; |
| 3523 | unsigned timeout; |
| 3524 | |
| 3525 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3526 | ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n", |
| 3527 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3528 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3529 | goto out; |
| 3530 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3531 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3532 | goto out; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3533 | if (strcmp(args[1], "hello") == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3534 | tv = &curagent->timeout.hello; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3535 | else if (strcmp(args[1], "idle") == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3536 | tv = &curagent->timeout.idle; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3537 | else if (strcmp(args[1], "processing") == 0) |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3538 | tv = &curagent->timeout.processing; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3539 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3540 | ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n", |
| 3541 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3542 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3543 | goto out; |
| 3544 | } |
| 3545 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3546 | ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n", |
| 3547 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3548 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3549 | goto out; |
| 3550 | } |
| 3551 | res = parse_time_err(args[2], &timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 3552 | if (res == PARSE_TIME_OVER) { |
| 3553 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n", |
| 3554 | file, linenum, args[2], args[0], args[1]); |
| 3555 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3556 | goto out; |
| 3557 | } |
| 3558 | else if (res == PARSE_TIME_UNDER) { |
| 3559 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n", |
| 3560 | file, linenum, args[2], args[0], args[1]); |
| 3561 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3562 | goto out; |
| 3563 | } |
| 3564 | else if (res) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3565 | ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n", |
| 3566 | file, linenum, *res, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3567 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3568 | goto out; |
| 3569 | } |
| 3570 | *tv = MS_TO_TICKS(timeout); |
| 3571 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3572 | else if (strcmp(args[0], "option") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3573 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3574 | ha_alert("parsing [%s:%d]: '%s' expects an option name.\n", |
| 3575 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3576 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3577 | goto out; |
| 3578 | } |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3579 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3580 | if (strcmp(args[1], "pipelining") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3581 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3582 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3583 | if (kwm == 1) |
| 3584 | curagent->flags &= ~SPOE_FL_PIPELINING; |
| 3585 | else |
| 3586 | curagent->flags |= SPOE_FL_PIPELINING; |
| 3587 | goto out; |
| 3588 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3589 | else if (strcmp(args[1], "async") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3590 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3591 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3592 | if (kwm == 1) |
| 3593 | curagent->flags &= ~SPOE_FL_ASYNC; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3594 | else |
| 3595 | curagent->flags |= SPOE_FL_ASYNC; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3596 | goto out; |
| 3597 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3598 | else if (strcmp(args[1], "send-frag-payload") == 0) { |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 3599 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3600 | goto out; |
| 3601 | if (kwm == 1) |
| 3602 | curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION; |
| 3603 | else |
| 3604 | curagent->flags |= SPOE_FL_SND_FRAGMENTATION; |
| 3605 | goto out; |
| 3606 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3607 | else if (strcmp(args[1], "dontlog-normal") == 0) { |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 3608 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3609 | goto out; |
| 3610 | if (kwm == 1) |
| 3611 | curpxopts2 &= ~PR_O2_NOLOGNORM; |
| 3612 | else |
| 3613 | curpxopts2 |= PR_O2_NOLOGNORM; |
Christopher Faulet | 799f518 | 2018-04-26 11:36:34 +0200 | [diff] [blame] | 3614 | goto out; |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 3615 | } |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3616 | |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3617 | /* Following options does not support negation */ |
| 3618 | if (kwm == 1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3619 | ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n", |
| 3620 | file, linenum, args[1]); |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3621 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3622 | goto out; |
| 3623 | } |
| 3624 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3625 | if (strcmp(args[1], "var-prefix") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3626 | char *tmp; |
| 3627 | |
| 3628 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3629 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3630 | file, linenum, args[0], |
| 3631 | args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3632 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3633 | goto out; |
| 3634 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3635 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3636 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3637 | tmp = args[2]; |
| 3638 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3639 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3640 | 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] | 3641 | file, linenum, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3642 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3643 | goto out; |
| 3644 | } |
| 3645 | tmp++; |
| 3646 | } |
| 3647 | curagent->var_pfx = strdup(args[2]); |
| 3648 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3649 | else if (strcmp(args[1], "force-set-var") == 0) { |
Etienne Carriere | aec8989 | 2017-12-14 09:36:40 +0000 | [diff] [blame] | 3650 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3651 | goto out; |
| 3652 | curagent->flags |= SPOE_FL_FORCE_SET_VAR; |
| 3653 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3654 | else if (strcmp(args[1], "continue-on-error") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3655 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3656 | goto out; |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3657 | curagent->flags |= SPOE_FL_CONT_ON_ERR; |
| 3658 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3659 | else if (strcmp(args[1], "set-on-error") == 0) { |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3660 | char *tmp; |
| 3661 | |
| 3662 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3663 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3664 | file, linenum, args[0], |
| 3665 | args[1]); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3666 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3667 | goto out; |
| 3668 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3669 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3670 | goto out; |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3671 | tmp = args[2]; |
| 3672 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3673 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3674 | 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] | 3675 | file, linenum, args[0], args[1]); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3676 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3677 | goto out; |
| 3678 | } |
| 3679 | tmp++; |
| 3680 | } |
| 3681 | curagent->var_on_error = strdup(args[2]); |
| 3682 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3683 | else if (strcmp(args[1], "set-process-time") == 0) { |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3684 | char *tmp; |
| 3685 | |
| 3686 | if (!*args[2]) { |
| 3687 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3688 | file, linenum, args[0], |
| 3689 | args[1]); |
| 3690 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3691 | goto out; |
| 3692 | } |
| 3693 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3694 | goto out; |
| 3695 | tmp = args[2]; |
| 3696 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3697 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3698 | 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] | 3699 | file, linenum, args[0], args[1]); |
| 3700 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3701 | goto out; |
| 3702 | } |
| 3703 | tmp++; |
| 3704 | } |
| 3705 | curagent->var_t_process = strdup(args[2]); |
| 3706 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3707 | else if (strcmp(args[1], "set-total-time") == 0) { |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3708 | char *tmp; |
| 3709 | |
| 3710 | if (!*args[2]) { |
| 3711 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3712 | file, linenum, args[0], |
| 3713 | args[1]); |
| 3714 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3715 | goto out; |
| 3716 | } |
| 3717 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3718 | goto out; |
| 3719 | tmp = args[2]; |
| 3720 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3721 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3722 | 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] | 3723 | file, linenum, args[0], args[1]); |
| 3724 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3725 | goto out; |
| 3726 | } |
| 3727 | tmp++; |
| 3728 | } |
| 3729 | curagent->var_t_total = strdup(args[2]); |
| 3730 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3731 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3732 | ha_alert("parsing [%s:%d]: option '%s' is not supported.\n", |
| 3733 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3734 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3735 | goto out; |
| 3736 | } |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3737 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3738 | else if (strcmp(args[0], "maxconnrate") == 0) { |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3739 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3740 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3741 | file, linenum, args[0]); |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3742 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3743 | goto out; |
| 3744 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3745 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3746 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3747 | curagent->cps_max = atol(args[1]); |
| 3748 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3749 | else if (strcmp(args[0], "maxerrrate") == 0) { |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3750 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3751 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3752 | file, linenum, args[0]); |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3753 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3754 | goto out; |
| 3755 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3756 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3757 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3758 | curagent->eps_max = atol(args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3759 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3760 | else if (strcmp(args[0], "max-frame-size") == 0) { |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3761 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3762 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3763 | file, linenum, args[0]); |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3764 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3765 | goto out; |
| 3766 | } |
| 3767 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3768 | goto out; |
| 3769 | curagent->max_frame_size = atol(args[1]); |
| 3770 | if (curagent->max_frame_size < MIN_FRAME_SIZE || |
| 3771 | curagent->max_frame_size > MAX_FRAME_SIZE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3772 | ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n", |
| 3773 | file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE); |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3774 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3775 | goto out; |
| 3776 | } |
| 3777 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3778 | else if (strcmp(args[0], "max-waiting-frames") == 0) { |
Christopher Faulet | e8ade38 | 2018-01-25 15:32:22 +0100 | [diff] [blame] | 3779 | if (!*args[1]) { |
| 3780 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3781 | file, linenum, args[0]); |
| 3782 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3783 | goto out; |
| 3784 | } |
| 3785 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3786 | goto out; |
| 3787 | curagent->max_fpa = atol(args[1]); |
| 3788 | if (curagent->max_fpa < 1) { |
| 3789 | ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n", |
| 3790 | file, linenum, args[0]); |
| 3791 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3792 | goto out; |
| 3793 | } |
| 3794 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3795 | else if (strcmp(args[0], "register-var-names") == 0) { |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3796 | int cur_arg; |
| 3797 | |
| 3798 | if (!*args[1]) { |
| 3799 | ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n", |
| 3800 | file, linenum, args[0]); |
| 3801 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3802 | goto out; |
| 3803 | } |
| 3804 | cur_arg = 1; |
| 3805 | while (*args[cur_arg]) { |
| 3806 | struct spoe_var_placeholder *vph; |
| 3807 | |
| 3808 | if ((vph = calloc(1, sizeof(*vph))) == NULL) { |
| 3809 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3810 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3811 | goto out; |
| 3812 | } |
| 3813 | if ((vph->name = strdup(args[cur_arg])) == NULL) { |
Tim Duesterhus | b298613 | 2019-06-23 22:10:13 +0200 | [diff] [blame] | 3814 | free(vph); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3815 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3816 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3817 | goto out; |
| 3818 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3819 | LIST_APPEND(&curvars, &vph->list); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3820 | cur_arg++; |
| 3821 | } |
| 3822 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3823 | else if (strcmp(args[0], "log") == 0) { |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3824 | char *errmsg = NULL; |
| 3825 | |
Emeric Brun | 9533a70 | 2021-04-02 10:13:43 +0200 | [diff] [blame] | 3826 | if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), file, linenum, &errmsg)) { |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3827 | ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); |
| 3828 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3829 | goto out; |
| 3830 | } |
| 3831 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3832 | else if (*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3833 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n", |
| 3834 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3835 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3836 | goto out; |
| 3837 | } |
| 3838 | out: |
| 3839 | return err_code; |
| 3840 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3841 | static int |
| 3842 | cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm) |
| 3843 | { |
| 3844 | struct spoe_group *grp; |
| 3845 | const char *err; |
| 3846 | int err_code = 0; |
| 3847 | |
| 3848 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3849 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3850 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3851 | goto out; |
| 3852 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3853 | if (strcmp(args[0], "spoe-group") == 0) { /* new spoe-group section */ |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3854 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3855 | ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n", |
| 3856 | file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3857 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3858 | goto out; |
| 3859 | } |
| 3860 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3861 | err_code |= ERR_ABORT; |
| 3862 | goto out; |
| 3863 | } |
| 3864 | |
| 3865 | err = invalid_char(args[1]); |
| 3866 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3867 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3868 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3869 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3870 | goto out; |
| 3871 | } |
| 3872 | |
| 3873 | list_for_each_entry(grp, &curgrps, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3874 | if (strcmp(grp->id, args[1]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3875 | ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same" |
| 3876 | " name as another one declared at %s:%d.\n", |
| 3877 | file, linenum, args[1], grp->conf.file, grp->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3878 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3879 | goto out; |
| 3880 | } |
| 3881 | } |
| 3882 | |
| 3883 | if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3884 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3885 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3886 | goto out; |
| 3887 | } |
| 3888 | |
| 3889 | curgrp->id = strdup(args[1]); |
| 3890 | curgrp->conf.file = strdup(file); |
| 3891 | curgrp->conf.line = linenum; |
| 3892 | LIST_INIT(&curgrp->phs); |
| 3893 | LIST_INIT(&curgrp->messages); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3894 | LIST_APPEND(&curgrps, &curgrp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3895 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3896 | else if (strcmp(args[0], "messages") == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3897 | int cur_arg = 1; |
| 3898 | while (*args[cur_arg]) { |
| 3899 | struct spoe_placeholder *ph = NULL; |
| 3900 | |
| 3901 | list_for_each_entry(ph, &curgrp->phs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3902 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3903 | ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3904 | file, linenum, args[cur_arg]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3905 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3906 | goto out; |
| 3907 | } |
| 3908 | } |
| 3909 | |
| 3910 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3911 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3912 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3913 | goto out; |
| 3914 | } |
| 3915 | ph->id = strdup(args[cur_arg]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3916 | LIST_APPEND(&curgrp->phs, &ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3917 | cur_arg++; |
| 3918 | } |
| 3919 | } |
| 3920 | else if (*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3921 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n", |
| 3922 | file, linenum, args[0]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3923 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3924 | goto out; |
| 3925 | } |
| 3926 | out: |
| 3927 | return err_code; |
| 3928 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3929 | |
| 3930 | static int |
| 3931 | cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm) |
| 3932 | { |
| 3933 | struct spoe_message *msg; |
| 3934 | struct spoe_arg *arg; |
| 3935 | const char *err; |
| 3936 | char *errmsg = NULL; |
| 3937 | int err_code = 0; |
| 3938 | |
| 3939 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3940 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3941 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3942 | goto out; |
| 3943 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3944 | if (strcmp(args[0], "spoe-message") == 0) { /* new spoe-message section */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3945 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3946 | ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n", |
| 3947 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3948 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3949 | goto out; |
| 3950 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3951 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3952 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3953 | goto out; |
| 3954 | } |
| 3955 | |
| 3956 | err = invalid_char(args[1]); |
| 3957 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3958 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3959 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3960 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3961 | goto out; |
| 3962 | } |
| 3963 | |
| 3964 | list_for_each_entry(msg, &curmsgs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3965 | if (strcmp(msg->id, args[1]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3966 | ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same" |
| 3967 | " name as another one declared at %s:%d.\n", |
| 3968 | file, linenum, args[1], msg->conf.file, msg->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3969 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3970 | goto out; |
| 3971 | } |
| 3972 | } |
| 3973 | |
| 3974 | if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3975 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3976 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3977 | goto out; |
| 3978 | } |
| 3979 | |
| 3980 | curmsg->id = strdup(args[1]); |
| 3981 | curmsg->id_len = strlen(curmsg->id); |
| 3982 | curmsg->event = SPOE_EV_NONE; |
| 3983 | curmsg->conf.file = strdup(file); |
| 3984 | curmsg->conf.line = linenum; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 3985 | curmsg->nargs = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3986 | LIST_INIT(&curmsg->args); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3987 | LIST_INIT(&curmsg->acls); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3988 | LIST_INIT(&curmsg->by_evt); |
| 3989 | LIST_INIT(&curmsg->by_grp); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3990 | LIST_APPEND(&curmsgs, &curmsg->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3991 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3992 | else if (strcmp(args[0], "args") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3993 | int cur_arg = 1; |
| 3994 | |
| 3995 | curproxy->conf.args.ctx = ARGC_SPOE; |
| 3996 | curproxy->conf.args.file = file; |
| 3997 | curproxy->conf.args.line = linenum; |
| 3998 | while (*args[cur_arg]) { |
| 3999 | char *delim = strchr(args[cur_arg], '='); |
| 4000 | int idx = 0; |
| 4001 | |
| 4002 | if ((arg = calloc(1, sizeof(*arg))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4003 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4004 | err_code |= ERR_ALERT | ERR_ABORT; |
| 4005 | goto out; |
| 4006 | } |
| 4007 | |
| 4008 | if (!delim) { |
| 4009 | arg->name = NULL; |
| 4010 | arg->name_len = 0; |
| 4011 | delim = args[cur_arg]; |
| 4012 | } |
| 4013 | else { |
| 4014 | arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]); |
| 4015 | arg->name_len = delim - args[cur_arg]; |
| 4016 | delim++; |
| 4017 | } |
Christopher Faulet | b0b4238 | 2017-02-23 22:41:09 +0100 | [diff] [blame] | 4018 | arg->expr = sample_parse_expr((char*[]){delim, NULL}, |
| 4019 | &idx, file, linenum, &errmsg, |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 4020 | &curproxy->conf.args, NULL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4021 | if (arg->expr == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4022 | 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] | 4023 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4024 | free(arg->name); |
| 4025 | free(arg); |
| 4026 | goto out; |
| 4027 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 4028 | curmsg->nargs++; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4029 | LIST_APPEND(&curmsg->args, &arg->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4030 | cur_arg++; |
| 4031 | } |
| 4032 | curproxy->conf.args.file = NULL; |
| 4033 | curproxy->conf.args.line = 0; |
| 4034 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4035 | else if (strcmp(args[0], "acl") == 0) { |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4036 | err = invalid_char(args[1]); |
| 4037 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4038 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n", |
| 4039 | file, linenum, *err, args[1]); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4040 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4041 | goto out; |
| 4042 | } |
Tim Duesterhus | 0cf811a | 2020-02-05 21:00:50 +0100 | [diff] [blame] | 4043 | if (strcasecmp(args[1], "or") == 0) { |
Tim Duesterhus | f1bc24c | 2020-02-06 22:04:03 +0100 | [diff] [blame] | 4044 | ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a " |
Tim Duesterhus | 0cf811a | 2020-02-05 21:00:50 +0100 | [diff] [blame] | 4045 | "logical disjunction within a condition.\n", |
| 4046 | file, linenum, args[1]); |
| 4047 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4048 | goto out; |
| 4049 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4050 | 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] | 4051 | ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n", |
| 4052 | file, linenum, args[1], errmsg); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4053 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4054 | goto out; |
| 4055 | } |
| 4056 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4057 | else if (strcmp(args[0], "event") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4058 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4059 | ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4060 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4061 | goto out; |
| 4062 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4063 | /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */ |
| 4064 | /* goto out; */ |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4065 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4066 | if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4067 | curmsg->event = SPOE_EV_ON_CLIENT_SESS; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4068 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4069 | curmsg->event = SPOE_EV_ON_SERVER_SESS; |
| 4070 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4071 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4072 | curmsg->event = SPOE_EV_ON_TCP_REQ_FE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4073 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4074 | curmsg->event = SPOE_EV_ON_TCP_REQ_BE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4075 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4076 | curmsg->event = SPOE_EV_ON_TCP_RSP; |
| 4077 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4078 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4079 | curmsg->event = SPOE_EV_ON_HTTP_REQ_FE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4080 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4081 | curmsg->event = SPOE_EV_ON_HTTP_REQ_BE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4082 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4083 | curmsg->event = SPOE_EV_ON_HTTP_RSP; |
| 4084 | else { |
Joseph Herlant | f1da69d | 2018-11-15 13:49:02 -0800 | [diff] [blame] | 4085 | ha_alert("parsing [%s:%d] : unknown event '%s'.\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4086 | file, linenum, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4087 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4088 | goto out; |
| 4089 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4090 | |
| 4091 | if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) { |
| 4092 | struct acl_cond *cond; |
| 4093 | |
| 4094 | cond = build_acl_cond(file, linenum, &curmsg->acls, |
| 4095 | curproxy, (const char **)args+2, |
| 4096 | &errmsg); |
| 4097 | if (cond == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4098 | ha_alert("parsing [%s:%d] : error detected while " |
| 4099 | "parsing an 'event %s' condition : %s.\n", |
| 4100 | file, linenum, args[1], errmsg); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4101 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4102 | goto out; |
| 4103 | } |
| 4104 | curmsg->cond = cond; |
| 4105 | } |
| 4106 | else if (*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4107 | ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' " |
| 4108 | "or 'unless' followed by a condition but found '%s'.\n", |
| 4109 | file, linenum, args[1], args[2]); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4110 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4111 | goto out; |
| 4112 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4113 | } |
| 4114 | else if (!*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4115 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n", |
| 4116 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4117 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4118 | goto out; |
| 4119 | } |
| 4120 | out: |
| 4121 | free(errmsg); |
| 4122 | return err_code; |
| 4123 | } |
| 4124 | |
| 4125 | /* Return -1 on error, else 0 */ |
| 4126 | static int |
| 4127 | parse_spoe_flt(char **args, int *cur_arg, struct proxy *px, |
| 4128 | struct flt_conf *fconf, char **err, void *private) |
| 4129 | { |
| 4130 | struct list backup_sections; |
| 4131 | struct spoe_config *conf; |
| 4132 | struct spoe_message *msg, *msgback; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4133 | struct spoe_group *grp, *grpback; |
| 4134 | struct spoe_placeholder *ph, *phback; |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4135 | struct spoe_var_placeholder *vph, *vphback; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4136 | struct logsrv *logsrv, *logsrvback; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4137 | char *file = NULL, *engine = NULL; |
| 4138 | int ret, pos = *cur_arg + 1; |
| 4139 | |
Christopher Faulet | 84c844e | 2018-03-23 14:37:14 +0100 | [diff] [blame] | 4140 | LIST_INIT(&curmsgs); |
| 4141 | LIST_INIT(&curgrps); |
| 4142 | LIST_INIT(&curmphs); |
| 4143 | LIST_INIT(&curgphs); |
| 4144 | LIST_INIT(&curvars); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4145 | LIST_INIT(&curlogsrvs); |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 4146 | curpxopts = 0; |
| 4147 | curpxopts2 = 0; |
Christopher Faulet | 84c844e | 2018-03-23 14:37:14 +0100 | [diff] [blame] | 4148 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4149 | conf = calloc(1, sizeof(*conf)); |
| 4150 | if (conf == NULL) { |
| 4151 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 4152 | goto error; |
| 4153 | } |
| 4154 | conf->proxy = px; |
| 4155 | |
| 4156 | while (*args[pos]) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4157 | if (strcmp(args[pos], "config") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4158 | if (!*args[pos+1]) { |
| 4159 | memprintf(err, "'%s' : '%s' option without value", |
| 4160 | args[*cur_arg], args[pos]); |
| 4161 | goto error; |
| 4162 | } |
| 4163 | file = args[pos+1]; |
| 4164 | pos += 2; |
| 4165 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4166 | else if (strcmp(args[pos], "engine") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4167 | if (!*args[pos+1]) { |
| 4168 | memprintf(err, "'%s' : '%s' option without value", |
| 4169 | args[*cur_arg], args[pos]); |
| 4170 | goto error; |
| 4171 | } |
| 4172 | engine = args[pos+1]; |
| 4173 | pos += 2; |
| 4174 | } |
| 4175 | else { |
| 4176 | memprintf(err, "unknown keyword '%s'", args[pos]); |
| 4177 | goto error; |
| 4178 | } |
| 4179 | } |
| 4180 | if (file == NULL) { |
| 4181 | memprintf(err, "'%s' : missing config file", args[*cur_arg]); |
| 4182 | goto error; |
| 4183 | } |
| 4184 | |
| 4185 | /* backup sections and register SPOE sections */ |
| 4186 | LIST_INIT(&backup_sections); |
| 4187 | cfg_backup_sections(&backup_sections); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4188 | cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL); |
| 4189 | cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL); |
William Lallemand | d2ff56d | 2017-10-16 11:06:50 +0200 | [diff] [blame] | 4190 | cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4191 | |
| 4192 | /* Parse SPOE filter configuration file */ |
| 4193 | curengine = engine; |
| 4194 | curproxy = px; |
| 4195 | curagent = NULL; |
| 4196 | curmsg = NULL; |
| 4197 | ret = readcfgfile(file); |
| 4198 | curproxy = NULL; |
| 4199 | |
| 4200 | /* unregister SPOE sections and restore previous sections */ |
| 4201 | cfg_unregister_sections(); |
| 4202 | cfg_restore_sections(&backup_sections); |
| 4203 | |
| 4204 | if (ret == -1) { |
| 4205 | memprintf(err, "Could not open configuration file %s : %s", |
| 4206 | file, strerror(errno)); |
| 4207 | goto error; |
| 4208 | } |
| 4209 | if (ret & (ERR_ABORT|ERR_FATAL)) { |
| 4210 | memprintf(err, "Error(s) found in configuration file %s", file); |
| 4211 | goto error; |
| 4212 | } |
| 4213 | |
| 4214 | /* Check SPOE agent */ |
| 4215 | if (curagent == NULL) { |
| 4216 | memprintf(err, "No SPOE agent found in file %s", file); |
| 4217 | goto error; |
| 4218 | } |
| 4219 | if (curagent->b.name == NULL) { |
| 4220 | memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d", |
| 4221 | curagent->id, curagent->conf.file, curagent->conf.line); |
| 4222 | goto error; |
| 4223 | } |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 4224 | if (curagent->timeout.hello == TICK_ETERNITY || |
| 4225 | curagent->timeout.idle == TICK_ETERNITY || |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 4226 | curagent->timeout.processing == TICK_ETERNITY) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4227 | ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n" |
| 4228 | " | While not properly invalid, you will certainly encounter various problems\n" |
| 4229 | " | with such a configuration. To fix this, please ensure that all following\n" |
| 4230 | " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n", |
| 4231 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4232 | } |
| 4233 | if (curagent->var_pfx == NULL) { |
| 4234 | char *tmp = curagent->id; |
| 4235 | |
| 4236 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4237 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4238 | memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. " |
| 4239 | "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n", |
| 4240 | curagent->id, curagent->id, curagent->conf.file, curagent->conf.line); |
| 4241 | goto error; |
| 4242 | } |
| 4243 | tmp++; |
| 4244 | } |
| 4245 | curagent->var_pfx = strdup(curagent->id); |
| 4246 | } |
| 4247 | |
Christopher Faulet | b7426d1 | 2018-03-21 14:12:17 +0100 | [diff] [blame] | 4248 | if (curagent->var_on_error) { |
| 4249 | struct arg arg; |
| 4250 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4251 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | b7426d1 | 2018-03-21 14:12:17 +0100 | [diff] [blame] | 4252 | curagent->var_pfx, curagent->var_on_error); |
| 4253 | |
| 4254 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4255 | arg.data.str.area = trash.area; |
| 4256 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4257 | arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */ |
Christopher Faulet | b7426d1 | 2018-03-21 14:12:17 +0100 | [diff] [blame] | 4258 | if (!vars_check_arg(&arg, err)) { |
| 4259 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4260 | curagent->id, curagent->var_pfx, curagent->var_on_error, *err); |
| 4261 | goto error; |
| 4262 | } |
| 4263 | } |
| 4264 | |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4265 | if (curagent->var_t_process) { |
| 4266 | struct arg arg; |
| 4267 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4268 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4269 | curagent->var_pfx, curagent->var_t_process); |
| 4270 | |
| 4271 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4272 | arg.data.str.area = trash.area; |
| 4273 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4274 | arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */ |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4275 | if (!vars_check_arg(&arg, err)) { |
| 4276 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4277 | curagent->id, curagent->var_pfx, curagent->var_t_process, *err); |
| 4278 | goto error; |
| 4279 | } |
| 4280 | } |
| 4281 | |
| 4282 | if (curagent->var_t_total) { |
| 4283 | struct arg arg; |
| 4284 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4285 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4286 | curagent->var_pfx, curagent->var_t_total); |
| 4287 | |
| 4288 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4289 | arg.data.str.area = trash.area; |
| 4290 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4291 | arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */ |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4292 | if (!vars_check_arg(&arg, err)) { |
| 4293 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4294 | curagent->id, curagent->var_pfx, curagent->var_t_process, *err); |
| 4295 | goto error; |
| 4296 | } |
| 4297 | } |
| 4298 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4299 | if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4300 | ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n", |
| 4301 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4302 | goto finish; |
| 4303 | } |
| 4304 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4305 | /* Replace placeholders by the corresponding messages for the SPOE |
| 4306 | * agent */ |
| 4307 | list_for_each_entry(ph, &curmphs, list) { |
| 4308 | list_for_each_entry(msg, &curmsgs, list) { |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4309 | struct spoe_arg *arg; |
| 4310 | unsigned int where; |
| 4311 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4312 | if (strcmp(msg->id, ph->id) == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4313 | if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) { |
| 4314 | if (msg->event == SPOE_EV_ON_TCP_REQ_BE) |
| 4315 | msg->event = SPOE_EV_ON_TCP_REQ_FE; |
| 4316 | if (msg->event == SPOE_EV_ON_HTTP_REQ_BE) |
| 4317 | msg->event = SPOE_EV_ON_HTTP_REQ_FE; |
| 4318 | } |
| 4319 | if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS || |
| 4320 | msg->event == SPOE_EV_ON_TCP_REQ_FE || |
| 4321 | msg->event == SPOE_EV_ON_HTTP_REQ_FE)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4322 | ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n", |
| 4323 | px->id, msg->conf.file, msg->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4324 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4325 | } |
| 4326 | if (msg->event == SPOE_EV_NONE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4327 | ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n", |
| 4328 | px->id, msg->id, msg->conf.file, msg->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4329 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4330 | } |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4331 | |
| 4332 | where = 0; |
| 4333 | switch (msg->event) { |
| 4334 | case SPOE_EV_ON_CLIENT_SESS: |
| 4335 | where |= SMP_VAL_FE_CON_ACC; |
| 4336 | break; |
| 4337 | |
| 4338 | case SPOE_EV_ON_TCP_REQ_FE: |
| 4339 | where |= SMP_VAL_FE_REQ_CNT; |
| 4340 | break; |
| 4341 | |
| 4342 | case SPOE_EV_ON_HTTP_REQ_FE: |
| 4343 | where |= SMP_VAL_FE_HRQ_HDR; |
| 4344 | break; |
| 4345 | |
| 4346 | case SPOE_EV_ON_TCP_REQ_BE: |
| 4347 | if (px->cap & PR_CAP_FE) |
| 4348 | where |= SMP_VAL_FE_REQ_CNT; |
| 4349 | if (px->cap & PR_CAP_BE) |
| 4350 | where |= SMP_VAL_BE_REQ_CNT; |
| 4351 | break; |
| 4352 | |
| 4353 | case SPOE_EV_ON_HTTP_REQ_BE: |
| 4354 | if (px->cap & PR_CAP_FE) |
| 4355 | where |= SMP_VAL_FE_HRQ_HDR; |
| 4356 | if (px->cap & PR_CAP_BE) |
| 4357 | where |= SMP_VAL_BE_HRQ_HDR; |
| 4358 | break; |
| 4359 | |
| 4360 | case SPOE_EV_ON_SERVER_SESS: |
| 4361 | where |= SMP_VAL_BE_SRV_CON; |
| 4362 | break; |
| 4363 | |
| 4364 | case SPOE_EV_ON_TCP_RSP: |
| 4365 | if (px->cap & PR_CAP_FE) |
| 4366 | where |= SMP_VAL_FE_RES_CNT; |
| 4367 | if (px->cap & PR_CAP_BE) |
| 4368 | where |= SMP_VAL_BE_RES_CNT; |
| 4369 | break; |
| 4370 | |
| 4371 | case SPOE_EV_ON_HTTP_RSP: |
| 4372 | if (px->cap & PR_CAP_FE) |
| 4373 | where |= SMP_VAL_FE_HRS_HDR; |
| 4374 | if (px->cap & PR_CAP_BE) |
| 4375 | where |= SMP_VAL_BE_HRS_HDR; |
| 4376 | break; |
| 4377 | |
| 4378 | default: |
| 4379 | break; |
| 4380 | } |
| 4381 | |
| 4382 | list_for_each_entry(arg, &msg->args, list) { |
| 4383 | if (!(arg->expr->fetch->val & where)) { |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4384 | memprintf(err, "Ignore SPOE message '%s' at %s:%d: " |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4385 | "some args extract information from '%s', " |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4386 | "none of which is available here ('%s')", |
| 4387 | msg->id, msg->conf.file, msg->conf.line, |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4388 | sample_ckp_names(arg->expr->fetch->use), |
| 4389 | sample_ckp_names(where)); |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4390 | goto error; |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4391 | } |
| 4392 | } |
| 4393 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4394 | msg->agent = curagent; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4395 | LIST_APPEND(&curagent->events[msg->event], &msg->by_evt); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4396 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4397 | } |
| 4398 | } |
| 4399 | 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] | 4400 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4401 | goto error; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4402 | next_mph: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4403 | continue; |
| 4404 | } |
| 4405 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4406 | /* Replace placeholders by the corresponding groups for the SPOE |
| 4407 | * agent */ |
| 4408 | list_for_each_entry(ph, &curgphs, list) { |
| 4409 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4410 | if (strcmp(grp->id, ph->id) == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4411 | grp->agent = curagent; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4412 | LIST_DELETE(&grp->list); |
| 4413 | LIST_APPEND(&curagent->groups, &grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4414 | goto next_aph; |
| 4415 | } |
| 4416 | } |
| 4417 | memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d", |
| 4418 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 4419 | goto error; |
| 4420 | next_aph: |
| 4421 | continue; |
| 4422 | } |
| 4423 | |
| 4424 | /* Replace placeholders by the corresponding message for each SPOE |
| 4425 | * group of the SPOE agent */ |
| 4426 | list_for_each_entry(grp, &curagent->groups, list) { |
| 4427 | list_for_each_entry_safe(ph, phback, &grp->phs, list) { |
| 4428 | list_for_each_entry(msg, &curmsgs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4429 | if (strcmp(msg->id, ph->id) == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4430 | if (msg->group != NULL) { |
| 4431 | memprintf(err, "SPOE message '%s' already belongs to " |
| 4432 | "the SPOE group '%s' declare at %s:%d", |
| 4433 | msg->id, msg->group->id, |
| 4434 | msg->group->conf.file, |
| 4435 | msg->group->conf.line); |
| 4436 | goto error; |
| 4437 | } |
| 4438 | |
| 4439 | /* Scope for arguments are not checked for now. We will check |
| 4440 | * them only if a rule use the corresponding SPOE group. */ |
| 4441 | msg->agent = curagent; |
| 4442 | msg->group = grp; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4443 | LIST_DELETE(&ph->list); |
| 4444 | LIST_APPEND(&grp->messages, &msg->by_grp); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4445 | goto next_mph_grp; |
| 4446 | } |
| 4447 | } |
| 4448 | memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d", |
| 4449 | grp->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 4450 | goto error; |
| 4451 | next_mph_grp: |
| 4452 | continue; |
| 4453 | } |
| 4454 | } |
| 4455 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4456 | finish: |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4457 | /* move curmsgs to the agent message list */ |
| 4458 | curmsgs.n->p = &curagent->messages; |
| 4459 | curmsgs.p->n = &curagent->messages; |
| 4460 | curagent->messages = curmsgs; |
| 4461 | LIST_INIT(&curmsgs); |
| 4462 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 4463 | conf->id = strdup(engine ? engine : curagent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4464 | conf->agent = curagent; |
Christopher Faulet | 541e261 | 2021-08-02 17:51:01 +0200 | [diff] [blame] | 4465 | curagent->spoe_conf = conf; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4466 | |
| 4467 | /* Start agent's proxy initialization here. It will be finished during |
| 4468 | * the filter init. */ |
| 4469 | memset(&conf->agent_fe, 0, sizeof(conf->agent_fe)); |
| 4470 | init_new_proxy(&conf->agent_fe); |
| 4471 | conf->agent_fe.id = conf->agent->id; |
| 4472 | conf->agent_fe.parent = conf->agent; |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 4473 | conf->agent_fe.options |= curpxopts; |
| 4474 | conf->agent_fe.options2 |= curpxopts2; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4475 | |
| 4476 | list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4477 | LIST_DELETE(&logsrv->list); |
| 4478 | LIST_APPEND(&conf->agent_fe.logsrvs, &logsrv->list); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4479 | } |
| 4480 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4481 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4482 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4483 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4484 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4485 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4486 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4487 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4488 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4489 | list_for_each_entry_safe(vph, vphback, &curvars, list) { |
| 4490 | struct arg arg; |
| 4491 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4492 | trash.data = snprintf(trash.area, trash.size, "proc.%s.%s", |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4493 | curagent->var_pfx, vph->name); |
| 4494 | |
| 4495 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4496 | arg.data.str.area = trash.area; |
| 4497 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4498 | arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */ |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4499 | if (!vars_check_arg(&arg, err)) { |
| 4500 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4501 | curagent->id, curagent->var_pfx, vph->name, *err); |
| 4502 | goto error; |
| 4503 | } |
| 4504 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4505 | LIST_DELETE(&vph->list); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4506 | free(vph->name); |
| 4507 | free(vph); |
| 4508 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4509 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4510 | LIST_DELETE(&grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4511 | spoe_release_group(grp); |
| 4512 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4513 | *cur_arg = pos; |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 4514 | fconf->id = spoe_filter_id; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4515 | fconf->ops = &spoe_ops; |
| 4516 | fconf->conf = conf; |
| 4517 | return 0; |
| 4518 | |
| 4519 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4520 | spoe_release_agent(curagent); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4521 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4522 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4523 | spoe_release_placeholder(ph); |
| 4524 | } |
| 4525 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4526 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4527 | spoe_release_placeholder(ph); |
| 4528 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4529 | list_for_each_entry_safe(vph, vphback, &curvars, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4530 | LIST_DELETE(&vph->list); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4531 | free(vph->name); |
| 4532 | free(vph); |
| 4533 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4534 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4535 | LIST_DELETE(&grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4536 | spoe_release_group(grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4537 | } |
| 4538 | list_for_each_entry_safe(msg, msgback, &curmsgs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4539 | LIST_DELETE(&msg->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4540 | spoe_release_message(msg); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4541 | } |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4542 | list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4543 | LIST_DELETE(&logsrv->list); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4544 | free(logsrv); |
| 4545 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4546 | free(conf); |
| 4547 | return -1; |
| 4548 | } |
| 4549 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4550 | /* Send message of a SPOE group. This is the action_ptr callback of a rule |
| 4551 | * associated to a "send-spoe-group" action. |
| 4552 | * |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4553 | * It returns ACT_RET_CONT if processing is finished (with error or not), it returns |
| 4554 | * ACT_RET_YIELD if the action is in progress. */ |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4555 | static enum act_return |
| 4556 | spoe_send_group(struct act_rule *rule, struct proxy *px, |
| 4557 | struct session *sess, struct stream *s, int flags) |
| 4558 | { |
| 4559 | struct filter *filter; |
| 4560 | struct spoe_agent *agent = NULL; |
| 4561 | struct spoe_group *group = NULL; |
| 4562 | struct spoe_context *ctx = NULL; |
| 4563 | int ret, dir; |
| 4564 | |
| 4565 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 4566 | if (filter->config == rule->arg.act.p[0]) { |
| 4567 | agent = rule->arg.act.p[2]; |
| 4568 | group = rule->arg.act.p[3]; |
| 4569 | ctx = filter->ctx; |
| 4570 | break; |
| 4571 | } |
| 4572 | } |
| 4573 | if (agent == NULL || group == NULL || ctx == NULL) |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4574 | return ACT_RET_CONT; |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4575 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 4576 | return ACT_RET_CONT; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4577 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4578 | switch (rule->from) { |
| 4579 | case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break; |
| 4580 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 4581 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 4582 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 4583 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
| 4584 | default: |
| 4585 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 4586 | " - internal error while execute spoe-send-group\n", |
| 4587 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 4588 | __FUNCTION__, s); |
| 4589 | send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n", |
| 4590 | agent->id); |
| 4591 | return ACT_RET_CONT; |
| 4592 | } |
| 4593 | |
| 4594 | ret = spoe_process_group(s, ctx, group, dir); |
| 4595 | if (ret == 1) |
| 4596 | return ACT_RET_CONT; |
| 4597 | else if (ret == 0) { |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 4598 | if (flags & ACT_OPT_FINAL) { |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4599 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 4600 | " - failed to process group '%s': interrupted by caller\n", |
| 4601 | (int)now.tv_sec, (int)now.tv_usec, |
| 4602 | agent->id, __FUNCTION__, s, group->id); |
| 4603 | ctx->status_code = SPOE_CTX_ERR_INTERRUPT; |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 4604 | spoe_stop_processing(agent, ctx); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 4605 | spoe_handle_processing_error(s, agent, ctx, dir); |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4606 | return ACT_RET_CONT; |
| 4607 | } |
| 4608 | return ACT_RET_YIELD; |
| 4609 | } |
| 4610 | else |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4611 | return ACT_RET_CONT; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4612 | } |
| 4613 | |
| 4614 | /* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE |
| 4615 | * group associated to <rule>. The format of an rule using 'send-spoe-group' |
| 4616 | * action should be: |
| 4617 | * |
| 4618 | * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id> |
| 4619 | * |
| 4620 | * So, we'll loop on each configured SPOE filter for the proxy <px> to find the |
| 4621 | * SPOE engine matching <engine-id>. And then, we'll try to find the good group |
| 4622 | * matching <group-id>. Finally, we'll check all messages referenced by the SPOE |
| 4623 | * group. |
| 4624 | * |
| 4625 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 4626 | * filled. |
| 4627 | */ |
| 4628 | static int |
| 4629 | check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err) |
| 4630 | { |
| 4631 | struct flt_conf *fconf; |
| 4632 | struct spoe_config *conf; |
| 4633 | struct spoe_agent *agent = NULL; |
| 4634 | struct spoe_group *group; |
| 4635 | struct spoe_message *msg; |
| 4636 | char *engine_id = rule->arg.act.p[0]; |
| 4637 | char *group_id = rule->arg.act.p[1]; |
| 4638 | unsigned int where = 0; |
| 4639 | |
| 4640 | switch (rule->from) { |
| 4641 | case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break; |
| 4642 | case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break; |
| 4643 | case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break; |
| 4644 | case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break; |
| 4645 | case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break; |
| 4646 | default: |
| 4647 | memprintf(err, |
| 4648 | "internal error, unexpected rule->from=%d, please report this bug!", |
| 4649 | rule->from); |
| 4650 | goto error; |
| 4651 | } |
| 4652 | |
| 4653 | /* Try to find the SPOE engine by checking all SPOE filters for proxy |
| 4654 | * <px> */ |
| 4655 | list_for_each_entry(fconf, &px->filter_configs, list) { |
| 4656 | conf = fconf->conf; |
| 4657 | |
| 4658 | /* This is not an SPOE filter */ |
| 4659 | if (fconf->id != spoe_filter_id) |
| 4660 | continue; |
| 4661 | |
| 4662 | /* This is the good engine */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4663 | if (strcmp(conf->id, engine_id) == 0) { |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4664 | agent = conf->agent; |
| 4665 | break; |
| 4666 | } |
| 4667 | } |
| 4668 | if (agent == NULL) { |
| 4669 | memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'", |
| 4670 | engine_id, group_id); |
| 4671 | goto error; |
| 4672 | } |
| 4673 | |
| 4674 | /* Try to find the right group */ |
| 4675 | list_for_each_entry(group, &agent->groups, list) { |
| 4676 | /* This is the good group */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4677 | if (strcmp(group->id, group_id) == 0) |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4678 | break; |
| 4679 | } |
| 4680 | if (&group->list == &agent->groups) { |
| 4681 | memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration", |
| 4682 | group_id, engine_id); |
| 4683 | goto error; |
| 4684 | } |
| 4685 | |
| 4686 | /* Ok, we found the group, we need to check messages and their |
| 4687 | * arguments */ |
| 4688 | list_for_each_entry(msg, &group->messages, by_grp) { |
| 4689 | struct spoe_arg *arg; |
| 4690 | |
| 4691 | list_for_each_entry(arg, &msg->args, list) { |
| 4692 | if (!(arg->expr->fetch->val & where)) { |
| 4693 | memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: " |
| 4694 | "some args extract information from '%s'," |
| 4695 | "none of which is available here ('%s')", |
| 4696 | msg->id, group->id, msg->conf.file, msg->conf.line, |
| 4697 | sample_ckp_names(arg->expr->fetch->use), |
| 4698 | sample_ckp_names(where)); |
| 4699 | goto error; |
| 4700 | } |
| 4701 | } |
| 4702 | } |
| 4703 | |
| 4704 | free(engine_id); |
| 4705 | free(group_id); |
| 4706 | rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */ |
| 4707 | rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */ |
| 4708 | rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */ |
| 4709 | rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */ |
| 4710 | return 1; |
| 4711 | |
| 4712 | error: |
| 4713 | free(engine_id); |
| 4714 | free(group_id); |
| 4715 | return 0; |
| 4716 | } |
| 4717 | |
| 4718 | /* Parse 'send-spoe-group' action following the format: |
| 4719 | * |
| 4720 | * ... send-spoe-group <engine-id> <group-id> |
| 4721 | * |
| 4722 | * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error |
| 4723 | * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group |
| 4724 | * ids are saved and used later, when the rule will be checked. |
| 4725 | */ |
| 4726 | static enum act_parse_ret |
| 4727 | parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px, |
| 4728 | struct act_rule *rule, char **err) |
| 4729 | { |
| 4730 | if (!*args[*orig_arg] || !*args[*orig_arg+1] || |
| 4731 | (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) { |
| 4732 | memprintf(err, "expects 2 arguments: <engine-id> <group-id>"); |
| 4733 | return ACT_RET_PRS_ERR; |
| 4734 | } |
| 4735 | rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */ |
| 4736 | rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */ |
| 4737 | |
| 4738 | (*orig_arg) += 2; |
| 4739 | |
| 4740 | rule->action = ACT_CUSTOM; |
| 4741 | rule->action_ptr = spoe_send_group; |
| 4742 | rule->check_ptr = check_send_spoe_group; |
| 4743 | return ACT_RET_PRS_OK; |
| 4744 | } |
| 4745 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4746 | |
| 4747 | /* Declare the filter parser for "spoe" keyword */ |
| 4748 | static struct flt_kw_list flt_kws = { "SPOE", { }, { |
| 4749 | { "spoe", parse_spoe_flt, NULL }, |
| 4750 | { NULL, NULL, NULL }, |
| 4751 | } |
| 4752 | }; |
| 4753 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4754 | INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws); |
| 4755 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4756 | /* Delcate the action parser for "spoe-action" keyword */ |
| 4757 | static struct action_kw_list tcp_req_action_kws = { { }, { |
| 4758 | { "send-spoe-group", parse_send_spoe_group }, |
| 4759 | { /* END */ }, |
| 4760 | } |
| 4761 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4762 | |
| 4763 | INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws); |
| 4764 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4765 | static struct action_kw_list tcp_res_action_kws = { { }, { |
| 4766 | { "send-spoe-group", parse_send_spoe_group }, |
| 4767 | { /* END */ }, |
| 4768 | } |
| 4769 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4770 | |
| 4771 | INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws); |
| 4772 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4773 | static struct action_kw_list http_req_action_kws = { { }, { |
| 4774 | { "send-spoe-group", parse_send_spoe_group }, |
| 4775 | { /* END */ }, |
| 4776 | } |
| 4777 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4778 | |
| 4779 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws); |
| 4780 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4781 | static struct action_kw_list http_res_action_kws = { { }, { |
| 4782 | { "send-spoe-group", parse_send_spoe_group }, |
| 4783 | { /* END */ }, |
| 4784 | } |
| 4785 | }; |
| 4786 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4787 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws); |