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> |
Tim Duesterhus | 75e2f8d | 2021-09-16 17:38:26 +0200 | [diff] [blame] | 20 | #include <haproxy/check.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 21 | #include <haproxy/filters.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 22 | #include <haproxy/freq_ctr.h> |
Willy Tarreau | 762d7a5 | 2020-06-04 11:23:07 +0200 | [diff] [blame] | 23 | #include <haproxy/frontend.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 24 | #include <haproxy/global.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 25 | #include <haproxy/http_rules.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 26 | #include <haproxy/log.h> |
Willy Tarreau | d0ef439 | 2020-06-02 09:38:52 +0200 | [diff] [blame] | 27 | #include <haproxy/pool.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 28 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 29 | #include <haproxy/sample.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 30 | #include <haproxy/session.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 31 | #include <haproxy/signal.h> |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 32 | #include <haproxy/sink.h> |
Willy Tarreau | 6c58ab0 | 2020-06-04 22:35:49 +0200 | [diff] [blame] | 33 | #include <haproxy/spoe.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 34 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 35 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 36 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 37 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 38 | #include <haproxy/thread.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 39 | #include <haproxy/time.h> |
Willy Tarreau | e16ada1 | 2021-05-08 12:57:17 +0200 | [diff] [blame] | 40 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 41 | #include <haproxy/vars.h> |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 42 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 43 | |
| 44 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 45 | #define SPOE_PRINTF(x...) fprintf(x) |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 46 | #define SPOE_DEBUG_STMT(statement) statement |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 47 | #else |
| 48 | #define SPOE_PRINTF(x...) |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 49 | #define SPOE_DEBUG_STMT(statement) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 50 | #endif |
| 51 | |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 52 | /* Reserved 4 bytes to the frame size. So a frame and its size can be written |
| 53 | * together in a buffer */ |
| 54 | #define MAX_FRAME_SIZE global.tune.bufsize - 4 |
| 55 | |
| 56 | /* The minimum size for a frame */ |
| 57 | #define MIN_FRAME_SIZE 256 |
| 58 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 59 | /* Reserved for the metadata and the frame type. |
| 60 | * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */ |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 61 | #define FRAME_HDR_SIZE 32 |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 62 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 63 | /* Helper to get SPOE ctx inside an appctx */ |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 64 | #define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr)) |
| 65 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 66 | /* SPOE filter id. Used to identify SPOE filters */ |
| 67 | const char *spoe_filter_id = "SPOE filter"; |
| 68 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 69 | /* Set if the handle on SIGUSR1 is registered */ |
| 70 | static int sighandler_registered = 0; |
| 71 | |
| 72 | /* proxy used during the parsing */ |
| 73 | struct proxy *curproxy = NULL; |
| 74 | |
| 75 | /* The name of the SPOE engine, used during the parsing */ |
| 76 | char *curengine = NULL; |
| 77 | |
| 78 | /* SPOE agent used during the parsing */ |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 79 | /* SPOE agent/group/message used during the parsing */ |
| 80 | struct spoe_agent *curagent = NULL; |
| 81 | struct spoe_group *curgrp = NULL; |
| 82 | struct spoe_message *curmsg = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 83 | |
| 84 | /* list of SPOE messages and placeholders used during the parsing */ |
| 85 | struct list curmsgs; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 86 | struct list curgrps; |
| 87 | struct list curmphs; |
| 88 | struct list curgphs; |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 89 | struct list curvars; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 90 | |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 91 | /* list of log servers used during the parsing */ |
| 92 | struct list curlogsrvs; |
| 93 | |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 94 | /* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */ |
| 95 | int curpxopts; |
| 96 | int curpxopts2; |
| 97 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 98 | /* Pools used to allocate SPOE structs */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 99 | DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context)); |
| 100 | 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] | 101 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 102 | struct flt_ops spoe_ops; |
| 103 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 104 | static int spoe_queue_context(struct spoe_context *ctx); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 105 | static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait); |
| 106 | static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait); |
Christopher Faulet | 6f1296b | 2021-08-02 17:53:56 +0200 | [diff] [blame] | 107 | static struct appctx *spoe_create_appctx(struct spoe_config *conf); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 108 | |
| 109 | /******************************************************************** |
| 110 | * helper functions/globals |
| 111 | ********************************************************************/ |
| 112 | static void |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 113 | spoe_release_placeholder(struct spoe_placeholder *ph) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 114 | { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 115 | if (!ph) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 116 | return; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 117 | free(ph->id); |
| 118 | free(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 121 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 122 | spoe_release_message(struct spoe_message *msg) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 123 | { |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 124 | struct spoe_arg *arg, *argback; |
| 125 | struct acl *acl, *aclback; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 126 | |
| 127 | if (!msg) |
| 128 | return; |
| 129 | free(msg->id); |
| 130 | free(msg->conf.file); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 131 | list_for_each_entry_safe(arg, argback, &msg->args, list) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 132 | release_sample_expr(arg->expr); |
| 133 | free(arg->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 134 | LIST_DELETE(&arg->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 135 | free(arg); |
| 136 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 137 | list_for_each_entry_safe(acl, aclback, &msg->acls, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 138 | LIST_DELETE(&acl->list); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 139 | prune_acl(acl); |
| 140 | free(acl); |
| 141 | } |
| 142 | if (msg->cond) { |
| 143 | prune_acl_cond(msg->cond); |
| 144 | free(msg->cond); |
| 145 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 146 | free(msg); |
| 147 | } |
| 148 | |
| 149 | static void |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 150 | spoe_release_group(struct spoe_group *grp) |
| 151 | { |
| 152 | if (!grp) |
| 153 | return; |
| 154 | free(grp->id); |
| 155 | free(grp->conf.file); |
| 156 | free(grp); |
| 157 | } |
| 158 | |
| 159 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 160 | spoe_release_agent(struct spoe_agent *agent) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 161 | { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 162 | struct spoe_message *msg, *msgback; |
| 163 | struct spoe_group *grp, *grpback; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 164 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 165 | |
| 166 | if (!agent) |
| 167 | return; |
| 168 | free(agent->id); |
| 169 | free(agent->conf.file); |
| 170 | free(agent->var_pfx); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 171 | free(agent->var_on_error); |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 172 | free(agent->var_t_process); |
| 173 | free(agent->var_t_total); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 174 | list_for_each_entry_safe(msg, msgback, &agent->messages, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 175 | LIST_DELETE(&msg->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 176 | spoe_release_message(msg); |
| 177 | } |
| 178 | list_for_each_entry_safe(grp, grpback, &agent->groups, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 179 | LIST_DELETE(&grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 180 | spoe_release_group(grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 181 | } |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 182 | if (agent->rt) { |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 183 | for (i = 0; i < global.nbthread; ++i) { |
| 184 | free(agent->rt[i].engine_id); |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 185 | HA_SPIN_DESTROY(&agent->rt[i].lock); |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 186 | } |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 187 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 188 | free(agent->rt); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 189 | free(agent); |
| 190 | } |
| 191 | |
| 192 | static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 193 | [SPOE_FRM_ERR_NONE] = "normal", |
| 194 | [SPOE_FRM_ERR_IO] = "I/O error", |
| 195 | [SPOE_FRM_ERR_TOUT] = "a timeout occurred", |
| 196 | [SPOE_FRM_ERR_TOO_BIG] = "frame is too big", |
| 197 | [SPOE_FRM_ERR_INVALID] = "invalid frame received", |
| 198 | [SPOE_FRM_ERR_NO_VSN] = "version value not found", |
| 199 | [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found", |
| 200 | [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found", |
| 201 | [SPOE_FRM_ERR_BAD_VSN] = "unsupported version", |
| 202 | [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small", |
| 203 | [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported", |
| 204 | [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames", |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 205 | [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 206 | [SPOE_FRM_ERR_RES] = "resource allocation error", |
| 207 | [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | static const char *spoe_event_str[SPOE_EV_EVENTS] = { |
| 211 | [SPOE_EV_ON_CLIENT_SESS] = "on-client-session", |
| 212 | [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request", |
| 213 | [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request", |
| 214 | [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request", |
| 215 | [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request", |
| 216 | |
| 217 | [SPOE_EV_ON_SERVER_SESS] = "on-server-session", |
| 218 | [SPOE_EV_ON_TCP_RSP] = "on-tcp-response", |
| 219 | [SPOE_EV_ON_HTTP_RSP] = "on-http-response", |
| 220 | }; |
| 221 | |
| 222 | |
| 223 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 224 | |
| 225 | static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 226 | [SPOE_CTX_ST_NONE] = "NONE", |
| 227 | [SPOE_CTX_ST_READY] = "READY", |
| 228 | [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS", |
| 229 | [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS", |
| 230 | [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK", |
| 231 | [SPOE_CTX_ST_DONE] = "DONE", |
| 232 | [SPOE_CTX_ST_ERROR] = "ERROR", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 236 | [SPOE_APPCTX_ST_CONNECT] = "CONNECT", |
| 237 | [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING", |
| 238 | [SPOE_APPCTX_ST_IDLE] = "IDLE", |
| 239 | [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING", |
| 240 | [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY", |
| 241 | [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK", |
| 242 | [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT", |
| 243 | [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING", |
| 244 | [SPOE_APPCTX_ST_EXIT] = "EXIT", |
| 245 | [SPOE_APPCTX_ST_END] = "END", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | #endif |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 249 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 250 | /* Used to generates a unique id for an engine. On success, it returns a |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 251 | * 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] | 252 | * allocation failed, it returns NULL. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 253 | static char * |
| 254 | generate_pseudo_uuid() |
| 255 | { |
Willy Tarreau | ee3bcdd | 2020-03-08 17:48:17 +0100 | [diff] [blame] | 256 | ha_generate_uuid(&trash); |
Kevin Zhu | 079f808 | 2020-03-13 10:39:51 +0800 | [diff] [blame] | 257 | return my_strndup(trash.area, trash.data); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 258 | } |
| 259 | |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 260 | |
| 261 | static inline void |
| 262 | spoe_update_stat_time(struct timeval *tv, long *t) |
| 263 | { |
| 264 | if (*t == -1) |
| 265 | *t = tv_ms_elapsed(tv, &now); |
| 266 | else |
| 267 | *t += tv_ms_elapsed(tv, &now); |
| 268 | tv_zero(tv); |
| 269 | } |
| 270 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 271 | /******************************************************************** |
| 272 | * Functions that encode/decode SPOE frames |
| 273 | ********************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 274 | /* Helper to get static string length, excluding the terminating null byte */ |
| 275 | #define SLEN(str) (sizeof(str)-1) |
| 276 | |
| 277 | /* Predefined key used in HELLO/DISCONNECT frames */ |
| 278 | #define SUPPORTED_VERSIONS_KEY "supported-versions" |
| 279 | #define VERSION_KEY "version" |
| 280 | #define MAX_FRAME_SIZE_KEY "max-frame-size" |
| 281 | #define CAPABILITIES_KEY "capabilities" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 282 | #define ENGINE_ID_KEY "engine-id" |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 283 | #define HEALTHCHECK_KEY "healthcheck" |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 284 | #define STATUS_CODE_KEY "status-code" |
| 285 | #define MSG_KEY "message" |
| 286 | |
| 287 | struct spoe_version { |
| 288 | char *str; |
| 289 | int min; |
| 290 | int max; |
| 291 | }; |
| 292 | |
| 293 | /* All supported versions */ |
| 294 | static struct spoe_version supported_versions[] = { |
Christopher Faulet | 6381650 | 2018-05-31 14:56:42 +0200 | [diff] [blame] | 295 | /* 1.0 is now unsupported because of a bug about frame's flags*/ |
| 296 | {"2.0", 2000, 2000}, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 297 | {NULL, 0, 0} |
| 298 | }; |
| 299 | |
| 300 | /* Comma-separated list of supported versions */ |
Christopher Faulet | 6381650 | 2018-05-31 14:56:42 +0200 | [diff] [blame] | 301 | #define SUPPORTED_VERSIONS_VAL "2.0" |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 302 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 303 | /* Convert a string to a SPOE version value. The string must follow the format |
| 304 | * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR). |
| 305 | * If an error occurred, -1 is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 306 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 307 | spoe_str_to_vsn(const char *str, size_t len) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 308 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 309 | const char *p, *end; |
| 310 | int maj, min, vsn; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 311 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 312 | p = str; |
| 313 | end = str+len; |
| 314 | maj = min = 0; |
| 315 | vsn = -1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 316 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 317 | /* skip leading spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 318 | while (p < end && isspace((unsigned char)*p)) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 319 | p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 320 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 321 | /* parse Major number, until the '.' */ |
| 322 | while (*p != '.') { |
| 323 | if (p >= end || *p < '0' || *p > '9') |
| 324 | goto out; |
| 325 | maj *= 10; |
| 326 | maj += (*p - '0'); |
| 327 | p++; |
| 328 | } |
| 329 | |
| 330 | /* check Major version */ |
| 331 | if (!maj) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 332 | goto out; |
| 333 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 334 | p++; /* skip the '.' */ |
| 335 | if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */ |
| 336 | goto out; |
| 337 | |
| 338 | /* Parse Minor number */ |
| 339 | while (p < end) { |
| 340 | if (*p < '0' || *p > '9') |
| 341 | break; |
| 342 | min *= 10; |
| 343 | min += (*p - '0'); |
| 344 | p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 345 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 346 | |
| 347 | /* check Minor number */ |
| 348 | if (min > 999) |
| 349 | goto out; |
| 350 | |
| 351 | /* skip trailing spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 352 | while (p < end && isspace((unsigned char)*p)) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 353 | p++; |
| 354 | if (p != end) |
| 355 | goto out; |
| 356 | |
| 357 | vsn = maj * 1000 + min; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 358 | out: |
| 359 | return vsn; |
| 360 | } |
| 361 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 362 | /* 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] | 363 | * 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] | 364 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 365 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 366 | spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 367 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 368 | struct buffer *chk; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 369 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 370 | char *p, *end; |
| 371 | unsigned int flags = SPOE_FRM_FL_FIN; |
| 372 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 373 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 374 | p = frame; |
| 375 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 376 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 377 | /* Set Frame type */ |
| 378 | *p++ = SPOE_FRM_T_HAPROXY_HELLO; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 379 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 380 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 381 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 382 | memcpy(p, (char *)&flags, 4); |
| 383 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 384 | |
| 385 | /* No stream-id and frame-id for HELLO frames */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 386 | *p++ = 0; *p++ = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 387 | |
| 388 | /* There are 3 mandatory items: "supported-versions", "max-frame-size" |
| 389 | * and "capabilities" */ |
| 390 | |
| 391 | /* "supported-versions" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 392 | sz = SLEN(SUPPORTED_VERSIONS_KEY); |
| 393 | if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1) |
| 394 | goto too_big; |
| 395 | |
| 396 | *p++ = SPOE_DATA_T_STR; |
| 397 | sz = SLEN(SUPPORTED_VERSIONS_VAL); |
| 398 | if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1) |
| 399 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 400 | |
| 401 | /* "max-fram-size" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 402 | sz = SLEN(MAX_FRAME_SIZE_KEY); |
| 403 | if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1) |
| 404 | goto too_big; |
| 405 | |
| 406 | *p++ = SPOE_DATA_T_UINT32; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 407 | if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 408 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 409 | |
| 410 | /* "capabilities" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 411 | sz = SLEN(CAPABILITIES_KEY); |
| 412 | if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1) |
| 413 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 414 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 415 | *p++ = SPOE_DATA_T_STR; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 416 | chk = get_trash_chunk(); |
| 417 | if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 418 | memcpy(chk->area, "pipelining", 10); |
| 419 | chk->data += 10; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 420 | } |
| 421 | if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 422 | if (chk->data) chk->area[chk->data++] = ','; |
| 423 | memcpy(chk->area+chk->data, "async", 5); |
| 424 | chk->data += 5; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 425 | } |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 426 | if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 427 | if (chk->data) chk->area[chk->data++] = ','; |
| 428 | memcpy(chk->area+chk->data, "fragmentation", 13); |
Miroslav Zagorac | 6b3690b | 2019-01-13 16:55:01 +0100 | [diff] [blame] | 429 | chk->data += 13; |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 430 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 431 | if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 432 | goto too_big; |
| 433 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 434 | /* (optional) "engine-id" K/V item, if present */ |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 435 | if (agent != NULL && agent->rt[tid].engine_id != NULL) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 436 | sz = SLEN(ENGINE_ID_KEY); |
| 437 | if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1) |
| 438 | goto too_big; |
| 439 | |
| 440 | *p++ = SPOE_DATA_T_STR; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 441 | sz = strlen(agent->rt[tid].engine_id); |
| 442 | 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] | 443 | goto too_big; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 444 | } |
| 445 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 446 | return (p - frame); |
| 447 | |
| 448 | too_big: |
| 449 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 450 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 451 | } |
| 452 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 453 | /* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of |
| 454 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
| 455 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 456 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 457 | spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 458 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 459 | const char *reason; |
| 460 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 461 | unsigned int flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 462 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 463 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 464 | p = frame; |
| 465 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 466 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 467 | /* Set Frame type */ |
| 468 | *p++ = SPOE_FRM_T_HAPROXY_DISCON; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 469 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 470 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 471 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 472 | memcpy(p, (char *)&flags, 4); |
| 473 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 474 | |
| 475 | /* No stream-id and frame-id for DISCONNECT frames */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 476 | *p++ = 0; *p++ = 0; |
| 477 | |
| 478 | if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS) |
| 479 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 480 | |
| 481 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 482 | |
| 483 | /* "status-code" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 484 | sz = SLEN(STATUS_CODE_KEY); |
| 485 | if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1) |
| 486 | goto too_big; |
| 487 | |
| 488 | *p++ = SPOE_DATA_T_UINT32; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 489 | if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 490 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 491 | |
| 492 | /* "message" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 493 | sz = SLEN(MSG_KEY); |
| 494 | if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1) |
| 495 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 496 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 497 | /*Get the message corresponding to the status code */ |
| 498 | reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]; |
| 499 | |
| 500 | *p++ = SPOE_DATA_T_STR; |
| 501 | sz = strlen(reason); |
| 502 | if (spoe_encode_buffer(reason, sz, &p, end) == -1) |
| 503 | goto too_big; |
| 504 | |
| 505 | return (p - frame); |
| 506 | |
| 507 | too_big: |
| 508 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 509 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 510 | } |
| 511 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 512 | /* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of |
| 513 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
| 514 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 515 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 516 | spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 517 | char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 518 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 519 | char *p, *end; |
| 520 | unsigned int stream_id, frame_id; |
| 521 | unsigned int flags = SPOE_FRM_FL_FIN; |
| 522 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 523 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 524 | p = frame; |
| 525 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 526 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 527 | stream_id = ctx->stream_id; |
| 528 | frame_id = ctx->frame_id; |
| 529 | |
| 530 | if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) { |
| 531 | /* The fragmentation is not supported by the applet */ |
| 532 | if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) { |
| 533 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
| 534 | return -1; |
| 535 | } |
| 536 | flags = ctx->frag_ctx.flags; |
| 537 | } |
| 538 | |
| 539 | /* Set Frame type */ |
| 540 | *p++ = SPOE_FRM_T_HAPROXY_NOTIFY; |
| 541 | |
| 542 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 543 | flags = htonl(flags); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 544 | memcpy(p, (char *)&flags, 4); |
| 545 | p += 4; |
| 546 | |
| 547 | /* Set stream-id and frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 548 | if (encode_varint(stream_id, &p, end) == -1) |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 549 | goto too_big; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 550 | if (encode_varint(frame_id, &p, end) == -1) |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 551 | goto too_big; |
| 552 | |
| 553 | /* Copy encoded messages, if possible */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 554 | sz = b_data(&ctx->buffer); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 555 | if (p + sz >= end) |
| 556 | goto too_big; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 557 | memcpy(p, b_head(&ctx->buffer), sz); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 558 | p += sz; |
| 559 | |
| 560 | return (p - frame); |
| 561 | |
| 562 | too_big: |
| 563 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | /* Encode next part of a fragmented frame sent by HAProxy to an agent. It |
| 568 | * returns the number of encoded bytes in the frame on success, 0 if an encoding |
| 569 | * error occurred and -1 if a fatal error occurred. */ |
| 570 | static int |
| 571 | spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx, |
| 572 | char *frame, size_t size) |
| 573 | { |
| 574 | char *p, *end; |
| 575 | unsigned int stream_id, frame_id; |
| 576 | unsigned int flags; |
| 577 | size_t sz; |
| 578 | |
| 579 | p = frame; |
| 580 | end = frame+size; |
| 581 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 582 | /* <ctx> is null when the stream has aborted the processing of a |
| 583 | * fragmented frame. In this case, we must notify the corresponding |
| 584 | * agent using ids stored in <frag_ctx>. */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 585 | if (ctx == NULL) { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 586 | flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 587 | stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid; |
| 588 | frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid; |
| 589 | } |
| 590 | else { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 591 | flags = ctx->frag_ctx.flags; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 592 | stream_id = ctx->stream_id; |
| 593 | frame_id = ctx->frame_id; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 594 | } |
| 595 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 596 | /* Set Frame type */ |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 597 | *p++ = SPOE_FRM_T_UNSET; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 598 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 599 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 600 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 601 | memcpy(p, (char *)&flags, 4); |
| 602 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 603 | |
| 604 | /* Set stream-id and frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 605 | if (encode_varint(stream_id, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 606 | goto too_big; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 607 | if (encode_varint(frame_id, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 608 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 609 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 610 | if (ctx == NULL) |
| 611 | goto end; |
| 612 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 613 | /* Copy encoded messages, if possible */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 614 | sz = b_data(&ctx->buffer); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 615 | if (p + sz >= end) |
| 616 | goto too_big; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 617 | memcpy(p, b_head(&ctx->buffer), sz); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 618 | p += sz; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 619 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 620 | end: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 621 | return (p - frame); |
| 622 | |
| 623 | too_big: |
| 624 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 625 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 626 | } |
| 627 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 628 | /* Decode and process the HELLO frame sent by an agent. It returns the number of |
| 629 | * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal |
| 630 | * error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 631 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 632 | spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 633 | { |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 634 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
| 635 | char *p, *end; |
| 636 | int vsn, max_frame_size; |
| 637 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 638 | |
| 639 | p = frame; |
| 640 | end = frame + size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 641 | |
| 642 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 643 | if (*p++ != SPOE_FRM_T_AGENT_HELLO) { |
| 644 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 645 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 646 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 647 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 648 | if (size < 7 /* TYPE + METADATA */) { |
| 649 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 650 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 651 | } |
| 652 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 653 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 654 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 655 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 656 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 657 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 658 | /* Fragmentation is not supported for HELLO frame */ |
| 659 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 660 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 661 | return -1; |
| 662 | } |
| 663 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 664 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 665 | if (*p != 0 || *(p+1) != 0) { |
| 666 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 667 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 668 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 669 | p += 2; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 670 | |
| 671 | /* There are 3 mandatory items: "version", "max-frame-size" and |
| 672 | * "capabilities" */ |
| 673 | |
| 674 | /* Loop on K/V items */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 675 | vsn = max_frame_size = flags = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 676 | while (p < end) { |
| 677 | char *str; |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 678 | uint64_t sz; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 679 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 680 | |
| 681 | /* Decode the item key */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 682 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 683 | if (ret == -1 || !sz) { |
| 684 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 685 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 686 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 687 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 688 | /* Check "version" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 689 | if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 690 | int i, type = *p++; |
| 691 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 692 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 693 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 694 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 695 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 696 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 697 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) { |
| 698 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 699 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 700 | } |
| 701 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 702 | vsn = spoe_str_to_vsn(str, sz); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 703 | if (vsn == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 704 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 705 | return -1; |
| 706 | } |
| 707 | for (i = 0; supported_versions[i].str != NULL; ++i) { |
| 708 | if (vsn >= supported_versions[i].min && |
| 709 | vsn <= supported_versions[i].max) |
| 710 | break; |
| 711 | } |
| 712 | if (supported_versions[i].str == NULL) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 713 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 714 | return -1; |
| 715 | } |
| 716 | } |
| 717 | /* Check "max-frame-size" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 718 | 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] | 719 | int type = *p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 720 | |
| 721 | /* The value must be integer */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 722 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 723 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 724 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 725 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 726 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 727 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 728 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 729 | if (decode_varint(&p, end, &sz) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 730 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 731 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 732 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 733 | if (sz < MIN_FRAME_SIZE || |
| 734 | sz > SPOE_APPCTX(appctx)->max_frame_size) { |
| 735 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 736 | return -1; |
| 737 | } |
| 738 | max_frame_size = sz; |
| 739 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 740 | /* Check "capabilities" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 741 | 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] | 742 | int type = *p++; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 743 | |
| 744 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 745 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 746 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 747 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 748 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 749 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) { |
| 750 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 751 | return 0; |
| 752 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 753 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 754 | while (sz) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 755 | char *delim; |
| 756 | |
| 757 | /* Skip leading spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 758 | for (; isspace((unsigned char)*str) && sz; str++, sz--); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 759 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 760 | if (sz >= 10 && !strncmp(str, "pipelining", 10)) { |
| 761 | str += 10; sz -= 10; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 762 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 763 | flags |= SPOE_APPCTX_FL_PIPELINING; |
| 764 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 765 | else if (sz >= 5 && !strncmp(str, "async", 5)) { |
| 766 | str += 5; sz -= 5; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 767 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 768 | flags |= SPOE_APPCTX_FL_ASYNC; |
| 769 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 770 | else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) { |
| 771 | str += 13; sz -= 13; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 772 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 773 | flags |= SPOE_APPCTX_FL_FRAGMENTATION; |
| 774 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 775 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 776 | /* Get the next comma or break */ |
| 777 | if (!sz || (delim = memchr(str, ',', sz)) == NULL) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 778 | break; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 779 | delim++; |
| 780 | sz -= (delim - str); |
| 781 | str = delim; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 782 | } |
| 783 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 784 | else { |
| 785 | /* Silently ignore unknown item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 786 | if (spoe_skip_data(&p, end) == -1) { |
| 787 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 788 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 789 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | |
| 793 | /* Final checks */ |
| 794 | if (!vsn) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 795 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 796 | return -1; |
| 797 | } |
| 798 | if (!max_frame_size) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 799 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 800 | return -1; |
| 801 | } |
Christopher Faulet | 1138901 | 2019-02-07 16:13:26 +0100 | [diff] [blame] | 802 | if (!agent) |
| 803 | flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC); |
| 804 | else { |
| 805 | if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING)) |
| 806 | flags &= ~SPOE_APPCTX_FL_PIPELINING; |
| 807 | if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC)) |
| 808 | flags &= ~SPOE_APPCTX_FL_ASYNC; |
| 809 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 810 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 811 | SPOE_APPCTX(appctx)->version = (unsigned int)vsn; |
| 812 | SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size; |
| 813 | SPOE_APPCTX(appctx)->flags |= flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 814 | |
| 815 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | /* Decode DISCONNECT frame sent by an agent. It returns the number of by read |
| 819 | * bytes on success, 0 if the frame can be ignored and -1 if an error |
| 820 | * occurred. */ |
| 821 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 822 | spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 823 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 824 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 825 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 826 | |
| 827 | p = frame; |
| 828 | end = frame + size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 829 | |
| 830 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 831 | if (*p++ != SPOE_FRM_T_AGENT_DISCON) { |
| 832 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 833 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 834 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 835 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 836 | if (size < 7 /* TYPE + METADATA */) { |
| 837 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 838 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 839 | } |
| 840 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 841 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 842 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 843 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 844 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 845 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 846 | /* Fragmentation is not supported for DISCONNECT frame */ |
| 847 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 848 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 849 | return -1; |
| 850 | } |
| 851 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 852 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 853 | if (*p != 0 || *(p+1) != 0) { |
| 854 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 855 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 856 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 857 | p += 2; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 858 | |
| 859 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 860 | |
| 861 | /* Loop on K/V items */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 862 | while (p < end) { |
| 863 | char *str; |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 864 | uint64_t sz; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 865 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 866 | |
| 867 | /* Decode the item key */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 868 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 869 | if (ret == -1 || !sz) { |
| 870 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 871 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | /* Check "status-code" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 875 | 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] | 876 | int type = *p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 877 | |
| 878 | /* The value must be an integer */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 879 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 880 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 881 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 882 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 883 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 884 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 885 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 886 | if (decode_varint(&p, end, &sz) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 887 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 888 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 889 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 890 | SPOE_APPCTX(appctx)->status_code = sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | /* Check "message" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 894 | 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] | 895 | int type = *p++; |
| 896 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 897 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 898 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 899 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 900 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 901 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 902 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 903 | if (ret == -1 || sz > 255) { |
| 904 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 905 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 906 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 907 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 908 | SPOE_APPCTX(appctx)->reason = str; |
| 909 | SPOE_APPCTX(appctx)->rlen = sz; |
| 910 | #endif |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 911 | } |
| 912 | else { |
| 913 | /* Silently ignore unknown item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 914 | if (spoe_skip_data(&p, end) == -1) { |
| 915 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 916 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 917 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 921 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 925 | /* 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] | 926 | * success, 0 if the frame can be ignored and -1 if an error occurred. */ |
| 927 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 928 | spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 929 | char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 930 | { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 931 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 932 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 933 | uint64_t stream_id, frame_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 934 | int len; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 935 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 936 | |
| 937 | p = frame; |
| 938 | end = frame + size; |
| 939 | *ctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 940 | |
| 941 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 942 | if (*p++ != SPOE_FRM_T_AGENT_ACK) { |
| 943 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 944 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 945 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 946 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 947 | if (size < 7 /* TYPE + METADATA */) { |
| 948 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 949 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 950 | } |
| 951 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 952 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 953 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 954 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 955 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 956 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 957 | /* Fragmentation is not supported for now */ |
| 958 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 959 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 960 | return -1; |
| 961 | } |
| 962 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 963 | /* Get the stream-id and the frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 964 | if (decode_varint(&p, end, &stream_id) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 965 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 966 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 967 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 968 | if (decode_varint(&p, end, &frame_id) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 969 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 970 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 971 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 972 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 973 | /* Try to find the corresponding SPOE context */ |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 974 | if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 975 | list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 976 | if ((*ctx)->stream_id == (unsigned int)stream_id && |
| 977 | (*ctx)->frame_id == (unsigned int)frame_id) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 978 | goto found; |
| 979 | } |
| 980 | } |
| 981 | else { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 982 | list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) { |
| 983 | if ((*ctx)->stream_id == (unsigned int)stream_id && |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 984 | (*ctx)->frame_id == (unsigned int)frame_id) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 985 | goto found; |
| 986 | } |
| 987 | } |
| 988 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 989 | if (SPOE_APPCTX(appctx)->frag_ctx.ctx && |
| 990 | SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id && |
| 991 | SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) { |
| 992 | |
| 993 | /* ABRT bit is set for an unfinished fragmented frame */ |
| 994 | if (flags & SPOE_FRM_FL_ABRT) { |
| 995 | *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx; |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 996 | (*ctx)->state = SPOE_CTX_ST_ERROR; |
| 997 | (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT; |
| 998 | /* Ignore the payload */ |
| 999 | goto end; |
| 1000 | } |
| 1001 | /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */ |
| 1002 | /* For now, we ignore the ack */ |
| 1003 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 1004 | return 0; |
| 1005 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1006 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1007 | /* No Stream found, ignore the frame */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1008 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1009 | " - Ignore ACK frame" |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1010 | " - stream-id=%u - frame-id=%u\n", |
| 1011 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1012 | __FUNCTION__, appctx, |
| 1013 | (unsigned int)stream_id, (unsigned int)frame_id); |
| 1014 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1015 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND; |
Christopher Faulet | c7ba910 | 2020-11-10 14:31:39 +0100 | [diff] [blame] | 1016 | if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) { |
| 1017 | /* Report an error if we are waiting the ack for another frame, |
| 1018 | * but not if there is no longer frame waiting for a ack |
| 1019 | * (timeout) |
| 1020 | */ |
| 1021 | if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) || |
| 1022 | SPOE_APPCTX(appctx)->frag_ctx.ctx) |
| 1023 | return -1; |
| 1024 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1025 | SPOE_APPCTX(appctx)->cur_fpa = 0; |
| 1026 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1027 | return 0; |
| 1028 | |
| 1029 | found: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1030 | if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer, |
| 1031 | &SPOE_APPCTX(appctx)->buffer_wait)) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1032 | *ctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1033 | return 1; /* Retry later */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1034 | } |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1035 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1036 | /* Copy encoded actions */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1037 | len = (end - p); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1038 | memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len); |
| 1039 | b_set_data(&SPOE_APPCTX(appctx)->buffer, len); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1040 | p += len; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1041 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1042 | /* Transfer the buffer ownership to the SPOE context */ |
| 1043 | (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1044 | SPOE_APPCTX(appctx)->buffer = BUF_NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1045 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1046 | (*ctx)->state = SPOE_CTX_ST_DONE; |
| 1047 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1048 | end: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1049 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1050 | " - ACK frame received" |
| 1051 | " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1052 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1053 | __FUNCTION__, appctx, *ctx, (*ctx)->stream_id, |
| 1054 | (*ctx)->frame_id, flags); |
| 1055 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1056 | } |
| 1057 | |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1058 | /* This function is used in cfgparse.c and declared in proto/checks.h. It |
| 1059 | * prepare the request to send to agents during a healthcheck. It returns 0 on |
| 1060 | * success and -1 if an error occurred. */ |
| 1061 | int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1062 | spoe_prepare_healthcheck_request(char **req, int *len) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1063 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1064 | struct appctx appctx; |
| 1065 | struct spoe_appctx spoe_appctx; |
| 1066 | char *frame, *end, buf[MAX_FRAME_SIZE+4]; |
| 1067 | size_t sz; |
| 1068 | int ret; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1069 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1070 | memset(&appctx, 0, sizeof(appctx)); |
| 1071 | memset(&spoe_appctx, 0, sizeof(spoe_appctx)); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1072 | memset(buf, 0, sizeof(buf)); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1073 | |
| 1074 | appctx.ctx.spoe.ptr = &spoe_appctx; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1075 | SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1076 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1077 | frame = buf+4; /* Reserved the 4 first bytes for the frame size */ |
| 1078 | end = frame + MAX_FRAME_SIZE; |
| 1079 | |
| 1080 | ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE); |
| 1081 | if (ret <= 0) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1082 | return -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1083 | frame += ret; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1084 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1085 | /* Add "healthcheck" K/V item */ |
| 1086 | sz = SLEN(HEALTHCHECK_KEY); |
| 1087 | if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1) |
| 1088 | return -1; |
| 1089 | *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1090 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1091 | *len = frame - buf; |
| 1092 | sz = htonl(*len - 4); |
| 1093 | memcpy(buf, (char *)&sz, 4); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1094 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1095 | if ((*req = malloc(*len)) == NULL) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1096 | return -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1097 | memcpy(*req, buf, *len); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1098 | return 0; |
| 1099 | } |
| 1100 | |
| 1101 | /* This function is used in checks.c and declared in proto/checks.h. It decode |
| 1102 | * the response received from an agent during a healthcheck. It returns 0 on |
| 1103 | * success and -1 if an error occurred. */ |
| 1104 | int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1105 | 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] | 1106 | { |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1107 | struct appctx appctx; |
| 1108 | struct spoe_appctx spoe_appctx; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1109 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1110 | memset(&appctx, 0, sizeof(appctx)); |
| 1111 | memset(&spoe_appctx, 0, sizeof(spoe_appctx)); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1112 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1113 | appctx.ctx.spoe.ptr = &spoe_appctx; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1114 | SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1115 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1116 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1117 | spoe_handle_agentdiscon_frame(&appctx, frame, size); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1118 | goto error; |
| 1119 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1120 | if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0) |
| 1121 | goto error; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1122 | |
| 1123 | return 0; |
| 1124 | |
| 1125 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1126 | if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS) |
| 1127 | SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN; |
| 1128 | strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1129 | return -1; |
| 1130 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1131 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1132 | /* 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] | 1133 | * 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] | 1134 | * success. */ |
| 1135 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1136 | spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1137 | { |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 1138 | struct stream_interface *si = cs_si(appctx->owner); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1139 | int ret; |
| 1140 | uint32_t netint; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1141 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1142 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1143 | * length. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1144 | netint = htonl(framesz); |
| 1145 | memcpy(buf, (char *)&netint, 4); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1146 | ret = ci_putblk(si_ic(si), buf, framesz+4); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1147 | if (ret <= 0) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1148 | if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1149 | si_rx_room_blk(si); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1150 | return 1; /* retry */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1151 | } |
| 1152 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1153 | return -1; /* error */ |
| 1154 | } |
| 1155 | return framesz; |
| 1156 | } |
| 1157 | |
| 1158 | /* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0 |
| 1159 | * when the frame can be ignored, 1 to retry later and the frame length on |
| 1160 | * success. */ |
| 1161 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1162 | spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1163 | { |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 1164 | struct stream_interface *si = cs_si(appctx->owner); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1165 | int ret; |
| 1166 | uint32_t netint; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1167 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1168 | ret = co_getblk(si_oc(si), (char *)&netint, 4, 0); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1169 | if (ret > 0) { |
| 1170 | framesz = ntohl(netint); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1171 | if (framesz > SPOE_APPCTX(appctx)->max_frame_size) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1172 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1173 | return -1; |
| 1174 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1175 | ret = co_getblk(si_oc(si), buf, framesz, 4); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1176 | } |
| 1177 | if (ret <= 0) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1178 | if (ret == 0) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1179 | return 1; /* retry */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1180 | } |
| 1181 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1182 | return -1; /* error */ |
| 1183 | } |
| 1184 | return framesz; |
| 1185 | } |
| 1186 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1187 | /******************************************************************** |
| 1188 | * Functions that manage the SPOE applet |
| 1189 | ********************************************************************/ |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1190 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1191 | spoe_wakeup_appctx(struct appctx *appctx) |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1192 | { |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 1193 | si_want_get(cs_si(appctx->owner)); |
| 1194 | si_rx_endp_more(cs_si(appctx->owner)); |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1195 | appctx_wakeup(appctx); |
| 1196 | return 1; |
| 1197 | } |
| 1198 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1199 | /* Callback function that catches applet timeouts. If a timeout occurred, we set |
| 1200 | * <appctx->st1> flag and the SPOE applet is woken up. */ |
| 1201 | static struct task * |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1202 | spoe_process_appctx(struct task * task, void *context, unsigned int state) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1203 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 1204 | struct appctx *appctx = context; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1205 | |
| 1206 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1207 | if (tick_is_expired(task->expire, now_ms)) { |
| 1208 | task->expire = TICK_ETERNITY; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1209 | appctx->st1 = SPOE_APPCTX_ERR_TOUT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1210 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1211 | spoe_wakeup_appctx(appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1212 | return task; |
| 1213 | } |
| 1214 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1215 | /* Callback function that releases a SPOE applet. This happens when the |
| 1216 | * connection with the agent is closed. */ |
| 1217 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1218 | spoe_release_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1219 | { |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 1220 | struct stream_interface *si = cs_si(appctx->owner); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1221 | struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx); |
| 1222 | struct spoe_agent *agent; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1223 | struct spoe_context *ctx, *back; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1224 | |
| 1225 | if (spoe_appctx == NULL) |
| 1226 | return; |
| 1227 | |
| 1228 | appctx->ctx.spoe.ptr = NULL; |
| 1229 | agent = spoe_appctx->agent; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1230 | |
| 1231 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n", |
| 1232 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1233 | __FUNCTION__, appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1234 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1235 | /* Remove applet from the list of running applets */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1236 | _HA_ATOMIC_DEC(&agent->counters.applets); |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1237 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1238 | if (!LIST_ISEMPTY(&spoe_appctx->list)) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1239 | LIST_DELETE(&spoe_appctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1240 | LIST_INIT(&spoe_appctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1241 | } |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1242 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1243 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1244 | /* Shutdown the server connection, if needed */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1245 | if (appctx->st0 != SPOE_APPCTX_ST_END) { |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1246 | if (appctx->st0 == SPOE_APPCTX_ST_IDLE) { |
| 1247 | eb32_delete(&spoe_appctx->node); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1248 | _HA_ATOMIC_DEC(&agent->counters.idles); |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1249 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1250 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1251 | appctx->st0 = SPOE_APPCTX_ST_END; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1252 | if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE) |
| 1253 | spoe_appctx->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1254 | |
| 1255 | si_shutw(si); |
| 1256 | si_shutr(si); |
| 1257 | si_ic(si)->flags |= CF_READ_NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1258 | } |
| 1259 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1260 | /* Destroy the task attached to this applet */ |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 1261 | task_destroy(spoe_appctx->task); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1262 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1263 | /* Notify all waiting streams */ |
| 1264 | list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1265 | LIST_DELETE(&ctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1266 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1267 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1268 | spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting); |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1269 | ctx->spoe_appctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1270 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1271 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1272 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1273 | } |
| 1274 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1275 | /* If the applet was processing a fragmented frame, notify the |
| 1276 | * corresponding stream. */ |
| 1277 | if (spoe_appctx->frag_ctx.ctx) { |
| 1278 | ctx = spoe_appctx->frag_ctx.ctx; |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1279 | ctx->spoe_appctx = NULL; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1280 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1281 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1282 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1283 | } |
| 1284 | |
Willy Tarreau | b042e4f | 2022-02-15 16:49:37 +0100 | [diff] [blame] | 1285 | if (!LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) { |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1286 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) { |
| 1287 | if (ctx->spoe_appctx == spoe_appctx) |
| 1288 | ctx->spoe_appctx = NULL; |
| 1289 | } |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1290 | goto end; |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1291 | } |
Christopher Faulet | 6f1296b | 2021-08-02 17:53:56 +0200 | [diff] [blame] | 1292 | else { |
| 1293 | /* It is the last running applet and the sending and waiting |
| 1294 | * queues are not empty. Try to start a new one if HAproxy is |
| 1295 | * not stopping. |
| 1296 | */ |
| 1297 | if (!stopping && |
| 1298 | (!LIST_ISEMPTY(&agent->rt[tid].sending_queue) || !LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) && |
| 1299 | spoe_create_appctx(agent->spoe_conf)) |
| 1300 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1301 | |
Christopher Faulet | 6f1296b | 2021-08-02 17:53:56 +0200 | [diff] [blame] | 1302 | /* otherwise, notify all waiting streams */ |
| 1303 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) { |
| 1304 | LIST_DELETE(&ctx->list); |
| 1305 | LIST_INIT(&ctx->list); |
| 1306 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
| 1307 | spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue); |
| 1308 | ctx->spoe_appctx = NULL; |
| 1309 | ctx->state = SPOE_CTX_ST_ERROR; |
| 1310 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
| 1311 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1312 | } |
| 1313 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) { |
| 1314 | LIST_DELETE(&ctx->list); |
| 1315 | LIST_INIT(&ctx->list); |
| 1316 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
| 1317 | spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting); |
| 1318 | ctx->spoe_appctx = NULL; |
| 1319 | ctx->state = SPOE_CTX_ST_ERROR; |
| 1320 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
| 1321 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1322 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1323 | } |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1324 | |
| 1325 | end: |
Christopher Faulet | 55ae8a6 | 2019-05-23 22:47:48 +0200 | [diff] [blame] | 1326 | /* Release allocated memory */ |
| 1327 | spoe_release_buffer(&spoe_appctx->buffer, |
| 1328 | &spoe_appctx->buffer_wait); |
| 1329 | pool_free(pool_head_spoe_appctx, spoe_appctx); |
| 1330 | |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1331 | /* Update runtinme agent info */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1332 | agent->rt[tid].frame_size = agent->max_frame_size; |
| 1333 | list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list) |
| 1334 | 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] | 1335 | } |
| 1336 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1337 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1338 | spoe_handle_connect_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1339 | { |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 1340 | struct stream_interface *si = cs_si(appctx->owner); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1341 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1342 | char *frame, *buf; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1343 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1344 | |
Willy Tarreau | 7ab22adb | 2019-06-05 14:53:22 +0200 | [diff] [blame] | 1345 | if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) { |
| 1346 | /* closed */ |
| 1347 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
| 1348 | goto exit; |
| 1349 | } |
| 1350 | |
Willy Tarreau | 4f283fa | 2019-06-05 14:34:03 +0200 | [diff] [blame] | 1351 | if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) { |
Willy Tarreau | 7ab22adb | 2019-06-05 14:53:22 +0200 | [diff] [blame] | 1352 | /* not connected yet */ |
Willy Tarreau | 8bb2ffb | 2018-11-14 17:54:13 +0100 | [diff] [blame] | 1353 | si_rx_endp_more(si); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1354 | task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG); |
| 1355 | goto stop; |
| 1356 | } |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1357 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1358 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1359 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1360 | " - Connection timed out\n", |
| 1361 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1362 | __FUNCTION__, appctx); |
| 1363 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1364 | goto exit; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1365 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1366 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1367 | if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1368 | SPOE_APPCTX(appctx)->task->expire = |
| 1369 | tick_add_ifset(now_ms, agent->timeout.hello); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1370 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1371 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1372 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1373 | buf = trash.area; frame = buf+4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1374 | ret = spoe_prepare_hahello_frame(appctx, frame, |
| 1375 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1376 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1377 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1378 | |
| 1379 | switch (ret) { |
| 1380 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1381 | case 0: /* ignore => an error, cannot be ignored */ |
| 1382 | goto exit; |
| 1383 | |
| 1384 | case 1: /* retry later */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1385 | goto stop; |
| 1386 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1387 | default: |
| 1388 | /* HELLO frame successfully sent, now wait for the |
| 1389 | * reply. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1390 | appctx->st0 = SPOE_APPCTX_ST_CONNECTING; |
| 1391 | goto next; |
| 1392 | } |
| 1393 | |
| 1394 | next: |
| 1395 | return 0; |
| 1396 | stop: |
| 1397 | return 1; |
| 1398 | exit: |
| 1399 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1400 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1401 | } |
| 1402 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1403 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1404 | spoe_handle_connecting_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1405 | { |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 1406 | struct stream_interface *si = cs_si(appctx->owner); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1407 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1408 | char *frame; |
| 1409 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1410 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1411 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1412 | 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] | 1413 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1414 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1415 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1416 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1417 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1418 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1419 | " - Connection timed out\n", |
| 1420 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1421 | __FUNCTION__, appctx); |
| 1422 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1423 | goto exit; |
| 1424 | } |
| 1425 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1426 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1427 | ret = spoe_recv_frame(appctx, frame, |
| 1428 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1429 | if (ret > 1) { |
| 1430 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1431 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1432 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1433 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1434 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1435 | ret = spoe_handle_agenthello_frame(appctx, frame, ret); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1436 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1437 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1438 | switch (ret) { |
| 1439 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1440 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1441 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1442 | goto next; |
| 1443 | |
| 1444 | case 1: /* retry later */ |
| 1445 | goto stop; |
| 1446 | |
| 1447 | default: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1448 | _HA_ATOMIC_INC(&agent->counters.idles); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1449 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1450 | SPOE_APPCTX(appctx)->node.key = 0; |
| 1451 | eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node); |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1452 | |
| 1453 | /* Update runtinme agent info */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1454 | 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] | 1455 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1456 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1457 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1458 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1459 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1460 | if (trash.data) |
| 1461 | co_skip(si_oc(si), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1462 | |
| 1463 | SPOE_APPCTX(appctx)->task->expire = |
| 1464 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1465 | return 0; |
| 1466 | stop: |
| 1467 | return 1; |
| 1468 | exit: |
| 1469 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1470 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1471 | } |
| 1472 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1473 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1474 | static int |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1475 | spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1476 | { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1477 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
| 1478 | struct spoe_context *ctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1479 | char *frame, *buf; |
| 1480 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1481 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1482 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1483 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1484 | buf = trash.area; frame = buf+4; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1485 | |
| 1486 | if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) { |
| 1487 | ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx; |
| 1488 | ret = spoe_prepare_hafrag_frame(appctx, ctx, frame, |
| 1489 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1490 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1491 | else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1492 | *skip = 1; |
| 1493 | ret = 1; |
| 1494 | goto end; |
| 1495 | } |
| 1496 | else { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1497 | ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1498 | ret = spoe_prepare_hanotify_frame(appctx, ctx, frame, |
| 1499 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1500 | |
| 1501 | } |
| 1502 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1503 | if (ret > 1) |
| 1504 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1505 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1506 | switch (ret) { |
| 1507 | case -1: /* error */ |
| 1508 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1509 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1510 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1511 | case 0: /* ignore */ |
| 1512 | if (ctx == NULL) |
| 1513 | goto abort_frag_frame; |
| 1514 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1515 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1516 | LIST_DELETE(&ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1517 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1518 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1519 | spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue); |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1520 | ctx->spoe_appctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1521 | ctx->state = SPOE_CTX_ST_ERROR; |
| 1522 | ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100); |
| 1523 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1524 | *skip = 1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1525 | break; |
| 1526 | |
| 1527 | case 1: /* retry */ |
| 1528 | *skip = 1; |
| 1529 | break; |
| 1530 | |
| 1531 | default: |
| 1532 | if (ctx == NULL) |
| 1533 | goto abort_frag_frame; |
| 1534 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1535 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1536 | LIST_DELETE(&ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1537 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1538 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1539 | spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue); |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1540 | ctx->spoe_appctx = SPOE_APPCTX(appctx); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1541 | if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) || |
| 1542 | (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN)) |
| 1543 | goto no_frag_frame_sent; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1544 | else |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1545 | goto frag_frame_sent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1546 | } |
| 1547 | goto end; |
| 1548 | |
| 1549 | frag_frame_sent: |
| 1550 | appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1551 | *skip = 1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1552 | SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx; |
| 1553 | SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id; |
| 1554 | SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1555 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
| 1556 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1557 | goto end; |
| 1558 | |
| 1559 | no_frag_frame_sent: |
| 1560 | if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) { |
| 1561 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1562 | LIST_APPEND(&agent->rt[tid].waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1563 | } |
| 1564 | else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) { |
| 1565 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1566 | LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1567 | } |
| 1568 | else { |
| 1569 | appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1570 | *skip = 1; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1571 | LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1572 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1573 | _HA_ATOMIC_INC(&agent->counters.nb_waiting); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1574 | ctx->stats.tv_wait = now; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1575 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1576 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1577 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1578 | SPOE_APPCTX(appctx)->cur_fpa++; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1579 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1580 | ctx->state = SPOE_CTX_ST_WAITING_ACK; |
| 1581 | goto end; |
| 1582 | |
| 1583 | abort_frag_frame: |
| 1584 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1585 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1586 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1587 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1588 | goto end; |
| 1589 | |
| 1590 | end: |
| 1591 | return ret; |
| 1592 | } |
| 1593 | |
| 1594 | static int |
| 1595 | spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip) |
| 1596 | { |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 1597 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1598 | struct spoe_context *ctx = NULL; |
| 1599 | char *frame; |
| 1600 | int ret; |
| 1601 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1602 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1603 | ret = spoe_recv_frame(appctx, frame, |
| 1604 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1605 | if (ret > 1) { |
| 1606 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1607 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1608 | ret = -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1609 | goto end; |
| 1610 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1611 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1612 | ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret); |
| 1613 | } |
| 1614 | switch (ret) { |
| 1615 | case -1: /* error */ |
| 1616 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1617 | break; |
| 1618 | |
| 1619 | case 0: /* ignore */ |
| 1620 | break; |
| 1621 | |
| 1622 | case 1: /* retry */ |
| 1623 | *skip = 1; |
| 1624 | break; |
| 1625 | |
| 1626 | default: |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1627 | LIST_DELETE(&ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1628 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1629 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1630 | spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting); |
| 1631 | ctx->stats.tv_response = now; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1632 | if (ctx->spoe_appctx) { |
| 1633 | ctx->spoe_appctx->cur_fpa--; |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1634 | ctx->spoe_appctx = NULL; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1635 | } |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1636 | if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY && |
| 1637 | ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) { |
| 1638 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1639 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1640 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1641 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1642 | } |
| 1643 | else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) |
| 1644 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1645 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1646 | break; |
| 1647 | } |
| 1648 | |
| 1649 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1650 | if (trash.data) |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 1651 | co_skip(si_oc(cs_si(appctx->owner)), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1652 | end: |
| 1653 | return ret; |
| 1654 | } |
| 1655 | |
| 1656 | static int |
| 1657 | spoe_handle_processing_appctx(struct appctx *appctx) |
| 1658 | { |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 1659 | struct stream_interface *si = cs_si(appctx->owner); |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1660 | struct server *srv = objt_server(si_strm(si)->target); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1661 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1662 | 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] | 1663 | |
| 1664 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) { |
| 1665 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
| 1666 | goto exit; |
| 1667 | } |
| 1668 | |
| 1669 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
| 1670 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
| 1671 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1672 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1673 | goto next; |
| 1674 | } |
| 1675 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1676 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1677 | " - 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] | 1678 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1679 | __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa, |
| 1680 | agent->max_fpa, spoe_appctx_state_str[appctx->st0], |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1681 | SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1682 | |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1683 | |
| 1684 | /* Close the applet ASAP because some sessions are waiting for a free |
| 1685 | * connection slot. It is only an issue in multithreaded mode. |
| 1686 | */ |
| 1687 | close_asap = (global.nbthread > 1 && |
| 1688 | (agent->b.be->queue.length || |
| 1689 | (srv && (srv->queue.length || (srv->maxconn && srv->served >= srv_dynamic_maxconn(srv)))))); |
| 1690 | |
| 1691 | /* Don"t try to send new frame we are waiting for at lease a ack, in |
| 1692 | * sync mode or if applet must be closed ASAP |
| 1693 | */ |
| 1694 | if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK || (close_asap && SPOE_APPCTX(appctx)->cur_fpa)) |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1695 | skip_sending = 1; |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1696 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1697 | /* receiving_frame loop */ |
| 1698 | while (!skip_receiving) { |
| 1699 | ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving); |
| 1700 | switch (ret) { |
| 1701 | case -1: /* error */ |
| 1702 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1703 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1704 | case 0: /* ignore */ |
| 1705 | active_r = 1; |
| 1706 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1707 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1708 | case 1: /* retry */ |
| 1709 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1710 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1711 | default: |
| 1712 | active_r = 1; |
| 1713 | break; |
| 1714 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1715 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1716 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1717 | /* send_frame loop */ |
| 1718 | while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) { |
| 1719 | ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending); |
| 1720 | switch (ret) { |
| 1721 | case -1: /* error */ |
| 1722 | goto next; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1723 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1724 | case 0: /* ignore */ |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1725 | if (SPOE_APPCTX(appctx)->node.key) |
| 1726 | SPOE_APPCTX(appctx)->node.key--; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1727 | active_s++; |
| 1728 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1729 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1730 | case 1: /* retry */ |
| 1731 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1732 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1733 | default: |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1734 | if (SPOE_APPCTX(appctx)->node.key) |
| 1735 | SPOE_APPCTX(appctx)->node.key--; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1736 | active_s++; |
| 1737 | break; |
| 1738 | } |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1739 | |
| 1740 | /* if applet must be close ASAP, don't send more than a frame */ |
| 1741 | if (close_asap) |
| 1742 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1743 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1744 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1745 | if (active_s || active_r) { |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1746 | update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s); |
| 1747 | SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle); |
| 1748 | } |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1749 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1750 | if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) { |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1751 | /* If applet must be closed, don't switch it in IDLE state and |
| 1752 | * close it when the last waiting frame is acknowledged. |
Christopher Faulet | 9e647e5 | 2021-03-01 15:01:14 +0100 | [diff] [blame] | 1753 | */ |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1754 | if (close_asap) { |
| 1755 | if (SPOE_APPCTX(appctx)->cur_fpa) |
| 1756 | goto out; |
Christopher Faulet | 9e647e5 | 2021-03-01 15:01:14 +0100 | [diff] [blame] | 1757 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
| 1758 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1759 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1760 | goto next; |
| 1761 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1762 | _HA_ATOMIC_INC(&agent->counters.idles); |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1763 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1764 | eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1765 | } |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1766 | |
| 1767 | out: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1768 | return 1; |
| 1769 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1770 | next: |
| 1771 | SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle); |
| 1772 | return 0; |
| 1773 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1774 | exit: |
| 1775 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1776 | return 0; |
| 1777 | } |
| 1778 | |
| 1779 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1780 | spoe_handle_disconnect_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1781 | { |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 1782 | struct stream_interface *si = cs_si(appctx->owner); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1783 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1784 | char *frame, *buf; |
| 1785 | int ret; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1786 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1787 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) |
| 1788 | goto exit; |
| 1789 | |
| 1790 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) |
| 1791 | goto exit; |
| 1792 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1793 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1794 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1795 | buf = trash.area; frame = buf+4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1796 | ret = spoe_prepare_hadiscon_frame(appctx, frame, |
| 1797 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1798 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1799 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1800 | |
| 1801 | switch (ret) { |
| 1802 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1803 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1804 | goto exit; |
| 1805 | |
| 1806 | case 1: /* retry */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1807 | goto stop; |
| 1808 | |
| 1809 | default: |
| 1810 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1811 | " - disconnected by HAProxy (%d): %s\n", |
| 1812 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1813 | __FUNCTION__, appctx, |
| 1814 | SPOE_APPCTX(appctx)->status_code, |
| 1815 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1816 | |
| 1817 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1818 | goto next; |
| 1819 | } |
| 1820 | |
| 1821 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1822 | SPOE_APPCTX(appctx)->task->expire = |
| 1823 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1824 | return 0; |
| 1825 | stop: |
| 1826 | return 1; |
| 1827 | exit: |
| 1828 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1829 | return 0; |
| 1830 | } |
| 1831 | |
| 1832 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1833 | spoe_handle_disconnecting_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1834 | { |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 1835 | struct stream_interface *si = cs_si(appctx->owner); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1836 | char *frame; |
| 1837 | int ret; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1838 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1839 | 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] | 1840 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1841 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1842 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1843 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1844 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1845 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1846 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1847 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1848 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1849 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1850 | ret = spoe_recv_frame(appctx, frame, |
| 1851 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1852 | if (ret > 1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1853 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1854 | ret = spoe_handle_agentdiscon_frame(appctx, frame, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | switch (ret) { |
| 1858 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1859 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1860 | " - error on frame (%s)\n", |
| 1861 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1862 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1863 | __FUNCTION__, appctx, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1864 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1865 | goto exit; |
| 1866 | |
| 1867 | case 0: /* ignore */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1868 | goto next; |
| 1869 | |
| 1870 | case 1: /* retry */ |
| 1871 | goto stop; |
| 1872 | |
| 1873 | default: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1874 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1875 | " - disconnected by peer (%d): %.*s\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1876 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1877 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1878 | __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code, |
| 1879 | SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1880 | goto exit; |
| 1881 | } |
| 1882 | |
| 1883 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1884 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1885 | if (trash.data) |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 1886 | co_skip(si_oc(cs_si(appctx->owner)), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1887 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1888 | return 0; |
| 1889 | stop: |
| 1890 | return 1; |
| 1891 | exit: |
| 1892 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1893 | return 0; |
| 1894 | } |
| 1895 | |
| 1896 | /* I/O Handler processing messages exchanged with the agent */ |
| 1897 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1898 | spoe_handle_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1899 | { |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 1900 | struct stream_interface *si = cs_si(appctx->owner); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1901 | struct spoe_agent *agent; |
| 1902 | |
| 1903 | if (SPOE_APPCTX(appctx) == NULL) |
| 1904 | return; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1905 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1906 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
| 1907 | agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1908 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1909 | switchstate: |
| 1910 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1911 | " - appctx-state=%s\n", |
| 1912 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1913 | __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]); |
| 1914 | |
| 1915 | switch (appctx->st0) { |
| 1916 | case SPOE_APPCTX_ST_CONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1917 | if (spoe_handle_connect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1918 | goto out; |
| 1919 | goto switchstate; |
| 1920 | |
| 1921 | case SPOE_APPCTX_ST_CONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1922 | if (spoe_handle_connecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1923 | goto out; |
| 1924 | goto switchstate; |
| 1925 | |
| 1926 | case SPOE_APPCTX_ST_IDLE: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1927 | _HA_ATOMIC_DEC(&agent->counters.idles); |
Christopher Faulet | 7d9f1ba | 2018-02-28 13:33:26 +0100 | [diff] [blame] | 1928 | eb32_delete(&SPOE_APPCTX(appctx)->node); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1929 | if (stopping && |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1930 | LIST_ISEMPTY(&agent->rt[tid].sending_queue) && |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1931 | LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1932 | SPOE_APPCTX(appctx)->task->expire = |
| 1933 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1934 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1935 | goto switchstate; |
| 1936 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1937 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1938 | /* fall through */ |
| 1939 | |
| 1940 | case SPOE_APPCTX_ST_PROCESSING: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1941 | case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY: |
| 1942 | case SPOE_APPCTX_ST_WAITING_SYNC_ACK: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1943 | if (spoe_handle_processing_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1944 | goto out; |
| 1945 | goto switchstate; |
| 1946 | |
| 1947 | case SPOE_APPCTX_ST_DISCONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1948 | if (spoe_handle_disconnect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1949 | goto out; |
| 1950 | goto switchstate; |
| 1951 | |
| 1952 | case SPOE_APPCTX_ST_DISCONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1953 | if (spoe_handle_disconnecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1954 | goto out; |
| 1955 | goto switchstate; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1956 | |
| 1957 | case SPOE_APPCTX_ST_EXIT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1958 | appctx->st0 = SPOE_APPCTX_ST_END; |
| 1959 | SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY; |
| 1960 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1961 | si_shutw(si); |
| 1962 | si_shutr(si); |
| 1963 | si_ic(si)->flags |= CF_READ_NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1964 | /* fall through */ |
| 1965 | |
| 1966 | case SPOE_APPCTX_ST_END: |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1967 | return; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1968 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1969 | out: |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1970 | if (stopping) |
| 1971 | spoe_wakeup_appctx(appctx); |
| 1972 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1973 | if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY) |
| 1974 | task_queue(SPOE_APPCTX(appctx)->task); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | struct applet spoe_applet = { |
| 1978 | .obj_type = OBJ_TYPE_APPLET, |
| 1979 | .name = "<SPOE>", /* used for logging */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1980 | .fct = spoe_handle_appctx, |
| 1981 | .release = spoe_release_appctx, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1982 | }; |
| 1983 | |
| 1984 | /* Create a SPOE applet. On success, the created applet is returned, else |
| 1985 | * NULL. */ |
| 1986 | static struct appctx * |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1987 | spoe_create_appctx(struct spoe_config *conf) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1988 | { |
| 1989 | struct appctx *appctx; |
| 1990 | struct session *sess; |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 1991 | struct conn_stream *cs; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1992 | struct stream *strm; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1993 | |
Willy Tarreau | e612446 | 2021-09-13 10:07:38 +0200 | [diff] [blame] | 1994 | if ((appctx = appctx_new(&spoe_applet)) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1995 | goto out_error; |
| 1996 | |
Willy Tarreau | c9ef9bc | 2021-03-22 21:04:50 +0100 | [diff] [blame] | 1997 | appctx->ctx.spoe.ptr = pool_zalloc(pool_head_spoe_appctx); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1998 | if (SPOE_APPCTX(appctx) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1999 | goto out_free_appctx; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2000 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2001 | appctx->st0 = SPOE_APPCTX_ST_CONNECT; |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 2002 | if ((SPOE_APPCTX(appctx)->task = task_new_here()) == NULL) |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2003 | goto out_free_spoe_appctx; |
| 2004 | |
| 2005 | SPOE_APPCTX(appctx)->owner = appctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2006 | SPOE_APPCTX(appctx)->task->process = spoe_process_appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2007 | SPOE_APPCTX(appctx)->task->context = appctx; |
| 2008 | SPOE_APPCTX(appctx)->agent = conf->agent; |
| 2009 | SPOE_APPCTX(appctx)->version = 0; |
| 2010 | SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size; |
| 2011 | SPOE_APPCTX(appctx)->flags = 0; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2012 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2013 | SPOE_APPCTX(appctx)->buffer = BUF_NULL; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 2014 | SPOE_APPCTX(appctx)->cur_fpa = 0; |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2015 | |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2016 | LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list); |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2017 | SPOE_APPCTX(appctx)->buffer_wait.target = appctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2018 | SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2019 | |
| 2020 | LIST_INIT(&SPOE_APPCTX(appctx)->list); |
| 2021 | LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2022 | |
Willy Tarreau | 5820a36 | 2016-12-22 15:59:02 +0100 | [diff] [blame] | 2023 | sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2024 | if (!sess) |
| 2025 | goto out_free_spoe; |
| 2026 | |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 2027 | cs = cs_new(); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2028 | if (!cs) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2029 | goto out_free_sess; |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 2030 | cs_attach_endp(cs, &appctx->obj_type, appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2031 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2032 | if ((strm = stream_new(sess, cs, &BUF_NULL)) == NULL) |
| 2033 | goto out_free_cs; |
| 2034 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2035 | stream_set_backend(strm, conf->agent->b.be); |
| 2036 | |
| 2037 | /* applet is waiting for data */ |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame] | 2038 | si_cant_get(strm->csf->si); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2039 | appctx_wakeup(appctx); |
| 2040 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2041 | strm->do_log = NULL; |
| 2042 | strm->res.flags |= CF_READ_DONTWAIT; |
| 2043 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2044 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2045 | LIST_APPEND(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2046 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2047 | _HA_ATOMIC_INC(&conf->agent->counters.applets); |
Emeric Brun | 5f77fef | 2017-05-29 15:26:51 +0200 | [diff] [blame] | 2048 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 2049 | task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2050 | return appctx; |
| 2051 | |
| 2052 | /* Error unrolling */ |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2053 | out_free_cs: |
| 2054 | cs_free(cs); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2055 | out_free_sess: |
| 2056 | session_free(sess); |
| 2057 | out_free_spoe: |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 2058 | task_destroy(SPOE_APPCTX(appctx)->task); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2059 | out_free_spoe_appctx: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2060 | pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx)); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2061 | out_free_appctx: |
| 2062 | appctx_free(appctx); |
| 2063 | out_error: |
| 2064 | return NULL; |
| 2065 | } |
| 2066 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2067 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2068 | spoe_queue_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2069 | { |
| 2070 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2071 | struct spoe_agent *agent = conf->agent; |
| 2072 | struct appctx *appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2073 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2074 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2075 | /* Check if we need to create a new SPOE applet or not. */ |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 2076 | if (!eb_is_empty(&agent->rt[tid].idle_applets) && |
Christopher Faulet | eccfa61 | 2020-06-22 15:32:14 +0200 | [diff] [blame] | 2077 | (agent->rt[tid].processing == 1 || agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2078 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2079 | |
| 2080 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2081 | " - try to create new SPOE appctx\n", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2082 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__, |
| 2083 | ctx->strm); |
| 2084 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2085 | /* Do not try to create a new applet if there is no server up for the |
| 2086 | * agent's backend. */ |
| 2087 | if (!agent->b.be->srv_act && !agent->b.be->srv_bck) { |
| 2088 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2089 | " - cannot create SPOE appctx: no server up\n", |
| 2090 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2091 | __FUNCTION__, ctx->strm); |
| 2092 | goto end; |
| 2093 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2094 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2095 | /* Do not try to create a new applet if we have reached the maximum of |
| 2096 | * connection per seconds */ |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 2097 | if (agent->cps_max > 0) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2098 | if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2099 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2100 | " - cannot create SPOE appctx: max CPS reached\n", |
| 2101 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2102 | __FUNCTION__, ctx->strm); |
| 2103 | goto end; |
| 2104 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2105 | } |
| 2106 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2107 | appctx = spoe_create_appctx(conf); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2108 | if (appctx == NULL) { |
| 2109 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2110 | " - failed to create SPOE appctx\n", |
| 2111 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2112 | __FUNCTION__, ctx->strm); |
Christopher Faulet | 3b8e349 | 2018-03-26 17:20:58 +0200 | [diff] [blame] | 2113 | send_log(&conf->agent_fe, LOG_EMERG, |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2114 | "SPOE: [%s] failed to create SPOE applet\n", |
| 2115 | agent->id); |
| 2116 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2117 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2118 | } |
| 2119 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2120 | /* Increase the per-process number of cumulated connections */ |
| 2121 | if (agent->cps_max > 0) |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2122 | update_freq_ctr(&agent->rt[tid].conn_per_sec, 1); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2123 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2124 | end: |
| 2125 | /* 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] | 2126 | if (LIST_ISEMPTY(&agent->rt[tid].applets)) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2127 | ctx->status_code = SPOE_CTX_ERR_RES; |
| 2128 | return -1; |
| 2129 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2130 | |
Christopher Faulet | 3e86cec | 2019-04-10 14:02:12 +0200 | [diff] [blame] | 2131 | /* Add the SPOE context in the sending queue if the stream has no applet |
| 2132 | * already assigned and wakeup all idle applets. Otherwise, don't queue |
| 2133 | * it. */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2134 | _HA_ATOMIC_INC(&agent->counters.nb_sending); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2135 | spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request); |
| 2136 | ctx->stats.tv_queue = now; |
Christopher Faulet | 3e86cec | 2019-04-10 14:02:12 +0200 | [diff] [blame] | 2137 | if (ctx->spoe_appctx) |
| 2138 | return 1; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2139 | LIST_APPEND(&agent->rt[tid].sending_queue, &ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2140 | |
| 2141 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2142 | " - Add stream in sending queue" |
Christopher Faulet | 68db023 | 2018-04-06 11:34:12 +0200 | [diff] [blame] | 2143 | " - applets=%u - idles=%u - processing=%u\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2144 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__, |
Christopher Faulet | 68db023 | 2018-04-06 11:34:12 +0200 | [diff] [blame] | 2145 | ctx->strm, agent->counters.applets, agent->counters.idles, |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2146 | agent->rt[tid].processing); |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2147 | |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 2148 | /* Finally try to wakeup an IDLE applet. */ |
| 2149 | if (!eb_is_empty(&agent->rt[tid].idle_applets)) { |
| 2150 | struct eb32_node *node; |
| 2151 | |
| 2152 | node = eb32_first(&agent->rt[tid].idle_applets); |
| 2153 | spoe_appctx = eb32_entry(node, struct spoe_appctx, node); |
| 2154 | if (node && spoe_appctx) { |
| 2155 | eb32_delete(&spoe_appctx->node); |
| 2156 | spoe_appctx->node.key++; |
| 2157 | eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node); |
| 2158 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2159 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2160 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2161 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2162 | } |
| 2163 | |
| 2164 | /*************************************************************************** |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2165 | * Functions that encode SPOE messages |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2166 | **************************************************************************/ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2167 | /* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle |
| 2168 | * fragmented_content. If the next message can be processed, it returns 0. If |
| 2169 | * the message is too big, it returns -1.*/ |
| 2170 | static int |
| 2171 | spoe_encode_message(struct stream *s, struct spoe_context *ctx, |
| 2172 | struct spoe_message *msg, int dir, |
| 2173 | char **buf, char *end) |
| 2174 | { |
| 2175 | struct sample *smp; |
| 2176 | struct spoe_arg *arg; |
| 2177 | int ret; |
| 2178 | |
| 2179 | if (msg->cond) { |
| 2180 | ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2181 | ret = acl_pass(ret); |
| 2182 | if (msg->cond->pol == ACL_COND_UNLESS) |
| 2183 | ret = !ret; |
| 2184 | |
| 2185 | /* the rule does not match */ |
| 2186 | if (!ret) |
| 2187 | goto next; |
| 2188 | } |
| 2189 | |
| 2190 | /* Resume encoding of a SPOE argument */ |
| 2191 | if (ctx->frag_ctx.curarg != NULL) { |
| 2192 | arg = ctx->frag_ctx.curarg; |
| 2193 | goto encode_argument; |
| 2194 | } |
| 2195 | |
| 2196 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2197 | goto encode_msg_payload; |
| 2198 | |
| 2199 | /* Check if there is enough space for the message name and the |
| 2200 | * number of arguments. It implies <msg->id_len> is encoded on 2 |
| 2201 | * bytes, at most (< 2288). */ |
| 2202 | if (*buf + 2 + msg->id_len + 1 > end) |
| 2203 | goto too_big; |
| 2204 | |
| 2205 | /* Encode the message name */ |
| 2206 | if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1) |
| 2207 | goto too_big; |
| 2208 | |
| 2209 | /* Set the number of arguments for this message */ |
| 2210 | **buf = msg->nargs; |
| 2211 | (*buf)++; |
| 2212 | |
| 2213 | ctx->frag_ctx.curoff = 0; |
| 2214 | encode_msg_payload: |
| 2215 | |
| 2216 | /* Loop on arguments */ |
| 2217 | list_for_each_entry(arg, &msg->args, list) { |
| 2218 | ctx->frag_ctx.curarg = arg; |
| 2219 | ctx->frag_ctx.curoff = UINT_MAX; |
Kevin Zhu | f7f5428 | 2019-04-26 14:00:01 +0800 | [diff] [blame] | 2220 | ctx->frag_ctx.curlen = 0; |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2221 | |
| 2222 | encode_argument: |
| 2223 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2224 | goto encode_arg_value; |
| 2225 | |
Joseph Herlant | f7f6031 | 2018-11-15 13:46:49 -0800 | [diff] [blame] | 2226 | /* Encode the argument name as a string. It can by NULL */ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2227 | if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1) |
| 2228 | goto too_big; |
| 2229 | |
| 2230 | ctx->frag_ctx.curoff = 0; |
| 2231 | encode_arg_value: |
| 2232 | |
Joseph Herlant | f7f6031 | 2018-11-15 13:46:49 -0800 | [diff] [blame] | 2233 | /* Fetch the argument value */ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2234 | 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] | 2235 | if (smp) { |
| 2236 | smp->ctx.a[0] = &ctx->frag_ctx.curlen; |
| 2237 | smp->ctx.a[1] = &ctx->frag_ctx.curoff; |
| 2238 | } |
Christopher Faulet | 85db321 | 2019-04-26 14:30:15 +0200 | [diff] [blame] | 2239 | ret = spoe_encode_data(smp, buf, end); |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2240 | if (ret == -1 || ctx->frag_ctx.curoff) |
| 2241 | goto too_big; |
| 2242 | } |
| 2243 | |
| 2244 | next: |
| 2245 | return 0; |
| 2246 | |
| 2247 | too_big: |
| 2248 | return -1; |
| 2249 | } |
| 2250 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2251 | /* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to |
| 2252 | * handle fragmented content. On success it returns 1. If an error occurred, -1 |
| 2253 | * is returned. If nothing has been encoded, it returns 0 (this is only possible |
| 2254 | * for unfragmented payload). */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2255 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2256 | spoe_encode_messages(struct stream *s, struct spoe_context *ctx, |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2257 | struct list *messages, int dir, int type) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2258 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2259 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2260 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2261 | struct spoe_message *msg; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2262 | char *p, *end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2263 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2264 | p = b_head(&ctx->buffer); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2265 | end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2266 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2267 | if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */ |
| 2268 | /* Resume encoding of a SPOE message */ |
| 2269 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2270 | msg = ctx->frag_ctx.curmsg; |
| 2271 | goto encode_evt_message; |
| 2272 | } |
| 2273 | |
| 2274 | list_for_each_entry(msg, messages, by_evt) { |
| 2275 | ctx->frag_ctx.curmsg = msg; |
| 2276 | ctx->frag_ctx.curarg = NULL; |
| 2277 | ctx->frag_ctx.curoff = UINT_MAX; |
| 2278 | |
| 2279 | encode_evt_message: |
| 2280 | if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1) |
| 2281 | goto too_big; |
| 2282 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2283 | } |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2284 | else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */ |
| 2285 | /* Resume encoding of a SPOE message */ |
| 2286 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2287 | msg = ctx->frag_ctx.curmsg; |
| 2288 | goto encode_grp_message; |
| 2289 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2290 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2291 | list_for_each_entry(msg, messages, by_grp) { |
| 2292 | ctx->frag_ctx.curmsg = msg; |
| 2293 | ctx->frag_ctx.curarg = NULL; |
| 2294 | ctx->frag_ctx.curoff = UINT_MAX; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2295 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2296 | encode_grp_message: |
| 2297 | if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1) |
| 2298 | goto too_big; |
| 2299 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2300 | } |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2301 | else |
| 2302 | goto skip; |
| 2303 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2304 | |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2305 | /* nothing has been encoded for an unfragmented payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2306 | if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer)) |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2307 | goto skip; |
| 2308 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2309 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2310 | " - encode %s messages - spoe_appctx=%p" |
| 2311 | "- max_size=%u - encoded=%ld\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2312 | (int)now.tv_sec, (int)now.tv_usec, |
| 2313 | agent->id, __FUNCTION__, s, |
| 2314 | ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"), |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2315 | ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE), |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2316 | p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2317 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2318 | b_set_data(&ctx->buffer, p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2319 | ctx->frag_ctx.curmsg = NULL; |
| 2320 | ctx->frag_ctx.curarg = NULL; |
| 2321 | ctx->frag_ctx.curoff = 0; |
| 2322 | ctx->frag_ctx.flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2323 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2324 | return 1; |
| 2325 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2326 | too_big: |
Christopher Faulet | a715ea8 | 2019-04-10 14:21:51 +0200 | [diff] [blame] | 2327 | /* Return an error if fragmentation is unsupported or if nothing has |
| 2328 | * been encoded because its too big and not splittable. */ |
| 2329 | if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) { |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 2330 | ctx->status_code = SPOE_CTX_ERR_TOO_BIG; |
| 2331 | return -1; |
| 2332 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2333 | |
| 2334 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2335 | " - encode fragmented messages - spoe_appctx=%p" |
| 2336 | " - curmsg=%p - curarg=%p - curoff=%u" |
| 2337 | " - max_size=%u - encoded=%ld\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2338 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2339 | agent->id, __FUNCTION__, s, ctx->spoe_appctx, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2340 | ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff, |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2341 | (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] | 2342 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2343 | b_set_data(&ctx->buffer, p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2344 | ctx->flags |= SPOE_CTX_FL_FRAGMENTED; |
| 2345 | ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN; |
| 2346 | return 1; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2347 | |
| 2348 | skip: |
| 2349 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2350 | " - skip the frame because nothing has been encoded\n", |
| 2351 | (int)now.tv_sec, (int)now.tv_usec, |
| 2352 | agent->id, __FUNCTION__, s); |
| 2353 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2354 | } |
| 2355 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2356 | |
| 2357 | /*************************************************************************** |
| 2358 | * Functions that handle SPOE actions |
| 2359 | **************************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2360 | /* Helper function to set a variable */ |
| 2361 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2362 | 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] | 2363 | struct sample *smp) |
| 2364 | { |
| 2365 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2366 | struct spoe_agent *agent = conf->agent; |
| 2367 | char varname[64]; |
| 2368 | |
| 2369 | memset(varname, 0, sizeof(varname)); |
| 2370 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2371 | scope, agent->var_pfx, len, name); |
Etienne Carriere | aec8989 | 2017-12-14 09:36:40 +0000 | [diff] [blame] | 2372 | if (agent->flags & SPOE_FL_FORCE_SET_VAR) |
| 2373 | vars_set_by_name(varname, len, smp); |
| 2374 | else |
| 2375 | vars_set_by_name_ifexist(varname, len, smp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2376 | } |
| 2377 | |
| 2378 | /* Helper function to unset a variable */ |
| 2379 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2380 | 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] | 2381 | struct sample *smp) |
| 2382 | { |
| 2383 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2384 | struct spoe_agent *agent = conf->agent; |
| 2385 | char varname[64]; |
| 2386 | |
| 2387 | memset(varname, 0, sizeof(varname)); |
| 2388 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2389 | scope, agent->var_pfx, len, name); |
| 2390 | vars_unset_by_name_ifexist(varname, len, smp); |
| 2391 | } |
| 2392 | |
| 2393 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2394 | static inline int |
| 2395 | spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx, |
| 2396 | char **buf, char *end, int dir) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2397 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2398 | char *str, *scope, *p = *buf; |
| 2399 | struct sample smp; |
| 2400 | uint64_t sz; |
| 2401 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2402 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2403 | if (p + 2 >= end) |
| 2404 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2405 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2406 | /* SET-VAR requires 3 arguments */ |
| 2407 | if (*p++ != 3) |
| 2408 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2409 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2410 | switch (*p++) { |
| 2411 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2412 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2413 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2414 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2415 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2416 | default: goto skip; |
| 2417 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2418 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2419 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2420 | goto skip; |
| 2421 | memset(&smp, 0, sizeof(smp)); |
| 2422 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2423 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2424 | if (spoe_decode_data(&p, end, &smp) == -1) |
| 2425 | goto skip; |
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 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2428 | " - set-var '%s.%s.%.*s'\n", |
| 2429 | (int)now.tv_sec, (int)now.tv_usec, |
| 2430 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2431 | __FUNCTION__, s, scope, |
| 2432 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2433 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2434 | |
Christopher Faulet | 2469eba | 2020-10-15 16:08:30 +0200 | [diff] [blame] | 2435 | if (smp.data.type == SMP_T_ANY) |
| 2436 | spoe_unset_var(ctx, scope, str, sz, &smp); |
| 2437 | else |
| 2438 | spoe_set_var(ctx, scope, str, sz, &smp); |
Christopher Faulet | b5cff60 | 2016-11-24 14:53:22 +0100 | [diff] [blame] | 2439 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2440 | ret = (p - *buf); |
| 2441 | *buf = p; |
| 2442 | return ret; |
| 2443 | skip: |
| 2444 | return 0; |
| 2445 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2446 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2447 | static inline int |
| 2448 | spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx, |
| 2449 | char **buf, char *end, int dir) |
| 2450 | { |
| 2451 | char *str, *scope, *p = *buf; |
| 2452 | struct sample smp; |
| 2453 | uint64_t sz; |
| 2454 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2455 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2456 | if (p + 2 >= end) |
| 2457 | goto skip; |
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 | /* UNSET-VAR requires 2 arguments */ |
| 2460 | if (*p++ != 2) |
| 2461 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2462 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2463 | switch (*p++) { |
| 2464 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2465 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2466 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2467 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2468 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2469 | default: goto skip; |
| 2470 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2471 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2472 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2473 | goto skip; |
| 2474 | memset(&smp, 0, sizeof(smp)); |
| 2475 | 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] | 2476 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2477 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2478 | " - unset-var '%s.%s.%.*s'\n", |
| 2479 | (int)now.tv_sec, (int)now.tv_usec, |
| 2480 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2481 | __FUNCTION__, s, scope, |
| 2482 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2483 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2484 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2485 | spoe_unset_var(ctx, scope, str, sz, &smp); |
| 2486 | |
| 2487 | ret = (p - *buf); |
| 2488 | *buf = p; |
| 2489 | return ret; |
| 2490 | skip: |
| 2491 | return 0; |
| 2492 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2493 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2494 | /* Process SPOE actions for a specific event. It returns 1 on success. If an |
| 2495 | * error occurred, 0 is returned. */ |
| 2496 | static int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2497 | spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2498 | { |
| 2499 | char *p, *end; |
| 2500 | int ret; |
| 2501 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2502 | p = b_head(&ctx->buffer); |
| 2503 | end = p + b_data(&ctx->buffer); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2504 | |
| 2505 | while (p < end) { |
| 2506 | enum spoe_action_type type; |
| 2507 | |
| 2508 | type = *p++; |
| 2509 | switch (type) { |
| 2510 | case SPOE_ACT_T_SET_VAR: |
| 2511 | ret = spoe_decode_action_set_var(s, ctx, &p, end, dir); |
| 2512 | if (!ret) |
| 2513 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2514 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2515 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2516 | case SPOE_ACT_T_UNSET_VAR: |
| 2517 | ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir); |
| 2518 | if (!ret) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2519 | goto skip; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2520 | break; |
| 2521 | |
| 2522 | default: |
| 2523 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2524 | } |
| 2525 | } |
| 2526 | |
| 2527 | return 1; |
| 2528 | skip: |
| 2529 | return 0; |
| 2530 | } |
| 2531 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2532 | /*************************************************************************** |
| 2533 | * Functions that process SPOE events |
| 2534 | **************************************************************************/ |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2535 | static void |
| 2536 | spoe_update_stats(struct stream *s, struct spoe_agent *agent, |
| 2537 | struct spoe_context *ctx, int dir) |
| 2538 | { |
| 2539 | if (!tv_iszero(&ctx->stats.tv_start)) { |
| 2540 | spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process); |
| 2541 | ctx->stats.t_total += ctx->stats.t_process; |
| 2542 | tv_zero(&ctx->stats.tv_request); |
| 2543 | tv_zero(&ctx->stats.tv_queue); |
| 2544 | tv_zero(&ctx->stats.tv_wait); |
| 2545 | tv_zero(&ctx->stats.tv_response); |
| 2546 | } |
| 2547 | |
| 2548 | if (agent->var_t_process) { |
| 2549 | struct sample smp; |
| 2550 | |
| 2551 | memset(&smp, 0, sizeof(smp)); |
| 2552 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2553 | smp.data.u.sint = ctx->stats.t_process; |
| 2554 | smp.data.type = SMP_T_SINT; |
| 2555 | |
| 2556 | spoe_set_var(ctx, "txn", agent->var_t_process, |
| 2557 | strlen(agent->var_t_process), &smp); |
| 2558 | } |
| 2559 | |
| 2560 | if (agent->var_t_total) { |
| 2561 | struct sample smp; |
| 2562 | |
| 2563 | memset(&smp, 0, sizeof(smp)); |
| 2564 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2565 | smp.data.u.sint = ctx->stats.t_total; |
| 2566 | smp.data.type = SMP_T_SINT; |
| 2567 | |
| 2568 | spoe_set_var(ctx, "txn", agent->var_t_total, |
| 2569 | strlen(agent->var_t_total), &smp); |
| 2570 | } |
| 2571 | } |
| 2572 | |
| 2573 | static void |
| 2574 | spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent, |
| 2575 | struct spoe_context *ctx, int dir) |
| 2576 | { |
| 2577 | if (agent->eps_max > 0) |
| 2578 | update_freq_ctr(&agent->rt[tid].err_per_sec, 1); |
| 2579 | |
| 2580 | if (agent->var_on_error) { |
| 2581 | struct sample smp; |
| 2582 | |
| 2583 | memset(&smp, 0, sizeof(smp)); |
| 2584 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2585 | smp.data.u.sint = ctx->status_code; |
| 2586 | smp.data.type = SMP_T_BOOL; |
| 2587 | |
| 2588 | spoe_set_var(ctx, "txn", agent->var_on_error, |
| 2589 | strlen(agent->var_on_error), &smp); |
| 2590 | } |
| 2591 | |
| 2592 | ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR) |
| 2593 | ? SPOE_CTX_ST_READY |
| 2594 | : SPOE_CTX_ST_NONE); |
| 2595 | } |
| 2596 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2597 | static inline int |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2598 | 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] | 2599 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2600 | /* If a process is already started for this SPOE context, retry |
| 2601 | * later. */ |
| 2602 | if (ctx->flags & SPOE_CTX_FL_PROCESS) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2603 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2604 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2605 | agent->rt[tid].processing++; |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2606 | ctx->stats.tv_start = now; |
| 2607 | ctx->stats.tv_request = now; |
| 2608 | ctx->stats.t_request = -1; |
| 2609 | ctx->stats.t_queue = -1; |
| 2610 | ctx->stats.t_waiting = -1; |
| 2611 | ctx->stats.t_response = -1; |
| 2612 | ctx->stats.t_process = -1; |
| 2613 | |
| 2614 | ctx->status_code = 0; |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2615 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2616 | /* Set the right flag to prevent request and response processing |
| 2617 | * in same time. */ |
| 2618 | ctx->flags |= ((dir == SMP_OPT_DIR_REQ) |
| 2619 | ? SPOE_CTX_FL_REQ_PROCESS |
| 2620 | : SPOE_CTX_FL_RSP_PROCESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2621 | return 1; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2622 | } |
| 2623 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2624 | static inline void |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2625 | spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2626 | { |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2627 | struct spoe_appctx *sa = ctx->spoe_appctx; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2628 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2629 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) |
| 2630 | return; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2631 | _HA_ATOMIC_INC(&agent->counters.nb_processed); |
Christopher Faulet | 879dca9 | 2018-03-23 11:53:24 +0100 | [diff] [blame] | 2632 | if (sa) { |
| 2633 | if (sa->frag_ctx.ctx == ctx) { |
| 2634 | sa->frag_ctx.ctx = NULL; |
| 2635 | spoe_wakeup_appctx(sa->owner); |
| 2636 | } |
| 2637 | else |
| 2638 | sa->cur_fpa--; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2639 | } |
| 2640 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2641 | /* Reset the flag to allow next processing */ |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2642 | agent->rt[tid].processing--; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2643 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2644 | |
| 2645 | /* Reset processing timer */ |
| 2646 | ctx->process_exp = TICK_ETERNITY; |
| 2647 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2648 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2649 | |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2650 | ctx->spoe_appctx = NULL; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2651 | ctx->frag_ctx.curmsg = NULL; |
| 2652 | ctx->frag_ctx.curarg = NULL; |
| 2653 | ctx->frag_ctx.curoff = 0; |
| 2654 | ctx->frag_ctx.flags = 0; |
| 2655 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2656 | if (!LIST_ISEMPTY(&ctx->list)) { |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2657 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2658 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2659 | else |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2660 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2661 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2662 | LIST_DELETE(&ctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2663 | LIST_INIT(&ctx->list); |
| 2664 | } |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2665 | } |
| 2666 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2667 | /* Process a list of SPOE messages. First, this functions will process messages |
| 2668 | * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame |
| 2669 | * to process corresponding actions. During all the processing, it returns 0 |
| 2670 | * and it returns 1 when the processing is finished. If an error occurred, -1 |
| 2671 | * is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2672 | static int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2673 | spoe_process_messages(struct stream *s, struct spoe_context *ctx, |
| 2674 | struct list *messages, int dir, int type) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2675 | { |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2676 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2677 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2678 | int ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2679 | |
| 2680 | if (ctx->state == SPOE_CTX_ST_ERROR) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2681 | goto end; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2682 | |
| 2683 | if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) { |
| 2684 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2685 | " - failed to process messages: timeout\n", |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2686 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2687 | agent->id, __FUNCTION__, s); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2688 | ctx->status_code = SPOE_CTX_ERR_TOUT; |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2689 | goto end; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2690 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2691 | |
| 2692 | if (ctx->state == SPOE_CTX_ST_READY) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2693 | if (agent->eps_max > 0) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2694 | 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] | 2695 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2696 | " - skip processing of messages: max EPS reached\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2697 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2698 | agent->id, __FUNCTION__, s); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2699 | goto skip; |
| 2700 | } |
| 2701 | } |
| 2702 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2703 | if (!tick_isset(ctx->process_exp)) { |
| 2704 | ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing); |
| 2705 | s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire), |
| 2706 | ctx->process_exp); |
| 2707 | } |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2708 | ret = spoe_start_processing(agent, ctx, dir); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2709 | if (!ret) |
| 2710 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2711 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2712 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2713 | /* fall through */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2714 | } |
| 2715 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2716 | if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) { |
Christopher Faulet | 63263e5 | 2019-04-10 14:47:18 +0200 | [diff] [blame] | 2717 | if (tv_iszero(&ctx->stats.tv_request)) |
| 2718 | ctx->stats.tv_request = now; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2719 | if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait)) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2720 | goto out; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2721 | ret = spoe_encode_messages(s, ctx, messages, dir, type); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2722 | if (ret < 0) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2723 | goto end; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2724 | if (!ret) |
| 2725 | goto skip; |
Christopher Faulet | 333694d | 2018-01-12 10:45:47 +0100 | [diff] [blame] | 2726 | if (spoe_queue_context(ctx) < 0) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2727 | goto end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2728 | ctx->state = SPOE_CTX_ST_SENDING_MSGS; |
| 2729 | } |
| 2730 | |
| 2731 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) { |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2732 | if (ctx->spoe_appctx) |
| 2733 | spoe_wakeup_appctx(ctx->spoe_appctx->owner); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2734 | ret = 0; |
| 2735 | goto out; |
| 2736 | } |
| 2737 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2738 | if (ctx->state == SPOE_CTX_ST_WAITING_ACK) { |
| 2739 | ret = 0; |
| 2740 | goto out; |
| 2741 | } |
| 2742 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2743 | if (ctx->state == SPOE_CTX_ST_DONE) { |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2744 | spoe_process_actions(s, ctx, dir); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2745 | ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2746 | ctx->frame_id++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2747 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2748 | spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2749 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2750 | } |
| 2751 | |
| 2752 | out: |
| 2753 | return ret; |
| 2754 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2755 | skip: |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2756 | tv_zero(&ctx->stats.tv_start); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2757 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2758 | spoe_stop_processing(agent, ctx); |
| 2759 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2760 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2761 | end: |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2762 | spoe_update_stats(s, agent, ctx, dir); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2763 | spoe_stop_processing(agent, ctx); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2764 | if (ctx->status_code) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2765 | _HA_ATOMIC_INC(&agent->counters.nb_errors); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2766 | spoe_handle_processing_error(s, agent, ctx, dir); |
| 2767 | ret = 1; |
| 2768 | } |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2769 | return ret; |
| 2770 | } |
| 2771 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2772 | /* Process a SPOE group, ie the list of messages attached to the group <grp>. |
| 2773 | * See spoe_process_message for details. */ |
| 2774 | static int |
| 2775 | spoe_process_group(struct stream *s, struct spoe_context *ctx, |
| 2776 | struct spoe_group *group, int dir) |
| 2777 | { |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2778 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2779 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2780 | int ret; |
| 2781 | |
| 2782 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2783 | " - ctx-state=%s - Process messages for group=%s\n", |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2784 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2785 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2786 | group->id); |
| 2787 | |
| 2788 | if (LIST_ISEMPTY(&group->messages)) |
| 2789 | return 1; |
| 2790 | |
| 2791 | 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] | 2792 | if (ret && ctx->stats.t_process != -1) { |
| 2793 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2794 | " - <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] | 2795 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2796 | __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code, |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2797 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2798 | ctx->stats.t_response, ctx->stats.t_process, |
| 2799 | agent->counters.idles, agent->counters.applets, |
| 2800 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2801 | agent->counters.nb_errors, agent->counters.nb_processed, |
| 2802 | agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec)); |
| 2803 | if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM)) |
| 2804 | send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING), |
| 2805 | "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n", |
| 2806 | agent->id, group->id, s->uniq_id, ctx->status_code, |
| 2807 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
| 2808 | ctx->stats.t_response, ctx->stats.t_process, |
| 2809 | agent->counters.idles, agent->counters.applets, |
| 2810 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2811 | agent->counters.nb_errors, agent->counters.nb_processed); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2812 | } |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2813 | return ret; |
| 2814 | } |
| 2815 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2816 | /* Process a SPOE event, ie the list of messages attached to the event <ev>. |
| 2817 | * See spoe_process_message for details. */ |
| 2818 | static int |
| 2819 | spoe_process_event(struct stream *s, struct spoe_context *ctx, |
| 2820 | enum spoe_event ev) |
| 2821 | { |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2822 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2823 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2824 | int dir, ret; |
| 2825 | |
| 2826 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2827 | " - ctx-state=%s - Process messages for event=%s\n", |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2828 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2829 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2830 | spoe_event_str[ev]); |
| 2831 | |
| 2832 | dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 2833 | |
| 2834 | if (LIST_ISEMPTY(&(ctx->events[ev]))) |
| 2835 | return 1; |
| 2836 | |
| 2837 | 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] | 2838 | if (ret && ctx->stats.t_process != -1) { |
| 2839 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2840 | " - <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] | 2841 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2842 | __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code, |
| 2843 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2844 | ctx->stats.t_response, ctx->stats.t_process, |
| 2845 | agent->counters.idles, agent->counters.applets, |
| 2846 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2847 | agent->counters.nb_errors, agent->counters.nb_processed, |
| 2848 | agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec)); |
| 2849 | if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM)) |
| 2850 | send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING), |
| 2851 | "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n", |
| 2852 | agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code, |
| 2853 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
| 2854 | ctx->stats.t_response, ctx->stats.t_process, |
| 2855 | agent->counters.idles, agent->counters.applets, |
| 2856 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2857 | agent->counters.nb_errors, agent->counters.nb_processed); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2858 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2859 | return ret; |
| 2860 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2861 | |
| 2862 | /*************************************************************************** |
| 2863 | * Functions that create/destroy SPOE contexts |
| 2864 | **************************************************************************/ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2865 | static int |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2866 | spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2867 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2868 | if (buf->size) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2869 | return 1; |
| 2870 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2871 | if (LIST_INLIST(&buffer_wait->list)) |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2872 | LIST_DEL_INIT(&buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2873 | |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 2874 | if (b_alloc(buf)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2875 | return 1; |
| 2876 | |
Willy Tarreau | b4e3476 | 2021-09-30 19:02:18 +0200 | [diff] [blame] | 2877 | LIST_APPEND(&th_ctx->buffer_wq, &buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2878 | return 0; |
| 2879 | } |
| 2880 | |
| 2881 | static void |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2882 | spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2883 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2884 | if (LIST_INLIST(&buffer_wait->list)) |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2885 | LIST_DEL_INIT(&buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2886 | |
| 2887 | /* Release the buffer if needed */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2888 | if (buf->size) { |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2889 | b_free(buf); |
Willy Tarreau | 4d77bbf | 2021-02-20 12:02:46 +0100 | [diff] [blame] | 2890 | offer_buffers(buffer_wait->target, 1); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2891 | } |
| 2892 | } |
| 2893 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2894 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2895 | spoe_wakeup_context(struct spoe_context *ctx) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2896 | { |
| 2897 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 2898 | return 1; |
| 2899 | } |
| 2900 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2901 | static struct spoe_context * |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2902 | spoe_create_context(struct stream *s, struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2903 | { |
| 2904 | struct spoe_config *conf = FLT_CONF(filter); |
| 2905 | struct spoe_context *ctx; |
| 2906 | |
Willy Tarreau | c9ef9bc | 2021-03-22 21:04:50 +0100 | [diff] [blame] | 2907 | ctx = pool_zalloc(pool_head_spoe_ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2908 | if (ctx == NULL) { |
| 2909 | return NULL; |
| 2910 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2911 | ctx->filter = filter; |
| 2912 | ctx->state = SPOE_CTX_ST_NONE; |
| 2913 | ctx->status_code = SPOE_CTX_ERR_NONE; |
| 2914 | ctx->flags = 0; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2915 | ctx->events = conf->agent->events; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 2916 | ctx->groups = &conf->agent->groups; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2917 | ctx->buffer = BUF_NULL; |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2918 | LIST_INIT(&ctx->buffer_wait.list); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2919 | ctx->buffer_wait.target = ctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2920 | ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2921 | LIST_INIT(&ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2922 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2923 | ctx->stream_id = 0; |
| 2924 | ctx->frame_id = 1; |
| 2925 | ctx->process_exp = TICK_ETERNITY; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2926 | |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2927 | tv_zero(&ctx->stats.tv_start); |
| 2928 | tv_zero(&ctx->stats.tv_request); |
| 2929 | tv_zero(&ctx->stats.tv_queue); |
| 2930 | tv_zero(&ctx->stats.tv_wait); |
| 2931 | tv_zero(&ctx->stats.tv_response); |
| 2932 | ctx->stats.t_request = -1; |
| 2933 | ctx->stats.t_queue = -1; |
| 2934 | ctx->stats.t_waiting = -1; |
| 2935 | ctx->stats.t_response = -1; |
| 2936 | ctx->stats.t_process = -1; |
| 2937 | ctx->stats.t_total = 0; |
| 2938 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2939 | ctx->strm = s; |
| 2940 | ctx->state = SPOE_CTX_ST_READY; |
| 2941 | filter->ctx = ctx; |
| 2942 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2943 | return ctx; |
| 2944 | } |
| 2945 | |
| 2946 | static void |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2947 | spoe_destroy_context(struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2948 | { |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2949 | struct spoe_config *conf = FLT_CONF(filter); |
| 2950 | struct spoe_context *ctx = filter->ctx; |
| 2951 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2952 | if (!ctx) |
| 2953 | return; |
| 2954 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2955 | spoe_stop_processing(conf->agent, ctx); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2956 | pool_free(pool_head_spoe_ctx, ctx); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2957 | filter->ctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2958 | } |
| 2959 | |
| 2960 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2961 | spoe_reset_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2962 | { |
| 2963 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 2964 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2965 | |
| 2966 | tv_zero(&ctx->stats.tv_start); |
| 2967 | tv_zero(&ctx->stats.tv_request); |
| 2968 | tv_zero(&ctx->stats.tv_queue); |
| 2969 | tv_zero(&ctx->stats.tv_wait); |
| 2970 | tv_zero(&ctx->stats.tv_response); |
| 2971 | ctx->stats.t_request = -1; |
| 2972 | ctx->stats.t_queue = -1; |
| 2973 | ctx->stats.t_waiting = -1; |
| 2974 | ctx->stats.t_response = -1; |
| 2975 | ctx->stats.t_process = -1; |
| 2976 | ctx->stats.t_total = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | |
| 2980 | /*************************************************************************** |
| 2981 | * Hooks that manage the filter lifecycle (init/check/deinit) |
| 2982 | **************************************************************************/ |
| 2983 | /* Signal handler: Do a soft stop, wakeup SPOE applet */ |
| 2984 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2985 | spoe_sig_stop(struct sig_handler *sh) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2986 | { |
| 2987 | struct proxy *p; |
| 2988 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 2989 | p = proxies_list; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2990 | while (p) { |
| 2991 | struct flt_conf *fconf; |
| 2992 | |
| 2993 | list_for_each_entry(fconf, &p->filter_configs, list) { |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 2994 | struct spoe_config *conf; |
| 2995 | struct spoe_agent *agent; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2996 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2997 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2998 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 2999 | if (fconf->id != spoe_filter_id) |
| 3000 | continue; |
| 3001 | |
| 3002 | conf = fconf->conf; |
| 3003 | agent = conf->agent; |
| 3004 | |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3005 | for (i = 0; i < global.nbthread; ++i) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3006 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3007 | list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list) |
| 3008 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3009 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3010 | } |
| 3011 | } |
| 3012 | p = p->next; |
| 3013 | } |
| 3014 | } |
| 3015 | |
| 3016 | |
| 3017 | /* Initialize the SPOE filter. Returns -1 on error, else 0. */ |
| 3018 | static int |
| 3019 | spoe_init(struct proxy *px, struct flt_conf *fconf) |
| 3020 | { |
| 3021 | struct spoe_config *conf = fconf->conf; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3022 | |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3023 | /* conf->agent_fe was already initialized during the config |
| 3024 | * parsing. Finish initialization. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3025 | conf->agent_fe.last_change = now.tv_sec; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3026 | conf->agent_fe.cap = PR_CAP_FE; |
| 3027 | conf->agent_fe.mode = PR_MODE_TCP; |
| 3028 | conf->agent_fe.maxconn = 0; |
| 3029 | conf->agent_fe.options2 |= PR_O2_INDEPSTR; |
| 3030 | conf->agent_fe.conn_retries = CONN_RETRIES; |
| 3031 | conf->agent_fe.accept = frontend_accept; |
| 3032 | conf->agent_fe.srv = NULL; |
| 3033 | conf->agent_fe.timeout.client = TICK_ETERNITY; |
| 3034 | conf->agent_fe.default_target = &spoe_applet.obj_type; |
| 3035 | conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES; |
| 3036 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3037 | if (!sighandler_registered) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3038 | signal_register_fct(0, spoe_sig_stop, 0); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3039 | sighandler_registered = 1; |
| 3040 | } |
| 3041 | |
Christopher Faulet | 0029235 | 2019-01-09 15:05:10 +0100 | [diff] [blame] | 3042 | fconf->flags |= FLT_CFG_FL_HTX; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3043 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3044 | } |
| 3045 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3046 | /* Free resources allocated by the SPOE filter. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3047 | static void |
| 3048 | spoe_deinit(struct proxy *px, struct flt_conf *fconf) |
| 3049 | { |
| 3050 | struct spoe_config *conf = fconf->conf; |
| 3051 | |
| 3052 | if (conf) { |
| 3053 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3054 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3055 | spoe_release_agent(agent); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3056 | free(conf->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3057 | free(conf); |
| 3058 | } |
| 3059 | fconf->conf = NULL; |
| 3060 | } |
| 3061 | |
| 3062 | /* Check configuration of a SPOE filter for a specified proxy. |
| 3063 | * Return 1 on error, else 0. */ |
| 3064 | static int |
| 3065 | spoe_check(struct proxy *px, struct flt_conf *fconf) |
| 3066 | { |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3067 | struct flt_conf *f; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3068 | struct spoe_config *conf = fconf->conf; |
| 3069 | struct proxy *target; |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3070 | struct logsrv *logsrv; |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3071 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3072 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3073 | /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names |
| 3074 | * are uniq */ |
| 3075 | list_for_each_entry(f, &px->filter_configs, list) { |
| 3076 | struct spoe_config *c = f->conf; |
| 3077 | |
| 3078 | /* This is not an SPOE filter */ |
| 3079 | if (f->id != spoe_filter_id) |
| 3080 | continue; |
| 3081 | /* This is the current SPOE filter */ |
| 3082 | if (f == fconf) |
| 3083 | continue; |
| 3084 | |
| 3085 | /* Check engine Id. It should be uniq */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3086 | if (strcmp(conf->id, c->id) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3087 | ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n", |
| 3088 | px->id, conf->id); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3089 | return 1; |
| 3090 | } |
| 3091 | } |
| 3092 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3093 | target = proxy_be_by_name(conf->agent->b.name); |
| 3094 | if (target == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3095 | ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'" |
| 3096 | " declared at %s:%d.\n", |
| 3097 | px->id, conf->agent->b.name, conf->agent->id, |
| 3098 | conf->agent->conf.file, conf->agent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3099 | return 1; |
| 3100 | } |
| 3101 | if (target->mode != PR_MODE_TCP) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3102 | ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared" |
| 3103 | " at %s:%d does not support HTTP mode.\n", |
| 3104 | px->id, target->id, conf->agent->id, |
| 3105 | conf->agent->conf.file, conf->agent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3106 | return 1; |
| 3107 | } |
| 3108 | |
Christopher Faulet | fe26155 | 2019-03-18 13:57:42 +0100 | [diff] [blame] | 3109 | if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) { |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3110 | ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n", |
| 3111 | px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line); |
| 3112 | return 1; |
| 3113 | } |
| 3114 | for (i = 0; i < global.nbthread; ++i) { |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3115 | conf->agent->rt[i].engine_id = NULL; |
Christopher Faulet | fe26155 | 2019-03-18 13:57:42 +0100 | [diff] [blame] | 3116 | conf->agent->rt[i].frame_size = conf->agent->max_frame_size; |
| 3117 | conf->agent->rt[i].processing = 0; |
| 3118 | LIST_INIT(&conf->agent->rt[i].applets); |
| 3119 | LIST_INIT(&conf->agent->rt[i].sending_queue); |
| 3120 | LIST_INIT(&conf->agent->rt[i].waiting_queue); |
| 3121 | HA_SPIN_INIT(&conf->agent->rt[i].lock); |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3122 | } |
| 3123 | |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3124 | list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) { |
| 3125 | if (logsrv->type == LOG_TARGET_BUFFER) { |
| 3126 | struct sink *sink = sink_find(logsrv->ring_name); |
| 3127 | |
| 3128 | if (!sink || sink->type != SINK_TYPE_BUFFER) { |
| 3129 | ha_alert("Proxy %s : log server used by SPOE agent '%s' declared" |
Ilya Shipitsin | d7a988c | 2021-03-04 23:26:15 +0500 | [diff] [blame] | 3130 | " at %s:%d uses unknown ring named '%s'.\n", |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3131 | px->id, conf->agent->id, conf->agent->conf.file, |
| 3132 | conf->agent->conf.line, logsrv->ring_name); |
| 3133 | return 1; |
| 3134 | } |
| 3135 | logsrv->sink = sink; |
| 3136 | } |
| 3137 | } |
| 3138 | |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3139 | ha_free(&conf->agent->b.name); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3140 | conf->agent->b.be = target; |
| 3141 | return 0; |
| 3142 | } |
| 3143 | |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3144 | /* Initializes the SPOE filter for a proxy for a specific thread. |
| 3145 | * Returns a negative value if an error occurs. */ |
| 3146 | static int |
| 3147 | spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf) |
| 3148 | { |
| 3149 | struct spoe_config *conf = fconf->conf; |
| 3150 | struct spoe_agent *agent = conf->agent; |
| 3151 | |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3152 | agent->rt[tid].engine_id = generate_pseudo_uuid(); |
| 3153 | if (agent->rt[tid].engine_id == NULL) |
| 3154 | return -1; |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3155 | return 0; |
| 3156 | } |
| 3157 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3158 | /************************************************************************** |
| 3159 | * Hooks attached to a stream |
| 3160 | *************************************************************************/ |
| 3161 | /* Called when a filter instance is created and attach to a stream. It creates |
| 3162 | * the context that will be used to process this stream. */ |
| 3163 | static int |
| 3164 | spoe_start(struct stream *s, struct filter *filter) |
| 3165 | { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3166 | struct spoe_config *conf = FLT_CONF(filter); |
| 3167 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3168 | struct spoe_context *ctx; |
| 3169 | |
| 3170 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3171 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3172 | __FUNCTION__, s); |
| 3173 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 3174 | if ((ctx = spoe_create_context(s, filter)) == NULL) { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3175 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 3176 | " - failed to create SPOE context\n", |
| 3177 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | ccbc3fd | 2017-09-15 11:51:18 +0200 | [diff] [blame] | 3178 | __FUNCTION__, s); |
Christopher Faulet | 3b8e349 | 2018-03-26 17:20:58 +0200 | [diff] [blame] | 3179 | send_log(&conf->agent_fe, LOG_EMERG, |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3180 | "SPOE: [%s] failed to create SPOE context\n", |
| 3181 | agent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3182 | return 0; |
| 3183 | } |
| 3184 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3185 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3186 | filter->pre_analyzers |= AN_REQ_INSPECT_FE; |
| 3187 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3188 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3189 | filter->pre_analyzers |= AN_REQ_INSPECT_BE; |
| 3190 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3191 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3192 | filter->pre_analyzers |= AN_RES_INSPECT; |
| 3193 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3194 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3195 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE; |
| 3196 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3197 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3198 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE; |
| 3199 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3200 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3201 | filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE; |
| 3202 | |
| 3203 | return 1; |
| 3204 | } |
| 3205 | |
| 3206 | /* Called when a filter instance is detached from a stream. It release the |
| 3207 | * attached SPOE context. */ |
| 3208 | static void |
| 3209 | spoe_stop(struct stream *s, struct filter *filter) |
| 3210 | { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3211 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
| 3212 | (int)now.tv_sec, (int)now.tv_usec, |
| 3213 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3214 | __FUNCTION__, s); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 3215 | spoe_destroy_context(filter); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3216 | } |
| 3217 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3218 | |
| 3219 | /* |
| 3220 | * Called when the stream is woken up because of expired timer. |
| 3221 | */ |
| 3222 | static void |
| 3223 | spoe_check_timeouts(struct stream *s, struct filter *filter) |
| 3224 | { |
| 3225 | struct spoe_context *ctx = filter->ctx; |
| 3226 | |
Christopher Faulet | ac58060 | 2018-03-20 16:09:20 +0100 | [diff] [blame] | 3227 | if (tick_is_expired(ctx->process_exp, now_ms)) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3228 | s->pending_events |= TASK_WOKEN_MSG; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3229 | } |
| 3230 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3231 | /* Called when we are ready to filter data on a channel */ |
| 3232 | static int |
| 3233 | spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 3234 | { |
| 3235 | struct spoe_context *ctx = filter->ctx; |
| 3236 | int ret = 1; |
| 3237 | |
| 3238 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3239 | " - ctx-flags=0x%08x\n", |
| 3240 | (int)now.tv_sec, (int)now.tv_usec, |
| 3241 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3242 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 3243 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3244 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 3245 | goto out; |
| 3246 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3247 | if (!(chn->flags & CF_ISRESP)) { |
| 3248 | if (filter->pre_analyzers & AN_REQ_INSPECT_FE) |
| 3249 | chn->analysers |= AN_REQ_INSPECT_FE; |
| 3250 | if (filter->pre_analyzers & AN_REQ_INSPECT_BE) |
| 3251 | chn->analysers |= AN_REQ_INSPECT_BE; |
| 3252 | |
| 3253 | if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED) |
| 3254 | goto out; |
| 3255 | |
| 3256 | ctx->stream_id = s->uniq_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3257 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3258 | if (!ret) |
| 3259 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3260 | ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED; |
| 3261 | } |
| 3262 | else { |
Miroslav Zagorac | 8840326 | 2020-06-19 22:17:17 +0200 | [diff] [blame] | 3263 | if (filter->pre_analyzers & AN_RES_INSPECT) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3264 | chn->analysers |= AN_RES_INSPECT; |
| 3265 | |
| 3266 | if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED) |
| 3267 | goto out; |
| 3268 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3269 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3270 | if (!ret) { |
| 3271 | channel_dont_read(chn); |
| 3272 | channel_dont_close(chn); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3273 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3274 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3275 | ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | out: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3279 | return ret; |
| 3280 | } |
| 3281 | |
| 3282 | /* Called before a processing happens on a given channel */ |
| 3283 | static int |
| 3284 | spoe_chn_pre_analyze(struct stream *s, struct filter *filter, |
| 3285 | struct channel *chn, unsigned an_bit) |
| 3286 | { |
| 3287 | struct spoe_context *ctx = filter->ctx; |
| 3288 | int ret = 1; |
| 3289 | |
| 3290 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3291 | " - ctx-flags=0x%08x - ana=0x%08x\n", |
| 3292 | (int)now.tv_sec, (int)now.tv_usec, |
| 3293 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3294 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 3295 | ctx->flags, an_bit); |
| 3296 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3297 | if (ctx->state == SPOE_CTX_ST_NONE) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3298 | goto out; |
| 3299 | |
| 3300 | switch (an_bit) { |
| 3301 | case AN_REQ_INSPECT_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3302 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3303 | break; |
| 3304 | case AN_REQ_INSPECT_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3305 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3306 | break; |
| 3307 | case AN_RES_INSPECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3308 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3309 | break; |
| 3310 | case AN_REQ_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3311 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3312 | break; |
| 3313 | case AN_REQ_HTTP_PROCESS_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3314 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3315 | break; |
| 3316 | case AN_RES_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3317 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3318 | break; |
| 3319 | } |
| 3320 | |
| 3321 | out: |
Christopher Faulet | 9cdca97 | 2018-02-01 08:45:45 +0100 | [diff] [blame] | 3322 | if (!ret && (chn->flags & CF_ISRESP)) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3323 | channel_dont_read(chn); |
| 3324 | channel_dont_close(chn); |
| 3325 | } |
| 3326 | return ret; |
| 3327 | } |
| 3328 | |
| 3329 | /* Called when the filtering on the channel ends. */ |
| 3330 | static int |
| 3331 | spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 3332 | { |
| 3333 | struct spoe_context *ctx = filter->ctx; |
| 3334 | |
| 3335 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3336 | " - ctx-flags=0x%08x\n", |
| 3337 | (int)now.tv_sec, (int)now.tv_usec, |
| 3338 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3339 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 3340 | |
| 3341 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3342 | spoe_reset_context(ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | return 1; |
| 3346 | } |
| 3347 | |
| 3348 | /******************************************************************** |
| 3349 | * Functions that manage the filter initialization |
| 3350 | ********************************************************************/ |
| 3351 | struct flt_ops spoe_ops = { |
| 3352 | /* Manage SPOE filter, called for each filter declaration */ |
| 3353 | .init = spoe_init, |
| 3354 | .deinit = spoe_deinit, |
| 3355 | .check = spoe_check, |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3356 | .init_per_thread = spoe_init_per_thread, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3357 | |
| 3358 | /* Handle start/stop of SPOE */ |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3359 | .attach = spoe_start, |
| 3360 | .detach = spoe_stop, |
| 3361 | .check_timeouts = spoe_check_timeouts, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3362 | |
| 3363 | /* Handle channels activity */ |
| 3364 | .channel_start_analyze = spoe_start_analyze, |
| 3365 | .channel_pre_analyze = spoe_chn_pre_analyze, |
| 3366 | .channel_end_analyze = spoe_end_analyze, |
| 3367 | }; |
| 3368 | |
| 3369 | |
| 3370 | static int |
| 3371 | cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm) |
| 3372 | { |
| 3373 | const char *err; |
| 3374 | int i, err_code = 0; |
| 3375 | |
| 3376 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3377 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3378 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3379 | goto out; |
| 3380 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3381 | if (strcmp(args[0], "spoe-agent") == 0) { /* new spoe-agent section */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3382 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3383 | ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n", |
| 3384 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3385 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3386 | goto out; |
| 3387 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3388 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3389 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3390 | goto out; |
| 3391 | } |
| 3392 | |
| 3393 | err = invalid_char(args[1]); |
| 3394 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3395 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3396 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3397 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3398 | goto out; |
| 3399 | } |
| 3400 | |
| 3401 | if (curagent != NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3402 | ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n", |
| 3403 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3404 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3405 | goto out; |
| 3406 | } |
| 3407 | if ((curagent = calloc(1, sizeof(*curagent))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3408 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3409 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3410 | goto out; |
| 3411 | } |
| 3412 | |
| 3413 | curagent->id = strdup(args[1]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3414 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3415 | curagent->conf.file = strdup(file); |
| 3416 | curagent->conf.line = linenum; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3417 | |
| 3418 | curagent->timeout.hello = TICK_ETERNITY; |
| 3419 | curagent->timeout.idle = TICK_ETERNITY; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3420 | curagent->timeout.processing = TICK_ETERNITY; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3421 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3422 | curagent->var_pfx = NULL; |
| 3423 | curagent->var_on_error = NULL; |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3424 | curagent->var_t_process = NULL; |
| 3425 | curagent->var_t_total = NULL; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3426 | curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3427 | curagent->cps_max = 0; |
| 3428 | curagent->eps_max = 0; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 3429 | curagent->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 3430 | curagent->max_fpa = 20; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3431 | |
| 3432 | for (i = 0; i < SPOE_EV_EVENTS; ++i) |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3433 | LIST_INIT(&curagent->events[i]); |
| 3434 | LIST_INIT(&curagent->groups); |
| 3435 | LIST_INIT(&curagent->messages); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3436 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3437 | else if (strcmp(args[0], "use-backend") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3438 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3439 | ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n", |
| 3440 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3441 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3442 | goto out; |
| 3443 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3444 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3445 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3446 | free(curagent->b.name); |
| 3447 | curagent->b.name = strdup(args[1]); |
| 3448 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3449 | else if (strcmp(args[0], "messages") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3450 | int cur_arg = 1; |
| 3451 | while (*args[cur_arg]) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3452 | struct spoe_placeholder *ph = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3453 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3454 | list_for_each_entry(ph, &curmphs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3455 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3456 | ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3457 | file, linenum, args[cur_arg]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3458 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3459 | goto out; |
| 3460 | } |
| 3461 | } |
| 3462 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3463 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3464 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3465 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3466 | goto out; |
| 3467 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3468 | ph->id = strdup(args[cur_arg]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3469 | LIST_APPEND(&curmphs, &ph->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3470 | cur_arg++; |
| 3471 | } |
| 3472 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3473 | else if (strcmp(args[0], "groups") == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3474 | int cur_arg = 1; |
| 3475 | while (*args[cur_arg]) { |
| 3476 | struct spoe_placeholder *ph = NULL; |
| 3477 | |
| 3478 | list_for_each_entry(ph, &curgphs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3479 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3480 | ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n", |
| 3481 | file, linenum, args[cur_arg]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3482 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3483 | goto out; |
| 3484 | } |
| 3485 | } |
| 3486 | |
| 3487 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3488 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3489 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3490 | goto out; |
| 3491 | } |
| 3492 | ph->id = strdup(args[cur_arg]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3493 | LIST_APPEND(&curgphs, &ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3494 | cur_arg++; |
| 3495 | } |
| 3496 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3497 | else if (strcmp(args[0], "timeout") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3498 | unsigned int *tv = NULL; |
| 3499 | const char *res; |
| 3500 | unsigned timeout; |
| 3501 | |
| 3502 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3503 | ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n", |
| 3504 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3505 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3506 | goto out; |
| 3507 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3508 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3509 | goto out; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3510 | if (strcmp(args[1], "hello") == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3511 | tv = &curagent->timeout.hello; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3512 | else if (strcmp(args[1], "idle") == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3513 | tv = &curagent->timeout.idle; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3514 | else if (strcmp(args[1], "processing") == 0) |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3515 | tv = &curagent->timeout.processing; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3516 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3517 | ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n", |
| 3518 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3519 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3520 | goto out; |
| 3521 | } |
| 3522 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3523 | ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n", |
| 3524 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3525 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3526 | goto out; |
| 3527 | } |
| 3528 | res = parse_time_err(args[2], &timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 3529 | if (res == PARSE_TIME_OVER) { |
| 3530 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n", |
| 3531 | file, linenum, args[2], args[0], args[1]); |
| 3532 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3533 | goto out; |
| 3534 | } |
| 3535 | else if (res == PARSE_TIME_UNDER) { |
| 3536 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n", |
| 3537 | file, linenum, args[2], args[0], args[1]); |
| 3538 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3539 | goto out; |
| 3540 | } |
| 3541 | else if (res) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3542 | ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n", |
| 3543 | file, linenum, *res, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3544 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3545 | goto out; |
| 3546 | } |
| 3547 | *tv = MS_TO_TICKS(timeout); |
| 3548 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3549 | else if (strcmp(args[0], "option") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3550 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3551 | ha_alert("parsing [%s:%d]: '%s' expects an option name.\n", |
| 3552 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3553 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3554 | goto out; |
| 3555 | } |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3556 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3557 | if (strcmp(args[1], "pipelining") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3558 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3559 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3560 | if (kwm == 1) |
| 3561 | curagent->flags &= ~SPOE_FL_PIPELINING; |
| 3562 | else |
| 3563 | curagent->flags |= SPOE_FL_PIPELINING; |
| 3564 | goto out; |
| 3565 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3566 | else if (strcmp(args[1], "async") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3567 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3568 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3569 | if (kwm == 1) |
| 3570 | curagent->flags &= ~SPOE_FL_ASYNC; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3571 | else |
| 3572 | curagent->flags |= SPOE_FL_ASYNC; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3573 | goto out; |
| 3574 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3575 | else if (strcmp(args[1], "send-frag-payload") == 0) { |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 3576 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3577 | goto out; |
| 3578 | if (kwm == 1) |
| 3579 | curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION; |
| 3580 | else |
| 3581 | curagent->flags |= SPOE_FL_SND_FRAGMENTATION; |
| 3582 | goto out; |
| 3583 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3584 | else if (strcmp(args[1], "dontlog-normal") == 0) { |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 3585 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3586 | goto out; |
| 3587 | if (kwm == 1) |
| 3588 | curpxopts2 &= ~PR_O2_NOLOGNORM; |
| 3589 | else |
| 3590 | curpxopts2 |= PR_O2_NOLOGNORM; |
Christopher Faulet | 799f518 | 2018-04-26 11:36:34 +0200 | [diff] [blame] | 3591 | goto out; |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 3592 | } |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3593 | |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3594 | /* Following options does not support negation */ |
| 3595 | if (kwm == 1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3596 | ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n", |
| 3597 | file, linenum, args[1]); |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3598 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3599 | goto out; |
| 3600 | } |
| 3601 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3602 | if (strcmp(args[1], "var-prefix") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3603 | char *tmp; |
| 3604 | |
| 3605 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3606 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3607 | file, linenum, args[0], |
| 3608 | args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3609 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3610 | goto out; |
| 3611 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3612 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3613 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3614 | tmp = args[2]; |
| 3615 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3616 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3617 | 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] | 3618 | file, linenum, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3619 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3620 | goto out; |
| 3621 | } |
| 3622 | tmp++; |
| 3623 | } |
| 3624 | curagent->var_pfx = strdup(args[2]); |
| 3625 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3626 | else if (strcmp(args[1], "force-set-var") == 0) { |
Etienne Carriere | aec8989 | 2017-12-14 09:36:40 +0000 | [diff] [blame] | 3627 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3628 | goto out; |
| 3629 | curagent->flags |= SPOE_FL_FORCE_SET_VAR; |
| 3630 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3631 | else if (strcmp(args[1], "continue-on-error") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3632 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3633 | goto out; |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3634 | curagent->flags |= SPOE_FL_CONT_ON_ERR; |
| 3635 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3636 | else if (strcmp(args[1], "set-on-error") == 0) { |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3637 | char *tmp; |
| 3638 | |
| 3639 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3640 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3641 | file, linenum, args[0], |
| 3642 | args[1]); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3643 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3644 | goto out; |
| 3645 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3646 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3647 | goto out; |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3648 | tmp = args[2]; |
| 3649 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3650 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3651 | 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] | 3652 | file, linenum, args[0], args[1]); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3653 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3654 | goto out; |
| 3655 | } |
| 3656 | tmp++; |
| 3657 | } |
| 3658 | curagent->var_on_error = strdup(args[2]); |
| 3659 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3660 | else if (strcmp(args[1], "set-process-time") == 0) { |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3661 | char *tmp; |
| 3662 | |
| 3663 | if (!*args[2]) { |
| 3664 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3665 | file, linenum, args[0], |
| 3666 | args[1]); |
| 3667 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3668 | goto out; |
| 3669 | } |
| 3670 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3671 | goto out; |
| 3672 | tmp = args[2]; |
| 3673 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3674 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3675 | 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] | 3676 | file, linenum, args[0], args[1]); |
| 3677 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3678 | goto out; |
| 3679 | } |
| 3680 | tmp++; |
| 3681 | } |
| 3682 | curagent->var_t_process = strdup(args[2]); |
| 3683 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3684 | else if (strcmp(args[1], "set-total-time") == 0) { |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3685 | char *tmp; |
| 3686 | |
| 3687 | if (!*args[2]) { |
| 3688 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3689 | file, linenum, args[0], |
| 3690 | args[1]); |
| 3691 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3692 | goto out; |
| 3693 | } |
| 3694 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3695 | goto out; |
| 3696 | tmp = args[2]; |
| 3697 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3698 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3699 | 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] | 3700 | file, linenum, args[0], args[1]); |
| 3701 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3702 | goto out; |
| 3703 | } |
| 3704 | tmp++; |
| 3705 | } |
| 3706 | curagent->var_t_total = strdup(args[2]); |
| 3707 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3708 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3709 | ha_alert("parsing [%s:%d]: option '%s' is not supported.\n", |
| 3710 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3711 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3712 | goto out; |
| 3713 | } |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3714 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3715 | else if (strcmp(args[0], "maxconnrate") == 0) { |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3716 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3717 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3718 | file, linenum, args[0]); |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3719 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3720 | goto out; |
| 3721 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3722 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3723 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3724 | curagent->cps_max = atol(args[1]); |
| 3725 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3726 | else if (strcmp(args[0], "maxerrrate") == 0) { |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3727 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3728 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3729 | file, linenum, args[0]); |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3730 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3731 | goto out; |
| 3732 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3733 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3734 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3735 | curagent->eps_max = atol(args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3736 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3737 | else if (strcmp(args[0], "max-frame-size") == 0) { |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3738 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3739 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3740 | file, linenum, args[0]); |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3741 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3742 | goto out; |
| 3743 | } |
| 3744 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3745 | goto out; |
| 3746 | curagent->max_frame_size = atol(args[1]); |
| 3747 | if (curagent->max_frame_size < MIN_FRAME_SIZE || |
| 3748 | curagent->max_frame_size > MAX_FRAME_SIZE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3749 | ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n", |
| 3750 | file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE); |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3751 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3752 | goto out; |
| 3753 | } |
| 3754 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3755 | else if (strcmp(args[0], "max-waiting-frames") == 0) { |
Christopher Faulet | e8ade38 | 2018-01-25 15:32:22 +0100 | [diff] [blame] | 3756 | if (!*args[1]) { |
| 3757 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3758 | file, linenum, args[0]); |
| 3759 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3760 | goto out; |
| 3761 | } |
| 3762 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3763 | goto out; |
| 3764 | curagent->max_fpa = atol(args[1]); |
| 3765 | if (curagent->max_fpa < 1) { |
| 3766 | ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n", |
| 3767 | file, linenum, args[0]); |
| 3768 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3769 | goto out; |
| 3770 | } |
| 3771 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3772 | else if (strcmp(args[0], "register-var-names") == 0) { |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3773 | int cur_arg; |
| 3774 | |
| 3775 | if (!*args[1]) { |
| 3776 | ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n", |
| 3777 | file, linenum, args[0]); |
| 3778 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3779 | goto out; |
| 3780 | } |
| 3781 | cur_arg = 1; |
| 3782 | while (*args[cur_arg]) { |
| 3783 | struct spoe_var_placeholder *vph; |
| 3784 | |
| 3785 | if ((vph = calloc(1, sizeof(*vph))) == NULL) { |
| 3786 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3787 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3788 | goto out; |
| 3789 | } |
| 3790 | if ((vph->name = strdup(args[cur_arg])) == NULL) { |
Tim Duesterhus | b298613 | 2019-06-23 22:10:13 +0200 | [diff] [blame] | 3791 | free(vph); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3792 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3793 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3794 | goto out; |
| 3795 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3796 | LIST_APPEND(&curvars, &vph->list); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3797 | cur_arg++; |
| 3798 | } |
| 3799 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3800 | else if (strcmp(args[0], "log") == 0) { |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3801 | char *errmsg = NULL; |
| 3802 | |
Emeric Brun | 9533a70 | 2021-04-02 10:13:43 +0200 | [diff] [blame] | 3803 | if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), file, linenum, &errmsg)) { |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3804 | ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); |
| 3805 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3806 | goto out; |
| 3807 | } |
| 3808 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3809 | else if (*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3810 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n", |
| 3811 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3812 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3813 | goto out; |
| 3814 | } |
| 3815 | out: |
| 3816 | return err_code; |
| 3817 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3818 | static int |
| 3819 | cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm) |
| 3820 | { |
| 3821 | struct spoe_group *grp; |
| 3822 | const char *err; |
| 3823 | int err_code = 0; |
| 3824 | |
| 3825 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3826 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3827 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3828 | goto out; |
| 3829 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3830 | if (strcmp(args[0], "spoe-group") == 0) { /* new spoe-group section */ |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3831 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3832 | ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n", |
| 3833 | file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3834 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3835 | goto out; |
| 3836 | } |
| 3837 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3838 | err_code |= ERR_ABORT; |
| 3839 | goto out; |
| 3840 | } |
| 3841 | |
| 3842 | err = invalid_char(args[1]); |
| 3843 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3844 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3845 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3846 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3847 | goto out; |
| 3848 | } |
| 3849 | |
| 3850 | list_for_each_entry(grp, &curgrps, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3851 | if (strcmp(grp->id, args[1]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3852 | ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same" |
| 3853 | " name as another one declared at %s:%d.\n", |
| 3854 | file, linenum, args[1], grp->conf.file, grp->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3855 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3856 | goto out; |
| 3857 | } |
| 3858 | } |
| 3859 | |
| 3860 | if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3861 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3862 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3863 | goto out; |
| 3864 | } |
| 3865 | |
| 3866 | curgrp->id = strdup(args[1]); |
| 3867 | curgrp->conf.file = strdup(file); |
| 3868 | curgrp->conf.line = linenum; |
| 3869 | LIST_INIT(&curgrp->phs); |
| 3870 | LIST_INIT(&curgrp->messages); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3871 | LIST_APPEND(&curgrps, &curgrp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3872 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3873 | else if (strcmp(args[0], "messages") == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3874 | int cur_arg = 1; |
| 3875 | while (*args[cur_arg]) { |
| 3876 | struct spoe_placeholder *ph = NULL; |
| 3877 | |
| 3878 | list_for_each_entry(ph, &curgrp->phs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3879 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3880 | ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3881 | file, linenum, args[cur_arg]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3882 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3883 | goto out; |
| 3884 | } |
| 3885 | } |
| 3886 | |
| 3887 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3888 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3889 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3890 | goto out; |
| 3891 | } |
| 3892 | ph->id = strdup(args[cur_arg]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3893 | LIST_APPEND(&curgrp->phs, &ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3894 | cur_arg++; |
| 3895 | } |
| 3896 | } |
| 3897 | else if (*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3898 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n", |
| 3899 | file, linenum, args[0]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3900 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3901 | goto out; |
| 3902 | } |
| 3903 | out: |
| 3904 | return err_code; |
| 3905 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3906 | |
| 3907 | static int |
| 3908 | cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm) |
| 3909 | { |
| 3910 | struct spoe_message *msg; |
| 3911 | struct spoe_arg *arg; |
| 3912 | const char *err; |
| 3913 | char *errmsg = NULL; |
| 3914 | int err_code = 0; |
| 3915 | |
| 3916 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3917 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3918 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3919 | goto out; |
| 3920 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3921 | if (strcmp(args[0], "spoe-message") == 0) { /* new spoe-message section */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3922 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3923 | ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n", |
| 3924 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3925 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3926 | goto out; |
| 3927 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3928 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3929 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3930 | goto out; |
| 3931 | } |
| 3932 | |
| 3933 | err = invalid_char(args[1]); |
| 3934 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3935 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3936 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3937 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3938 | goto out; |
| 3939 | } |
| 3940 | |
| 3941 | list_for_each_entry(msg, &curmsgs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3942 | if (strcmp(msg->id, args[1]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3943 | ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same" |
| 3944 | " name as another one declared at %s:%d.\n", |
| 3945 | file, linenum, args[1], msg->conf.file, msg->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3946 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3947 | goto out; |
| 3948 | } |
| 3949 | } |
| 3950 | |
| 3951 | if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3952 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3953 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3954 | goto out; |
| 3955 | } |
| 3956 | |
| 3957 | curmsg->id = strdup(args[1]); |
| 3958 | curmsg->id_len = strlen(curmsg->id); |
| 3959 | curmsg->event = SPOE_EV_NONE; |
| 3960 | curmsg->conf.file = strdup(file); |
| 3961 | curmsg->conf.line = linenum; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 3962 | curmsg->nargs = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3963 | LIST_INIT(&curmsg->args); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3964 | LIST_INIT(&curmsg->acls); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3965 | LIST_INIT(&curmsg->by_evt); |
| 3966 | LIST_INIT(&curmsg->by_grp); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3967 | LIST_APPEND(&curmsgs, &curmsg->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3968 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3969 | else if (strcmp(args[0], "args") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3970 | int cur_arg = 1; |
| 3971 | |
| 3972 | curproxy->conf.args.ctx = ARGC_SPOE; |
| 3973 | curproxy->conf.args.file = file; |
| 3974 | curproxy->conf.args.line = linenum; |
| 3975 | while (*args[cur_arg]) { |
| 3976 | char *delim = strchr(args[cur_arg], '='); |
| 3977 | int idx = 0; |
| 3978 | |
| 3979 | if ((arg = calloc(1, sizeof(*arg))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3980 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3981 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3982 | goto out; |
| 3983 | } |
| 3984 | |
| 3985 | if (!delim) { |
| 3986 | arg->name = NULL; |
| 3987 | arg->name_len = 0; |
| 3988 | delim = args[cur_arg]; |
| 3989 | } |
| 3990 | else { |
| 3991 | arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]); |
| 3992 | arg->name_len = delim - args[cur_arg]; |
| 3993 | delim++; |
| 3994 | } |
Christopher Faulet | b0b4238 | 2017-02-23 22:41:09 +0100 | [diff] [blame] | 3995 | arg->expr = sample_parse_expr((char*[]){delim, NULL}, |
| 3996 | &idx, file, linenum, &errmsg, |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 3997 | &curproxy->conf.args, NULL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3998 | if (arg->expr == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3999 | 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] | 4000 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4001 | free(arg->name); |
| 4002 | free(arg); |
| 4003 | goto out; |
| 4004 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 4005 | curmsg->nargs++; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4006 | LIST_APPEND(&curmsg->args, &arg->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4007 | cur_arg++; |
| 4008 | } |
| 4009 | curproxy->conf.args.file = NULL; |
| 4010 | curproxy->conf.args.line = 0; |
| 4011 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4012 | else if (strcmp(args[0], "acl") == 0) { |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4013 | err = invalid_char(args[1]); |
| 4014 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4015 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n", |
| 4016 | file, linenum, *err, args[1]); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4017 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4018 | goto out; |
| 4019 | } |
Tim Duesterhus | 0cf811a | 2020-02-05 21:00:50 +0100 | [diff] [blame] | 4020 | if (strcasecmp(args[1], "or") == 0) { |
Tim Duesterhus | f1bc24c | 2020-02-06 22:04:03 +0100 | [diff] [blame] | 4021 | 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] | 4022 | "logical disjunction within a condition.\n", |
| 4023 | file, linenum, args[1]); |
| 4024 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4025 | goto out; |
| 4026 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4027 | 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] | 4028 | ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n", |
| 4029 | file, linenum, args[1], errmsg); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4030 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4031 | goto out; |
| 4032 | } |
| 4033 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4034 | else if (strcmp(args[0], "event") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4035 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4036 | ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4037 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4038 | goto out; |
| 4039 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4040 | /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */ |
| 4041 | /* goto out; */ |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4042 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4043 | 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] | 4044 | curmsg->event = SPOE_EV_ON_CLIENT_SESS; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4045 | 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] | 4046 | curmsg->event = SPOE_EV_ON_SERVER_SESS; |
| 4047 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4048 | 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] | 4049 | curmsg->event = SPOE_EV_ON_TCP_REQ_FE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4050 | 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] | 4051 | curmsg->event = SPOE_EV_ON_TCP_REQ_BE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4052 | 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] | 4053 | curmsg->event = SPOE_EV_ON_TCP_RSP; |
| 4054 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4055 | 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] | 4056 | curmsg->event = SPOE_EV_ON_HTTP_REQ_FE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4057 | 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] | 4058 | curmsg->event = SPOE_EV_ON_HTTP_REQ_BE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4059 | 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] | 4060 | curmsg->event = SPOE_EV_ON_HTTP_RSP; |
| 4061 | else { |
Joseph Herlant | f1da69d | 2018-11-15 13:49:02 -0800 | [diff] [blame] | 4062 | ha_alert("parsing [%s:%d] : unknown event '%s'.\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4063 | file, linenum, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4064 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4065 | goto out; |
| 4066 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4067 | |
| 4068 | if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) { |
| 4069 | struct acl_cond *cond; |
| 4070 | |
| 4071 | cond = build_acl_cond(file, linenum, &curmsg->acls, |
| 4072 | curproxy, (const char **)args+2, |
| 4073 | &errmsg); |
| 4074 | if (cond == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4075 | ha_alert("parsing [%s:%d] : error detected while " |
| 4076 | "parsing an 'event %s' condition : %s.\n", |
| 4077 | file, linenum, args[1], errmsg); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4078 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4079 | goto out; |
| 4080 | } |
| 4081 | curmsg->cond = cond; |
| 4082 | } |
| 4083 | else if (*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4084 | ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' " |
| 4085 | "or 'unless' followed by a condition but found '%s'.\n", |
| 4086 | file, linenum, args[1], args[2]); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4087 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4088 | goto out; |
| 4089 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4090 | } |
| 4091 | else if (!*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4092 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n", |
| 4093 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4094 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4095 | goto out; |
| 4096 | } |
| 4097 | out: |
| 4098 | free(errmsg); |
| 4099 | return err_code; |
| 4100 | } |
| 4101 | |
| 4102 | /* Return -1 on error, else 0 */ |
| 4103 | static int |
| 4104 | parse_spoe_flt(char **args, int *cur_arg, struct proxy *px, |
| 4105 | struct flt_conf *fconf, char **err, void *private) |
| 4106 | { |
| 4107 | struct list backup_sections; |
| 4108 | struct spoe_config *conf; |
| 4109 | struct spoe_message *msg, *msgback; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4110 | struct spoe_group *grp, *grpback; |
| 4111 | struct spoe_placeholder *ph, *phback; |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4112 | struct spoe_var_placeholder *vph, *vphback; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4113 | struct logsrv *logsrv, *logsrvback; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4114 | char *file = NULL, *engine = NULL; |
| 4115 | int ret, pos = *cur_arg + 1; |
| 4116 | |
Christopher Faulet | 84c844e | 2018-03-23 14:37:14 +0100 | [diff] [blame] | 4117 | LIST_INIT(&curmsgs); |
| 4118 | LIST_INIT(&curgrps); |
| 4119 | LIST_INIT(&curmphs); |
| 4120 | LIST_INIT(&curgphs); |
| 4121 | LIST_INIT(&curvars); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4122 | LIST_INIT(&curlogsrvs); |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 4123 | curpxopts = 0; |
| 4124 | curpxopts2 = 0; |
Christopher Faulet | 84c844e | 2018-03-23 14:37:14 +0100 | [diff] [blame] | 4125 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4126 | conf = calloc(1, sizeof(*conf)); |
| 4127 | if (conf == NULL) { |
| 4128 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 4129 | goto error; |
| 4130 | } |
| 4131 | conf->proxy = px; |
| 4132 | |
| 4133 | while (*args[pos]) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4134 | if (strcmp(args[pos], "config") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4135 | if (!*args[pos+1]) { |
| 4136 | memprintf(err, "'%s' : '%s' option without value", |
| 4137 | args[*cur_arg], args[pos]); |
| 4138 | goto error; |
| 4139 | } |
| 4140 | file = args[pos+1]; |
| 4141 | pos += 2; |
| 4142 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4143 | else if (strcmp(args[pos], "engine") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4144 | if (!*args[pos+1]) { |
| 4145 | memprintf(err, "'%s' : '%s' option without value", |
| 4146 | args[*cur_arg], args[pos]); |
| 4147 | goto error; |
| 4148 | } |
| 4149 | engine = args[pos+1]; |
| 4150 | pos += 2; |
| 4151 | } |
| 4152 | else { |
| 4153 | memprintf(err, "unknown keyword '%s'", args[pos]); |
| 4154 | goto error; |
| 4155 | } |
| 4156 | } |
| 4157 | if (file == NULL) { |
| 4158 | memprintf(err, "'%s' : missing config file", args[*cur_arg]); |
| 4159 | goto error; |
| 4160 | } |
| 4161 | |
| 4162 | /* backup sections and register SPOE sections */ |
| 4163 | LIST_INIT(&backup_sections); |
| 4164 | cfg_backup_sections(&backup_sections); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4165 | cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL); |
| 4166 | cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL); |
William Lallemand | d2ff56d | 2017-10-16 11:06:50 +0200 | [diff] [blame] | 4167 | cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4168 | |
| 4169 | /* Parse SPOE filter configuration file */ |
| 4170 | curengine = engine; |
| 4171 | curproxy = px; |
| 4172 | curagent = NULL; |
| 4173 | curmsg = NULL; |
| 4174 | ret = readcfgfile(file); |
| 4175 | curproxy = NULL; |
| 4176 | |
| 4177 | /* unregister SPOE sections and restore previous sections */ |
| 4178 | cfg_unregister_sections(); |
| 4179 | cfg_restore_sections(&backup_sections); |
| 4180 | |
| 4181 | if (ret == -1) { |
| 4182 | memprintf(err, "Could not open configuration file %s : %s", |
| 4183 | file, strerror(errno)); |
| 4184 | goto error; |
| 4185 | } |
| 4186 | if (ret & (ERR_ABORT|ERR_FATAL)) { |
| 4187 | memprintf(err, "Error(s) found in configuration file %s", file); |
| 4188 | goto error; |
| 4189 | } |
| 4190 | |
| 4191 | /* Check SPOE agent */ |
| 4192 | if (curagent == NULL) { |
| 4193 | memprintf(err, "No SPOE agent found in file %s", file); |
| 4194 | goto error; |
| 4195 | } |
| 4196 | if (curagent->b.name == NULL) { |
| 4197 | memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d", |
| 4198 | curagent->id, curagent->conf.file, curagent->conf.line); |
| 4199 | goto error; |
| 4200 | } |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 4201 | if (curagent->timeout.hello == TICK_ETERNITY || |
| 4202 | curagent->timeout.idle == TICK_ETERNITY || |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 4203 | curagent->timeout.processing == TICK_ETERNITY) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4204 | ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n" |
| 4205 | " | While not properly invalid, you will certainly encounter various problems\n" |
| 4206 | " | with such a configuration. To fix this, please ensure that all following\n" |
| 4207 | " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n", |
| 4208 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4209 | } |
| 4210 | if (curagent->var_pfx == NULL) { |
| 4211 | char *tmp = curagent->id; |
| 4212 | |
| 4213 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4214 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4215 | memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. " |
| 4216 | "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n", |
| 4217 | curagent->id, curagent->id, curagent->conf.file, curagent->conf.line); |
| 4218 | goto error; |
| 4219 | } |
| 4220 | tmp++; |
| 4221 | } |
| 4222 | curagent->var_pfx = strdup(curagent->id); |
| 4223 | } |
| 4224 | |
Christopher Faulet | b7426d1 | 2018-03-21 14:12:17 +0100 | [diff] [blame] | 4225 | if (curagent->var_on_error) { |
| 4226 | struct arg arg; |
| 4227 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4228 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | b7426d1 | 2018-03-21 14:12:17 +0100 | [diff] [blame] | 4229 | curagent->var_pfx, curagent->var_on_error); |
| 4230 | |
| 4231 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4232 | arg.data.str.area = trash.area; |
| 4233 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4234 | 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] | 4235 | if (!vars_check_arg(&arg, err)) { |
| 4236 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4237 | curagent->id, curagent->var_pfx, curagent->var_on_error, *err); |
| 4238 | goto error; |
| 4239 | } |
| 4240 | } |
| 4241 | |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4242 | if (curagent->var_t_process) { |
| 4243 | struct arg arg; |
| 4244 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4245 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4246 | curagent->var_pfx, curagent->var_t_process); |
| 4247 | |
| 4248 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4249 | arg.data.str.area = trash.area; |
| 4250 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4251 | 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] | 4252 | if (!vars_check_arg(&arg, err)) { |
| 4253 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4254 | curagent->id, curagent->var_pfx, curagent->var_t_process, *err); |
| 4255 | goto error; |
| 4256 | } |
| 4257 | } |
| 4258 | |
| 4259 | if (curagent->var_t_total) { |
| 4260 | struct arg arg; |
| 4261 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4262 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4263 | curagent->var_pfx, curagent->var_t_total); |
| 4264 | |
| 4265 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4266 | arg.data.str.area = trash.area; |
| 4267 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4268 | 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] | 4269 | if (!vars_check_arg(&arg, err)) { |
| 4270 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4271 | curagent->id, curagent->var_pfx, curagent->var_t_process, *err); |
| 4272 | goto error; |
| 4273 | } |
| 4274 | } |
| 4275 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4276 | if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4277 | ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n", |
| 4278 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4279 | goto finish; |
| 4280 | } |
| 4281 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4282 | /* Replace placeholders by the corresponding messages for the SPOE |
| 4283 | * agent */ |
| 4284 | list_for_each_entry(ph, &curmphs, list) { |
| 4285 | list_for_each_entry(msg, &curmsgs, list) { |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4286 | struct spoe_arg *arg; |
| 4287 | unsigned int where; |
| 4288 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4289 | if (strcmp(msg->id, ph->id) == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4290 | if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) { |
| 4291 | if (msg->event == SPOE_EV_ON_TCP_REQ_BE) |
| 4292 | msg->event = SPOE_EV_ON_TCP_REQ_FE; |
| 4293 | if (msg->event == SPOE_EV_ON_HTTP_REQ_BE) |
| 4294 | msg->event = SPOE_EV_ON_HTTP_REQ_FE; |
| 4295 | } |
| 4296 | if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS || |
| 4297 | msg->event == SPOE_EV_ON_TCP_REQ_FE || |
| 4298 | msg->event == SPOE_EV_ON_HTTP_REQ_FE)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4299 | ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n", |
| 4300 | px->id, msg->conf.file, msg->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4301 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4302 | } |
| 4303 | if (msg->event == SPOE_EV_NONE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4304 | ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n", |
| 4305 | px->id, msg->id, msg->conf.file, msg->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4306 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4307 | } |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4308 | |
| 4309 | where = 0; |
| 4310 | switch (msg->event) { |
| 4311 | case SPOE_EV_ON_CLIENT_SESS: |
| 4312 | where |= SMP_VAL_FE_CON_ACC; |
| 4313 | break; |
| 4314 | |
| 4315 | case SPOE_EV_ON_TCP_REQ_FE: |
| 4316 | where |= SMP_VAL_FE_REQ_CNT; |
| 4317 | break; |
| 4318 | |
| 4319 | case SPOE_EV_ON_HTTP_REQ_FE: |
| 4320 | where |= SMP_VAL_FE_HRQ_HDR; |
| 4321 | break; |
| 4322 | |
| 4323 | case SPOE_EV_ON_TCP_REQ_BE: |
| 4324 | if (px->cap & PR_CAP_FE) |
| 4325 | where |= SMP_VAL_FE_REQ_CNT; |
| 4326 | if (px->cap & PR_CAP_BE) |
| 4327 | where |= SMP_VAL_BE_REQ_CNT; |
| 4328 | break; |
| 4329 | |
| 4330 | case SPOE_EV_ON_HTTP_REQ_BE: |
| 4331 | if (px->cap & PR_CAP_FE) |
| 4332 | where |= SMP_VAL_FE_HRQ_HDR; |
| 4333 | if (px->cap & PR_CAP_BE) |
| 4334 | where |= SMP_VAL_BE_HRQ_HDR; |
| 4335 | break; |
| 4336 | |
| 4337 | case SPOE_EV_ON_SERVER_SESS: |
| 4338 | where |= SMP_VAL_BE_SRV_CON; |
| 4339 | break; |
| 4340 | |
| 4341 | case SPOE_EV_ON_TCP_RSP: |
| 4342 | if (px->cap & PR_CAP_FE) |
| 4343 | where |= SMP_VAL_FE_RES_CNT; |
| 4344 | if (px->cap & PR_CAP_BE) |
| 4345 | where |= SMP_VAL_BE_RES_CNT; |
| 4346 | break; |
| 4347 | |
| 4348 | case SPOE_EV_ON_HTTP_RSP: |
| 4349 | if (px->cap & PR_CAP_FE) |
| 4350 | where |= SMP_VAL_FE_HRS_HDR; |
| 4351 | if (px->cap & PR_CAP_BE) |
| 4352 | where |= SMP_VAL_BE_HRS_HDR; |
| 4353 | break; |
| 4354 | |
| 4355 | default: |
| 4356 | break; |
| 4357 | } |
| 4358 | |
| 4359 | list_for_each_entry(arg, &msg->args, list) { |
| 4360 | if (!(arg->expr->fetch->val & where)) { |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4361 | memprintf(err, "Ignore SPOE message '%s' at %s:%d: " |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4362 | "some args extract information from '%s', " |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4363 | "none of which is available here ('%s')", |
| 4364 | msg->id, msg->conf.file, msg->conf.line, |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4365 | sample_ckp_names(arg->expr->fetch->use), |
| 4366 | sample_ckp_names(where)); |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4367 | goto error; |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4368 | } |
| 4369 | } |
| 4370 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4371 | msg->agent = curagent; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4372 | LIST_APPEND(&curagent->events[msg->event], &msg->by_evt); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4373 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4374 | } |
| 4375 | } |
| 4376 | 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] | 4377 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4378 | goto error; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4379 | next_mph: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4380 | continue; |
| 4381 | } |
| 4382 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4383 | /* Replace placeholders by the corresponding groups for the SPOE |
| 4384 | * agent */ |
| 4385 | list_for_each_entry(ph, &curgphs, list) { |
| 4386 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4387 | if (strcmp(grp->id, ph->id) == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4388 | grp->agent = curagent; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4389 | LIST_DELETE(&grp->list); |
| 4390 | LIST_APPEND(&curagent->groups, &grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4391 | goto next_aph; |
| 4392 | } |
| 4393 | } |
| 4394 | memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d", |
| 4395 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 4396 | goto error; |
| 4397 | next_aph: |
| 4398 | continue; |
| 4399 | } |
| 4400 | |
| 4401 | /* Replace placeholders by the corresponding message for each SPOE |
| 4402 | * group of the SPOE agent */ |
| 4403 | list_for_each_entry(grp, &curagent->groups, list) { |
| 4404 | list_for_each_entry_safe(ph, phback, &grp->phs, list) { |
| 4405 | list_for_each_entry(msg, &curmsgs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4406 | if (strcmp(msg->id, ph->id) == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4407 | if (msg->group != NULL) { |
| 4408 | memprintf(err, "SPOE message '%s' already belongs to " |
| 4409 | "the SPOE group '%s' declare at %s:%d", |
| 4410 | msg->id, msg->group->id, |
| 4411 | msg->group->conf.file, |
| 4412 | msg->group->conf.line); |
| 4413 | goto error; |
| 4414 | } |
| 4415 | |
| 4416 | /* Scope for arguments are not checked for now. We will check |
| 4417 | * them only if a rule use the corresponding SPOE group. */ |
| 4418 | msg->agent = curagent; |
| 4419 | msg->group = grp; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4420 | LIST_DELETE(&ph->list); |
| 4421 | LIST_APPEND(&grp->messages, &msg->by_grp); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4422 | goto next_mph_grp; |
| 4423 | } |
| 4424 | } |
| 4425 | memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d", |
| 4426 | grp->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 4427 | goto error; |
| 4428 | next_mph_grp: |
| 4429 | continue; |
| 4430 | } |
| 4431 | } |
| 4432 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4433 | finish: |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4434 | /* move curmsgs to the agent message list */ |
| 4435 | curmsgs.n->p = &curagent->messages; |
| 4436 | curmsgs.p->n = &curagent->messages; |
| 4437 | curagent->messages = curmsgs; |
| 4438 | LIST_INIT(&curmsgs); |
| 4439 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 4440 | conf->id = strdup(engine ? engine : curagent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4441 | conf->agent = curagent; |
Christopher Faulet | 434b852 | 2021-08-02 17:51:01 +0200 | [diff] [blame] | 4442 | curagent->spoe_conf = conf; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4443 | |
| 4444 | /* Start agent's proxy initialization here. It will be finished during |
| 4445 | * the filter init. */ |
| 4446 | memset(&conf->agent_fe, 0, sizeof(conf->agent_fe)); |
| 4447 | init_new_proxy(&conf->agent_fe); |
| 4448 | conf->agent_fe.id = conf->agent->id; |
| 4449 | conf->agent_fe.parent = conf->agent; |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 4450 | conf->agent_fe.options |= curpxopts; |
| 4451 | conf->agent_fe.options2 |= curpxopts2; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4452 | |
| 4453 | list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4454 | LIST_DELETE(&logsrv->list); |
| 4455 | LIST_APPEND(&conf->agent_fe.logsrvs, &logsrv->list); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4456 | } |
| 4457 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4458 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4459 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4460 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4461 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4462 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4463 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4464 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4465 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4466 | list_for_each_entry_safe(vph, vphback, &curvars, list) { |
| 4467 | struct arg arg; |
| 4468 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4469 | trash.data = snprintf(trash.area, trash.size, "proc.%s.%s", |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4470 | curagent->var_pfx, vph->name); |
| 4471 | |
| 4472 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4473 | arg.data.str.area = trash.area; |
| 4474 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4475 | 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] | 4476 | if (!vars_check_arg(&arg, err)) { |
| 4477 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4478 | curagent->id, curagent->var_pfx, vph->name, *err); |
| 4479 | goto error; |
| 4480 | } |
| 4481 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4482 | LIST_DELETE(&vph->list); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4483 | free(vph->name); |
| 4484 | free(vph); |
| 4485 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4486 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4487 | LIST_DELETE(&grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4488 | spoe_release_group(grp); |
| 4489 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4490 | *cur_arg = pos; |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 4491 | fconf->id = spoe_filter_id; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4492 | fconf->ops = &spoe_ops; |
| 4493 | fconf->conf = conf; |
| 4494 | return 0; |
| 4495 | |
| 4496 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4497 | spoe_release_agent(curagent); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4498 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4499 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4500 | spoe_release_placeholder(ph); |
| 4501 | } |
| 4502 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4503 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4504 | spoe_release_placeholder(ph); |
| 4505 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4506 | list_for_each_entry_safe(vph, vphback, &curvars, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4507 | LIST_DELETE(&vph->list); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4508 | free(vph->name); |
| 4509 | free(vph); |
| 4510 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4511 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4512 | LIST_DELETE(&grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4513 | spoe_release_group(grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4514 | } |
| 4515 | list_for_each_entry_safe(msg, msgback, &curmsgs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4516 | LIST_DELETE(&msg->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4517 | spoe_release_message(msg); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4518 | } |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4519 | list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4520 | LIST_DELETE(&logsrv->list); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4521 | free(logsrv); |
| 4522 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4523 | free(conf); |
| 4524 | return -1; |
| 4525 | } |
| 4526 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4527 | /* Send message of a SPOE group. This is the action_ptr callback of a rule |
| 4528 | * associated to a "send-spoe-group" action. |
| 4529 | * |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4530 | * It returns ACT_RET_CONT if processing is finished (with error or not), it returns |
| 4531 | * ACT_RET_YIELD if the action is in progress. */ |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4532 | static enum act_return |
| 4533 | spoe_send_group(struct act_rule *rule, struct proxy *px, |
| 4534 | struct session *sess, struct stream *s, int flags) |
| 4535 | { |
| 4536 | struct filter *filter; |
| 4537 | struct spoe_agent *agent = NULL; |
| 4538 | struct spoe_group *group = NULL; |
| 4539 | struct spoe_context *ctx = NULL; |
| 4540 | int ret, dir; |
| 4541 | |
| 4542 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 4543 | if (filter->config == rule->arg.act.p[0]) { |
| 4544 | agent = rule->arg.act.p[2]; |
| 4545 | group = rule->arg.act.p[3]; |
| 4546 | ctx = filter->ctx; |
| 4547 | break; |
| 4548 | } |
| 4549 | } |
| 4550 | if (agent == NULL || group == NULL || ctx == NULL) |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4551 | return ACT_RET_CONT; |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4552 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 4553 | return ACT_RET_CONT; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4554 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4555 | switch (rule->from) { |
| 4556 | case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break; |
| 4557 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 4558 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 4559 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 4560 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
| 4561 | default: |
| 4562 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 4563 | " - internal error while execute spoe-send-group\n", |
| 4564 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 4565 | __FUNCTION__, s); |
| 4566 | send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n", |
| 4567 | agent->id); |
| 4568 | return ACT_RET_CONT; |
| 4569 | } |
| 4570 | |
| 4571 | ret = spoe_process_group(s, ctx, group, dir); |
| 4572 | if (ret == 1) |
| 4573 | return ACT_RET_CONT; |
| 4574 | else if (ret == 0) { |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 4575 | if (flags & ACT_OPT_FINAL) { |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4576 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 4577 | " - failed to process group '%s': interrupted by caller\n", |
| 4578 | (int)now.tv_sec, (int)now.tv_usec, |
| 4579 | agent->id, __FUNCTION__, s, group->id); |
| 4580 | ctx->status_code = SPOE_CTX_ERR_INTERRUPT; |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 4581 | spoe_stop_processing(agent, ctx); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 4582 | spoe_handle_processing_error(s, agent, ctx, dir); |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4583 | return ACT_RET_CONT; |
| 4584 | } |
| 4585 | return ACT_RET_YIELD; |
| 4586 | } |
| 4587 | else |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4588 | return ACT_RET_CONT; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4589 | } |
| 4590 | |
| 4591 | /* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE |
| 4592 | * group associated to <rule>. The format of an rule using 'send-spoe-group' |
| 4593 | * action should be: |
| 4594 | * |
| 4595 | * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id> |
| 4596 | * |
| 4597 | * So, we'll loop on each configured SPOE filter for the proxy <px> to find the |
| 4598 | * SPOE engine matching <engine-id>. And then, we'll try to find the good group |
| 4599 | * matching <group-id>. Finally, we'll check all messages referenced by the SPOE |
| 4600 | * group. |
| 4601 | * |
| 4602 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 4603 | * filled. |
| 4604 | */ |
| 4605 | static int |
| 4606 | check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err) |
| 4607 | { |
| 4608 | struct flt_conf *fconf; |
| 4609 | struct spoe_config *conf; |
| 4610 | struct spoe_agent *agent = NULL; |
| 4611 | struct spoe_group *group; |
| 4612 | struct spoe_message *msg; |
| 4613 | char *engine_id = rule->arg.act.p[0]; |
| 4614 | char *group_id = rule->arg.act.p[1]; |
| 4615 | unsigned int where = 0; |
| 4616 | |
| 4617 | switch (rule->from) { |
| 4618 | case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break; |
| 4619 | case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break; |
| 4620 | case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break; |
| 4621 | case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break; |
| 4622 | case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break; |
| 4623 | default: |
| 4624 | memprintf(err, |
| 4625 | "internal error, unexpected rule->from=%d, please report this bug!", |
| 4626 | rule->from); |
| 4627 | goto error; |
| 4628 | } |
| 4629 | |
| 4630 | /* Try to find the SPOE engine by checking all SPOE filters for proxy |
| 4631 | * <px> */ |
| 4632 | list_for_each_entry(fconf, &px->filter_configs, list) { |
| 4633 | conf = fconf->conf; |
| 4634 | |
| 4635 | /* This is not an SPOE filter */ |
| 4636 | if (fconf->id != spoe_filter_id) |
| 4637 | continue; |
| 4638 | |
| 4639 | /* This is the good engine */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4640 | if (strcmp(conf->id, engine_id) == 0) { |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4641 | agent = conf->agent; |
| 4642 | break; |
| 4643 | } |
| 4644 | } |
| 4645 | if (agent == NULL) { |
| 4646 | memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'", |
| 4647 | engine_id, group_id); |
| 4648 | goto error; |
| 4649 | } |
| 4650 | |
| 4651 | /* Try to find the right group */ |
| 4652 | list_for_each_entry(group, &agent->groups, list) { |
| 4653 | /* This is the good group */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4654 | if (strcmp(group->id, group_id) == 0) |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4655 | break; |
| 4656 | } |
| 4657 | if (&group->list == &agent->groups) { |
| 4658 | memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration", |
| 4659 | group_id, engine_id); |
| 4660 | goto error; |
| 4661 | } |
| 4662 | |
| 4663 | /* Ok, we found the group, we need to check messages and their |
| 4664 | * arguments */ |
| 4665 | list_for_each_entry(msg, &group->messages, by_grp) { |
| 4666 | struct spoe_arg *arg; |
| 4667 | |
| 4668 | list_for_each_entry(arg, &msg->args, list) { |
| 4669 | if (!(arg->expr->fetch->val & where)) { |
| 4670 | memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: " |
| 4671 | "some args extract information from '%s'," |
| 4672 | "none of which is available here ('%s')", |
| 4673 | msg->id, group->id, msg->conf.file, msg->conf.line, |
| 4674 | sample_ckp_names(arg->expr->fetch->use), |
| 4675 | sample_ckp_names(where)); |
| 4676 | goto error; |
| 4677 | } |
| 4678 | } |
| 4679 | } |
| 4680 | |
| 4681 | free(engine_id); |
| 4682 | free(group_id); |
| 4683 | rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */ |
| 4684 | rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */ |
| 4685 | rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */ |
| 4686 | rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */ |
| 4687 | return 1; |
| 4688 | |
| 4689 | error: |
| 4690 | free(engine_id); |
| 4691 | free(group_id); |
| 4692 | return 0; |
| 4693 | } |
| 4694 | |
| 4695 | /* Parse 'send-spoe-group' action following the format: |
| 4696 | * |
| 4697 | * ... send-spoe-group <engine-id> <group-id> |
| 4698 | * |
| 4699 | * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error |
| 4700 | * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group |
| 4701 | * ids are saved and used later, when the rule will be checked. |
| 4702 | */ |
| 4703 | static enum act_parse_ret |
| 4704 | parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px, |
| 4705 | struct act_rule *rule, char **err) |
| 4706 | { |
| 4707 | if (!*args[*orig_arg] || !*args[*orig_arg+1] || |
| 4708 | (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) { |
| 4709 | memprintf(err, "expects 2 arguments: <engine-id> <group-id>"); |
| 4710 | return ACT_RET_PRS_ERR; |
| 4711 | } |
| 4712 | rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */ |
| 4713 | rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */ |
| 4714 | |
| 4715 | (*orig_arg) += 2; |
| 4716 | |
| 4717 | rule->action = ACT_CUSTOM; |
| 4718 | rule->action_ptr = spoe_send_group; |
| 4719 | rule->check_ptr = check_send_spoe_group; |
| 4720 | return ACT_RET_PRS_OK; |
| 4721 | } |
| 4722 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4723 | |
| 4724 | /* Declare the filter parser for "spoe" keyword */ |
| 4725 | static struct flt_kw_list flt_kws = { "SPOE", { }, { |
| 4726 | { "spoe", parse_spoe_flt, NULL }, |
| 4727 | { NULL, NULL, NULL }, |
| 4728 | } |
| 4729 | }; |
| 4730 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4731 | INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws); |
| 4732 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4733 | /* Delcate the action parser for "spoe-action" keyword */ |
| 4734 | static struct action_kw_list tcp_req_action_kws = { { }, { |
| 4735 | { "send-spoe-group", parse_send_spoe_group }, |
| 4736 | { /* END */ }, |
| 4737 | } |
| 4738 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4739 | |
| 4740 | INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws); |
| 4741 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4742 | static struct action_kw_list tcp_res_action_kws = { { }, { |
| 4743 | { "send-spoe-group", parse_send_spoe_group }, |
| 4744 | { /* END */ }, |
| 4745 | } |
| 4746 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4747 | |
| 4748 | INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws); |
| 4749 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4750 | static struct action_kw_list http_req_action_kws = { { }, { |
| 4751 | { "send-spoe-group", parse_send_spoe_group }, |
| 4752 | { /* END */ }, |
| 4753 | } |
| 4754 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4755 | |
| 4756 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws); |
| 4757 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4758 | static struct action_kw_list http_res_action_kws = { { }, { |
| 4759 | { "send-spoe-group", parse_send_spoe_group }, |
| 4760 | { /* END */ }, |
| 4761 | } |
| 4762 | }; |
| 4763 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4764 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws); |