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> |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 16 | #include <haproxy/applet.h> |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 17 | #include <haproxy/action-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 18 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 19 | #include <haproxy/arg.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 20 | #include <haproxy/cfgparse.h> |
Tim Duesterhus | 75e2f8d | 2021-09-16 17:38:26 +0200 | [diff] [blame] | 21 | #include <haproxy/check.h> |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 22 | #include <haproxy/conn_stream.h> |
| 23 | #include <haproxy/cs_utils.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 24 | #include <haproxy/filters.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 25 | #include <haproxy/freq_ctr.h> |
Willy Tarreau | 762d7a5 | 2020-06-04 11:23:07 +0200 | [diff] [blame] | 26 | #include <haproxy/frontend.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 27 | #include <haproxy/global.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 28 | #include <haproxy/http_rules.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 29 | #include <haproxy/log.h> |
Willy Tarreau | d0ef439 | 2020-06-02 09:38:52 +0200 | [diff] [blame] | 30 | #include <haproxy/pool.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 31 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 32 | #include <haproxy/sample.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 33 | #include <haproxy/session.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 34 | #include <haproxy/signal.h> |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 35 | #include <haproxy/sink.h> |
Willy Tarreau | 6c58ab0 | 2020-06-04 22:35:49 +0200 | [diff] [blame] | 36 | #include <haproxy/spoe.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 37 | #include <haproxy/stream.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 38 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 39 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 40 | #include <haproxy/thread.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 41 | #include <haproxy/time.h> |
Willy Tarreau | e16ada1 | 2021-05-08 12:57:17 +0200 | [diff] [blame] | 42 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 43 | #include <haproxy/vars.h> |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 44 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 45 | |
| 46 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 47 | #define SPOE_PRINTF(x...) fprintf(x) |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 48 | #define SPOE_DEBUG_STMT(statement) statement |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 49 | #else |
| 50 | #define SPOE_PRINTF(x...) |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 51 | #define SPOE_DEBUG_STMT(statement) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 52 | #endif |
| 53 | |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 54 | /* Reserved 4 bytes to the frame size. So a frame and its size can be written |
| 55 | * together in a buffer */ |
| 56 | #define MAX_FRAME_SIZE global.tune.bufsize - 4 |
| 57 | |
| 58 | /* The minimum size for a frame */ |
| 59 | #define MIN_FRAME_SIZE 256 |
| 60 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 61 | /* Reserved for the metadata and the frame type. |
| 62 | * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */ |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 63 | #define FRAME_HDR_SIZE 32 |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 64 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 65 | /* Helper to get SPOE ctx inside an appctx */ |
Willy Tarreau | 23a2407 | 2022-05-05 20:18:44 +0200 | [diff] [blame] | 66 | #define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->svcctx)) |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 67 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 68 | /* SPOE filter id. Used to identify SPOE filters */ |
| 69 | const char *spoe_filter_id = "SPOE filter"; |
| 70 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 71 | /* Set if the handle on SIGUSR1 is registered */ |
| 72 | static int sighandler_registered = 0; |
| 73 | |
| 74 | /* proxy used during the parsing */ |
| 75 | struct proxy *curproxy = NULL; |
| 76 | |
| 77 | /* The name of the SPOE engine, used during the parsing */ |
| 78 | char *curengine = NULL; |
| 79 | |
| 80 | /* SPOE agent used during the parsing */ |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 81 | /* SPOE agent/group/message used during the parsing */ |
| 82 | struct spoe_agent *curagent = NULL; |
| 83 | struct spoe_group *curgrp = NULL; |
| 84 | struct spoe_message *curmsg = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 85 | |
| 86 | /* list of SPOE messages and placeholders used during the parsing */ |
| 87 | struct list curmsgs; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 88 | struct list curgrps; |
| 89 | struct list curmphs; |
| 90 | struct list curgphs; |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 91 | struct list curvars; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 92 | |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 93 | /* list of log servers used during the parsing */ |
| 94 | struct list curlogsrvs; |
| 95 | |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 96 | /* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */ |
| 97 | int curpxopts; |
| 98 | int curpxopts2; |
| 99 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 100 | /* Pools used to allocate SPOE structs */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 101 | DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context)); |
| 102 | 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] | 103 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 104 | struct flt_ops spoe_ops; |
| 105 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 106 | static int spoe_queue_context(struct spoe_context *ctx); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 107 | static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait); |
| 108 | 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] | 109 | static struct appctx *spoe_create_appctx(struct spoe_config *conf); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 110 | |
| 111 | /******************************************************************** |
| 112 | * helper functions/globals |
| 113 | ********************************************************************/ |
| 114 | static void |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 115 | spoe_release_placeholder(struct spoe_placeholder *ph) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 116 | { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 117 | if (!ph) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 118 | return; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 119 | free(ph->id); |
| 120 | free(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 123 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 124 | spoe_release_message(struct spoe_message *msg) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 125 | { |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 126 | struct spoe_arg *arg, *argback; |
| 127 | struct acl *acl, *aclback; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 128 | |
| 129 | if (!msg) |
| 130 | return; |
| 131 | free(msg->id); |
| 132 | free(msg->conf.file); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 133 | list_for_each_entry_safe(arg, argback, &msg->args, list) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 134 | release_sample_expr(arg->expr); |
| 135 | free(arg->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 136 | LIST_DELETE(&arg->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 137 | free(arg); |
| 138 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 139 | list_for_each_entry_safe(acl, aclback, &msg->acls, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 140 | LIST_DELETE(&acl->list); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 141 | prune_acl(acl); |
| 142 | free(acl); |
| 143 | } |
| 144 | if (msg->cond) { |
| 145 | prune_acl_cond(msg->cond); |
| 146 | free(msg->cond); |
| 147 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 148 | free(msg); |
| 149 | } |
| 150 | |
| 151 | static void |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 152 | spoe_release_group(struct spoe_group *grp) |
| 153 | { |
| 154 | if (!grp) |
| 155 | return; |
| 156 | free(grp->id); |
| 157 | free(grp->conf.file); |
| 158 | free(grp); |
| 159 | } |
| 160 | |
| 161 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 162 | spoe_release_agent(struct spoe_agent *agent) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 163 | { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 164 | struct spoe_message *msg, *msgback; |
| 165 | struct spoe_group *grp, *grpback; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 166 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 167 | |
| 168 | if (!agent) |
| 169 | return; |
| 170 | free(agent->id); |
| 171 | free(agent->conf.file); |
| 172 | free(agent->var_pfx); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 173 | free(agent->var_on_error); |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 174 | free(agent->var_t_process); |
| 175 | free(agent->var_t_total); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 176 | list_for_each_entry_safe(msg, msgback, &agent->messages, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 177 | LIST_DELETE(&msg->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 178 | spoe_release_message(msg); |
| 179 | } |
| 180 | list_for_each_entry_safe(grp, grpback, &agent->groups, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 181 | LIST_DELETE(&grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 182 | spoe_release_group(grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 183 | } |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 184 | if (agent->rt) { |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 185 | for (i = 0; i < global.nbthread; ++i) { |
| 186 | free(agent->rt[i].engine_id); |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 187 | HA_SPIN_DESTROY(&agent->rt[i].lock); |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 188 | } |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 189 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 190 | free(agent->rt); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 191 | free(agent); |
| 192 | } |
| 193 | |
| 194 | static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 195 | [SPOE_FRM_ERR_NONE] = "normal", |
| 196 | [SPOE_FRM_ERR_IO] = "I/O error", |
| 197 | [SPOE_FRM_ERR_TOUT] = "a timeout occurred", |
| 198 | [SPOE_FRM_ERR_TOO_BIG] = "frame is too big", |
| 199 | [SPOE_FRM_ERR_INVALID] = "invalid frame received", |
| 200 | [SPOE_FRM_ERR_NO_VSN] = "version value not found", |
| 201 | [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found", |
| 202 | [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found", |
| 203 | [SPOE_FRM_ERR_BAD_VSN] = "unsupported version", |
| 204 | [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small", |
| 205 | [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported", |
| 206 | [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames", |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 207 | [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 208 | [SPOE_FRM_ERR_RES] = "resource allocation error", |
| 209 | [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | static const char *spoe_event_str[SPOE_EV_EVENTS] = { |
| 213 | [SPOE_EV_ON_CLIENT_SESS] = "on-client-session", |
| 214 | [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request", |
| 215 | [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request", |
| 216 | [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request", |
| 217 | [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request", |
| 218 | |
| 219 | [SPOE_EV_ON_SERVER_SESS] = "on-server-session", |
| 220 | [SPOE_EV_ON_TCP_RSP] = "on-tcp-response", |
| 221 | [SPOE_EV_ON_HTTP_RSP] = "on-http-response", |
| 222 | }; |
| 223 | |
| 224 | |
| 225 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 226 | |
| 227 | static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 228 | [SPOE_CTX_ST_NONE] = "NONE", |
| 229 | [SPOE_CTX_ST_READY] = "READY", |
| 230 | [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS", |
| 231 | [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS", |
| 232 | [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK", |
| 233 | [SPOE_CTX_ST_DONE] = "DONE", |
| 234 | [SPOE_CTX_ST_ERROR] = "ERROR", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 238 | [SPOE_APPCTX_ST_CONNECT] = "CONNECT", |
| 239 | [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING", |
| 240 | [SPOE_APPCTX_ST_IDLE] = "IDLE", |
| 241 | [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING", |
| 242 | [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY", |
| 243 | [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK", |
| 244 | [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT", |
| 245 | [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING", |
| 246 | [SPOE_APPCTX_ST_EXIT] = "EXIT", |
| 247 | [SPOE_APPCTX_ST_END] = "END", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | #endif |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 251 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 252 | /* 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] | 253 | * 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] | 254 | * allocation failed, it returns NULL. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 255 | static char * |
| 256 | generate_pseudo_uuid() |
| 257 | { |
Willy Tarreau | ee3bcdd | 2020-03-08 17:48:17 +0100 | [diff] [blame] | 258 | ha_generate_uuid(&trash); |
Kevin Zhu | 079f808 | 2020-03-13 10:39:51 +0800 | [diff] [blame] | 259 | return my_strndup(trash.area, trash.data); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 262 | |
| 263 | static inline void |
| 264 | spoe_update_stat_time(struct timeval *tv, long *t) |
| 265 | { |
| 266 | if (*t == -1) |
| 267 | *t = tv_ms_elapsed(tv, &now); |
| 268 | else |
| 269 | *t += tv_ms_elapsed(tv, &now); |
| 270 | tv_zero(tv); |
| 271 | } |
| 272 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 273 | /******************************************************************** |
| 274 | * Functions that encode/decode SPOE frames |
| 275 | ********************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 276 | /* Helper to get static string length, excluding the terminating null byte */ |
| 277 | #define SLEN(str) (sizeof(str)-1) |
| 278 | |
| 279 | /* Predefined key used in HELLO/DISCONNECT frames */ |
| 280 | #define SUPPORTED_VERSIONS_KEY "supported-versions" |
| 281 | #define VERSION_KEY "version" |
| 282 | #define MAX_FRAME_SIZE_KEY "max-frame-size" |
| 283 | #define CAPABILITIES_KEY "capabilities" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 284 | #define ENGINE_ID_KEY "engine-id" |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 285 | #define HEALTHCHECK_KEY "healthcheck" |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 286 | #define STATUS_CODE_KEY "status-code" |
| 287 | #define MSG_KEY "message" |
| 288 | |
| 289 | struct spoe_version { |
| 290 | char *str; |
| 291 | int min; |
| 292 | int max; |
| 293 | }; |
| 294 | |
| 295 | /* All supported versions */ |
| 296 | static struct spoe_version supported_versions[] = { |
Christopher Faulet | 6381650 | 2018-05-31 14:56:42 +0200 | [diff] [blame] | 297 | /* 1.0 is now unsupported because of a bug about frame's flags*/ |
| 298 | {"2.0", 2000, 2000}, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 299 | {NULL, 0, 0} |
| 300 | }; |
| 301 | |
| 302 | /* Comma-separated list of supported versions */ |
Christopher Faulet | 6381650 | 2018-05-31 14:56:42 +0200 | [diff] [blame] | 303 | #define SUPPORTED_VERSIONS_VAL "2.0" |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 304 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 305 | /* Convert a string to a SPOE version value. The string must follow the format |
| 306 | * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR). |
| 307 | * If an error occurred, -1 is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 308 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 309 | spoe_str_to_vsn(const char *str, size_t len) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 310 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 311 | const char *p, *end; |
| 312 | int maj, min, vsn; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 313 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 314 | p = str; |
| 315 | end = str+len; |
| 316 | maj = min = 0; |
| 317 | vsn = -1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 318 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 319 | /* skip leading spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 320 | while (p < end && isspace((unsigned char)*p)) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 321 | p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 322 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 323 | /* parse Major number, until the '.' */ |
| 324 | while (*p != '.') { |
| 325 | if (p >= end || *p < '0' || *p > '9') |
| 326 | goto out; |
| 327 | maj *= 10; |
| 328 | maj += (*p - '0'); |
| 329 | p++; |
| 330 | } |
| 331 | |
| 332 | /* check Major version */ |
| 333 | if (!maj) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 334 | goto out; |
| 335 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 336 | p++; /* skip the '.' */ |
| 337 | if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */ |
| 338 | goto out; |
| 339 | |
| 340 | /* Parse Minor number */ |
| 341 | while (p < end) { |
| 342 | if (*p < '0' || *p > '9') |
| 343 | break; |
| 344 | min *= 10; |
| 345 | min += (*p - '0'); |
| 346 | p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 347 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 348 | |
| 349 | /* check Minor number */ |
| 350 | if (min > 999) |
| 351 | goto out; |
| 352 | |
| 353 | /* skip trailing spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 354 | while (p < end && isspace((unsigned char)*p)) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 355 | p++; |
| 356 | if (p != end) |
| 357 | goto out; |
| 358 | |
| 359 | vsn = maj * 1000 + min; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 360 | out: |
| 361 | return vsn; |
| 362 | } |
| 363 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 364 | /* 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] | 365 | * 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] | 366 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 367 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 368 | spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 369 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 370 | struct buffer *chk; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 371 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 372 | char *p, *end; |
| 373 | unsigned int flags = SPOE_FRM_FL_FIN; |
| 374 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 375 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 376 | p = frame; |
| 377 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 378 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 379 | /* Set Frame type */ |
| 380 | *p++ = SPOE_FRM_T_HAPROXY_HELLO; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 381 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 382 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 383 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 384 | memcpy(p, (char *)&flags, 4); |
| 385 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 386 | |
| 387 | /* No stream-id and frame-id for HELLO frames */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 388 | *p++ = 0; *p++ = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 389 | |
| 390 | /* There are 3 mandatory items: "supported-versions", "max-frame-size" |
| 391 | * and "capabilities" */ |
| 392 | |
| 393 | /* "supported-versions" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 394 | sz = SLEN(SUPPORTED_VERSIONS_KEY); |
| 395 | if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1) |
| 396 | goto too_big; |
| 397 | |
| 398 | *p++ = SPOE_DATA_T_STR; |
| 399 | sz = SLEN(SUPPORTED_VERSIONS_VAL); |
| 400 | if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1) |
| 401 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 402 | |
| 403 | /* "max-fram-size" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 404 | sz = SLEN(MAX_FRAME_SIZE_KEY); |
| 405 | if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1) |
| 406 | goto too_big; |
| 407 | |
| 408 | *p++ = SPOE_DATA_T_UINT32; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 409 | if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 410 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 411 | |
| 412 | /* "capabilities" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 413 | sz = SLEN(CAPABILITIES_KEY); |
| 414 | if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1) |
| 415 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 416 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 417 | *p++ = SPOE_DATA_T_STR; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 418 | chk = get_trash_chunk(); |
| 419 | if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 420 | memcpy(chk->area, "pipelining", 10); |
| 421 | chk->data += 10; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 422 | } |
| 423 | if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 424 | if (chk->data) chk->area[chk->data++] = ','; |
| 425 | memcpy(chk->area+chk->data, "async", 5); |
| 426 | chk->data += 5; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 427 | } |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 428 | if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 429 | if (chk->data) chk->area[chk->data++] = ','; |
| 430 | memcpy(chk->area+chk->data, "fragmentation", 13); |
Miroslav Zagorac | 6b3690b | 2019-01-13 16:55:01 +0100 | [diff] [blame] | 431 | chk->data += 13; |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 432 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 433 | if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 434 | goto too_big; |
| 435 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 436 | /* (optional) "engine-id" K/V item, if present */ |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 437 | if (agent != NULL && agent->rt[tid].engine_id != NULL) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 438 | sz = SLEN(ENGINE_ID_KEY); |
| 439 | if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1) |
| 440 | goto too_big; |
| 441 | |
| 442 | *p++ = SPOE_DATA_T_STR; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 443 | sz = strlen(agent->rt[tid].engine_id); |
| 444 | 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] | 445 | goto too_big; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 446 | } |
| 447 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 448 | return (p - frame); |
| 449 | |
| 450 | too_big: |
| 451 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 452 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 455 | /* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of |
| 456 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
| 457 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 458 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 459 | spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 460 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 461 | const char *reason; |
| 462 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 463 | unsigned int flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 464 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 465 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 466 | p = frame; |
| 467 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 468 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 469 | /* Set Frame type */ |
| 470 | *p++ = SPOE_FRM_T_HAPROXY_DISCON; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 471 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 472 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 473 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 474 | memcpy(p, (char *)&flags, 4); |
| 475 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 476 | |
| 477 | /* No stream-id and frame-id for DISCONNECT frames */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 478 | *p++ = 0; *p++ = 0; |
| 479 | |
| 480 | if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS) |
| 481 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 482 | |
| 483 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 484 | |
| 485 | /* "status-code" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 486 | sz = SLEN(STATUS_CODE_KEY); |
| 487 | if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1) |
| 488 | goto too_big; |
| 489 | |
| 490 | *p++ = SPOE_DATA_T_UINT32; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 491 | if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 492 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 493 | |
| 494 | /* "message" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 495 | sz = SLEN(MSG_KEY); |
| 496 | if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1) |
| 497 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 498 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 499 | /*Get the message corresponding to the status code */ |
| 500 | reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]; |
| 501 | |
| 502 | *p++ = SPOE_DATA_T_STR; |
| 503 | sz = strlen(reason); |
| 504 | if (spoe_encode_buffer(reason, sz, &p, end) == -1) |
| 505 | goto too_big; |
| 506 | |
| 507 | return (p - frame); |
| 508 | |
| 509 | too_big: |
| 510 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 511 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 512 | } |
| 513 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 514 | /* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of |
| 515 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
| 516 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 517 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 518 | spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 519 | char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 520 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 521 | char *p, *end; |
| 522 | unsigned int stream_id, frame_id; |
| 523 | unsigned int flags = SPOE_FRM_FL_FIN; |
| 524 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 525 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 526 | p = frame; |
| 527 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 528 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 529 | stream_id = ctx->stream_id; |
| 530 | frame_id = ctx->frame_id; |
| 531 | |
| 532 | if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) { |
| 533 | /* The fragmentation is not supported by the applet */ |
| 534 | if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) { |
| 535 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
| 536 | return -1; |
| 537 | } |
| 538 | flags = ctx->frag_ctx.flags; |
| 539 | } |
| 540 | |
| 541 | /* Set Frame type */ |
| 542 | *p++ = SPOE_FRM_T_HAPROXY_NOTIFY; |
| 543 | |
| 544 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 545 | flags = htonl(flags); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 546 | memcpy(p, (char *)&flags, 4); |
| 547 | p += 4; |
| 548 | |
| 549 | /* Set stream-id and frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 550 | if (encode_varint(stream_id, &p, end) == -1) |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 551 | goto too_big; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 552 | if (encode_varint(frame_id, &p, end) == -1) |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 553 | goto too_big; |
| 554 | |
| 555 | /* Copy encoded messages, if possible */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 556 | sz = b_data(&ctx->buffer); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 557 | if (p + sz >= end) |
| 558 | goto too_big; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 559 | memcpy(p, b_head(&ctx->buffer), sz); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 560 | p += sz; |
| 561 | |
| 562 | return (p - frame); |
| 563 | |
| 564 | too_big: |
| 565 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | /* Encode next part of a fragmented frame sent by HAProxy to an agent. It |
| 570 | * returns the number of encoded bytes in the frame on success, 0 if an encoding |
| 571 | * error occurred and -1 if a fatal error occurred. */ |
| 572 | static int |
| 573 | spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx, |
| 574 | char *frame, size_t size) |
| 575 | { |
| 576 | char *p, *end; |
| 577 | unsigned int stream_id, frame_id; |
| 578 | unsigned int flags; |
| 579 | size_t sz; |
| 580 | |
| 581 | p = frame; |
| 582 | end = frame+size; |
| 583 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 584 | /* <ctx> is null when the stream has aborted the processing of a |
| 585 | * fragmented frame. In this case, we must notify the corresponding |
| 586 | * agent using ids stored in <frag_ctx>. */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 587 | if (ctx == NULL) { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 588 | flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 589 | stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid; |
| 590 | frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid; |
| 591 | } |
| 592 | else { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 593 | flags = ctx->frag_ctx.flags; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 594 | stream_id = ctx->stream_id; |
| 595 | frame_id = ctx->frame_id; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 598 | /* Set Frame type */ |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 599 | *p++ = SPOE_FRM_T_UNSET; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 600 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 601 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 602 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 603 | memcpy(p, (char *)&flags, 4); |
| 604 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 605 | |
| 606 | /* Set stream-id and frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 607 | if (encode_varint(stream_id, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 608 | goto too_big; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 609 | if (encode_varint(frame_id, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 610 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 611 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 612 | if (ctx == NULL) |
| 613 | goto end; |
| 614 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 615 | /* Copy encoded messages, if possible */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 616 | sz = b_data(&ctx->buffer); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 617 | if (p + sz >= end) |
| 618 | goto too_big; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 619 | memcpy(p, b_head(&ctx->buffer), sz); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 620 | p += sz; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 621 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 622 | end: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 623 | return (p - frame); |
| 624 | |
| 625 | too_big: |
| 626 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 627 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 628 | } |
| 629 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 630 | /* Decode and process the HELLO frame sent by an agent. It returns the number of |
| 631 | * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal |
| 632 | * error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 633 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 634 | spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 635 | { |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 636 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
| 637 | char *p, *end; |
| 638 | int vsn, max_frame_size; |
| 639 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 640 | |
| 641 | p = frame; |
| 642 | end = frame + size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 643 | |
| 644 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 645 | if (*p++ != SPOE_FRM_T_AGENT_HELLO) { |
| 646 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 647 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 648 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 649 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 650 | if (size < 7 /* TYPE + METADATA */) { |
| 651 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 652 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 653 | } |
| 654 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 655 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 656 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 657 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 658 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 659 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 660 | /* Fragmentation is not supported for HELLO frame */ |
| 661 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 662 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 663 | return -1; |
| 664 | } |
| 665 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 666 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 667 | if (*p != 0 || *(p+1) != 0) { |
| 668 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 669 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 670 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 671 | p += 2; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 672 | |
| 673 | /* There are 3 mandatory items: "version", "max-frame-size" and |
| 674 | * "capabilities" */ |
| 675 | |
| 676 | /* Loop on K/V items */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 677 | vsn = max_frame_size = flags = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 678 | while (p < end) { |
| 679 | char *str; |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 680 | uint64_t sz; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 681 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 682 | |
| 683 | /* Decode the item key */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 684 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 685 | if (ret == -1 || !sz) { |
| 686 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 687 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 688 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 689 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 690 | /* Check "version" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 691 | if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 692 | int i, type = *p++; |
| 693 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 694 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 695 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 696 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 697 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 698 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 699 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) { |
| 700 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 701 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 702 | } |
| 703 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 704 | vsn = spoe_str_to_vsn(str, sz); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 705 | if (vsn == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 706 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 707 | return -1; |
| 708 | } |
| 709 | for (i = 0; supported_versions[i].str != NULL; ++i) { |
| 710 | if (vsn >= supported_versions[i].min && |
| 711 | vsn <= supported_versions[i].max) |
| 712 | break; |
| 713 | } |
| 714 | if (supported_versions[i].str == NULL) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 715 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 716 | return -1; |
| 717 | } |
| 718 | } |
| 719 | /* Check "max-frame-size" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 720 | 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] | 721 | int type = *p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 722 | |
| 723 | /* The value must be integer */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 724 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 725 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 726 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 727 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 728 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 729 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 730 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 731 | if (decode_varint(&p, end, &sz) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 732 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 733 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 734 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 735 | if (sz < MIN_FRAME_SIZE || |
| 736 | sz > SPOE_APPCTX(appctx)->max_frame_size) { |
| 737 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 738 | return -1; |
| 739 | } |
| 740 | max_frame_size = sz; |
| 741 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 742 | /* Check "capabilities" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 743 | 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] | 744 | int type = *p++; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 745 | |
| 746 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 747 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 748 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 749 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 750 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 751 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) { |
| 752 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 753 | return 0; |
| 754 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 755 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 756 | while (sz) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 757 | char *delim; |
| 758 | |
| 759 | /* Skip leading spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 760 | for (; isspace((unsigned char)*str) && sz; str++, sz--); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 761 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 762 | if (sz >= 10 && !strncmp(str, "pipelining", 10)) { |
| 763 | str += 10; sz -= 10; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 764 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 765 | flags |= SPOE_APPCTX_FL_PIPELINING; |
| 766 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 767 | else if (sz >= 5 && !strncmp(str, "async", 5)) { |
| 768 | str += 5; sz -= 5; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 769 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 770 | flags |= SPOE_APPCTX_FL_ASYNC; |
| 771 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 772 | else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) { |
| 773 | str += 13; sz -= 13; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 774 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 775 | flags |= SPOE_APPCTX_FL_FRAGMENTATION; |
| 776 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 777 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 778 | /* Get the next comma or break */ |
| 779 | if (!sz || (delim = memchr(str, ',', sz)) == NULL) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 780 | break; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 781 | delim++; |
| 782 | sz -= (delim - str); |
| 783 | str = delim; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 784 | } |
| 785 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 786 | else { |
| 787 | /* Silently ignore unknown item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 788 | if (spoe_skip_data(&p, end) == -1) { |
| 789 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 790 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 791 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
| 795 | /* Final checks */ |
| 796 | if (!vsn) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 797 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 798 | return -1; |
| 799 | } |
| 800 | if (!max_frame_size) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 801 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 802 | return -1; |
| 803 | } |
Christopher Faulet | 1138901 | 2019-02-07 16:13:26 +0100 | [diff] [blame] | 804 | if (!agent) |
| 805 | flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC); |
| 806 | else { |
| 807 | if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING)) |
| 808 | flags &= ~SPOE_APPCTX_FL_PIPELINING; |
| 809 | if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC)) |
| 810 | flags &= ~SPOE_APPCTX_FL_ASYNC; |
| 811 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 812 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 813 | SPOE_APPCTX(appctx)->version = (unsigned int)vsn; |
| 814 | SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size; |
| 815 | SPOE_APPCTX(appctx)->flags |= flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 816 | |
| 817 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /* Decode DISCONNECT frame sent by an agent. It returns the number of by read |
| 821 | * bytes on success, 0 if the frame can be ignored and -1 if an error |
| 822 | * occurred. */ |
| 823 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 824 | spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 825 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 826 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 827 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 828 | |
| 829 | p = frame; |
| 830 | end = frame + size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 831 | |
| 832 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 833 | if (*p++ != SPOE_FRM_T_AGENT_DISCON) { |
| 834 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 835 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 836 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 837 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 838 | if (size < 7 /* TYPE + METADATA */) { |
| 839 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 840 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 841 | } |
| 842 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 843 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 844 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 845 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 846 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 847 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 848 | /* Fragmentation is not supported for DISCONNECT frame */ |
| 849 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 850 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 851 | return -1; |
| 852 | } |
| 853 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 854 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 855 | if (*p != 0 || *(p+1) != 0) { |
| 856 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 857 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 858 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 859 | p += 2; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 860 | |
| 861 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 862 | |
| 863 | /* Loop on K/V items */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 864 | while (p < end) { |
| 865 | char *str; |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 866 | uint64_t sz; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 867 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 868 | |
| 869 | /* Decode the item key */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 870 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 871 | if (ret == -1 || !sz) { |
| 872 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 873 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | /* Check "status-code" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 877 | 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] | 878 | int type = *p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 879 | |
| 880 | /* The value must be an integer */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 881 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 882 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 883 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 884 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 885 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 886 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 887 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 888 | if (decode_varint(&p, end, &sz) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 889 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 890 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 891 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 892 | SPOE_APPCTX(appctx)->status_code = sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /* Check "message" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 896 | 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] | 897 | int type = *p++; |
| 898 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 899 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 900 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 901 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 902 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 903 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 904 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 905 | if (ret == -1 || sz > 255) { |
| 906 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 907 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 908 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 909 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 910 | SPOE_APPCTX(appctx)->reason = str; |
| 911 | SPOE_APPCTX(appctx)->rlen = sz; |
| 912 | #endif |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 913 | } |
| 914 | else { |
| 915 | /* Silently ignore unknown item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 916 | if (spoe_skip_data(&p, end) == -1) { |
| 917 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 918 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 919 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 923 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 927 | /* 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] | 928 | * success, 0 if the frame can be ignored and -1 if an error occurred. */ |
| 929 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 930 | spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 931 | char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 932 | { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 933 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 934 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 935 | uint64_t stream_id, frame_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 936 | int len; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 937 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 938 | |
| 939 | p = frame; |
| 940 | end = frame + size; |
| 941 | *ctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 942 | |
| 943 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 944 | if (*p++ != SPOE_FRM_T_AGENT_ACK) { |
| 945 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 946 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 947 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 948 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 949 | if (size < 7 /* TYPE + METADATA */) { |
| 950 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 951 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 952 | } |
| 953 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 954 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 955 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 956 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 957 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 958 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 959 | /* Fragmentation is not supported for now */ |
| 960 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 961 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 962 | return -1; |
| 963 | } |
| 964 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 965 | /* Get the stream-id and the frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 966 | if (decode_varint(&p, end, &stream_id) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 967 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 968 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 969 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 970 | if (decode_varint(&p, end, &frame_id) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 971 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 972 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 973 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 974 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 975 | /* Try to find the corresponding SPOE context */ |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 976 | if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 977 | list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 978 | if ((*ctx)->stream_id == (unsigned int)stream_id && |
| 979 | (*ctx)->frame_id == (unsigned int)frame_id) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 980 | goto found; |
| 981 | } |
| 982 | } |
| 983 | else { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 984 | list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) { |
| 985 | if ((*ctx)->stream_id == (unsigned int)stream_id && |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 986 | (*ctx)->frame_id == (unsigned int)frame_id) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 987 | goto found; |
| 988 | } |
| 989 | } |
| 990 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 991 | if (SPOE_APPCTX(appctx)->frag_ctx.ctx && |
| 992 | SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id && |
| 993 | SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) { |
| 994 | |
| 995 | /* ABRT bit is set for an unfinished fragmented frame */ |
| 996 | if (flags & SPOE_FRM_FL_ABRT) { |
| 997 | *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx; |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 998 | (*ctx)->state = SPOE_CTX_ST_ERROR; |
| 999 | (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT; |
| 1000 | /* Ignore the payload */ |
| 1001 | goto end; |
| 1002 | } |
| 1003 | /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */ |
| 1004 | /* For now, we ignore the ack */ |
| 1005 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 1006 | return 0; |
| 1007 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1008 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1009 | /* No Stream found, ignore the frame */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1010 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1011 | " - Ignore ACK frame" |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1012 | " - stream-id=%u - frame-id=%u\n", |
| 1013 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1014 | __FUNCTION__, appctx, |
| 1015 | (unsigned int)stream_id, (unsigned int)frame_id); |
| 1016 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1017 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND; |
Christopher Faulet | c7ba910 | 2020-11-10 14:31:39 +0100 | [diff] [blame] | 1018 | if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) { |
| 1019 | /* Report an error if we are waiting the ack for another frame, |
| 1020 | * but not if there is no longer frame waiting for a ack |
| 1021 | * (timeout) |
| 1022 | */ |
| 1023 | if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) || |
| 1024 | SPOE_APPCTX(appctx)->frag_ctx.ctx) |
| 1025 | return -1; |
| 1026 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1027 | SPOE_APPCTX(appctx)->cur_fpa = 0; |
| 1028 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1029 | return 0; |
| 1030 | |
| 1031 | found: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1032 | if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer, |
| 1033 | &SPOE_APPCTX(appctx)->buffer_wait)) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1034 | *ctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1035 | return 1; /* Retry later */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1036 | } |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1037 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1038 | /* Copy encoded actions */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1039 | len = (end - p); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1040 | memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len); |
| 1041 | b_set_data(&SPOE_APPCTX(appctx)->buffer, len); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1042 | p += len; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1043 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1044 | /* Transfer the buffer ownership to the SPOE context */ |
| 1045 | (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1046 | SPOE_APPCTX(appctx)->buffer = BUF_NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1047 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1048 | (*ctx)->state = SPOE_CTX_ST_DONE; |
| 1049 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1050 | end: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1051 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1052 | " - ACK frame received" |
| 1053 | " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1054 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1055 | __FUNCTION__, appctx, *ctx, (*ctx)->stream_id, |
| 1056 | (*ctx)->frame_id, flags); |
| 1057 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1058 | } |
| 1059 | |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1060 | /* This function is used in cfgparse.c and declared in proto/checks.h. It |
| 1061 | * prepare the request to send to agents during a healthcheck. It returns 0 on |
| 1062 | * success and -1 if an error occurred. */ |
| 1063 | int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1064 | spoe_prepare_healthcheck_request(char **req, int *len) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1065 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1066 | struct appctx appctx; |
| 1067 | struct spoe_appctx spoe_appctx; |
| 1068 | char *frame, *end, buf[MAX_FRAME_SIZE+4]; |
| 1069 | size_t sz; |
| 1070 | int ret; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1071 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1072 | memset(&appctx, 0, sizeof(appctx)); |
| 1073 | memset(&spoe_appctx, 0, sizeof(spoe_appctx)); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1074 | memset(buf, 0, sizeof(buf)); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1075 | |
Willy Tarreau | 23a2407 | 2022-05-05 20:18:44 +0200 | [diff] [blame] | 1076 | appctx.svcctx = &spoe_appctx; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1077 | SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1078 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1079 | frame = buf+4; /* Reserved the 4 first bytes for the frame size */ |
| 1080 | end = frame + MAX_FRAME_SIZE; |
| 1081 | |
| 1082 | ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE); |
| 1083 | if (ret <= 0) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1084 | return -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1085 | frame += ret; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1086 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1087 | /* Add "healthcheck" K/V item */ |
| 1088 | sz = SLEN(HEALTHCHECK_KEY); |
| 1089 | if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1) |
| 1090 | return -1; |
| 1091 | *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1092 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1093 | *len = frame - buf; |
| 1094 | sz = htonl(*len - 4); |
| 1095 | memcpy(buf, (char *)&sz, 4); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1096 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1097 | if ((*req = malloc(*len)) == NULL) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1098 | return -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1099 | memcpy(*req, buf, *len); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | /* This function is used in checks.c and declared in proto/checks.h. It decode |
| 1104 | * the response received from an agent during a healthcheck. It returns 0 on |
| 1105 | * success and -1 if an error occurred. */ |
| 1106 | int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1107 | 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] | 1108 | { |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1109 | struct appctx appctx; |
| 1110 | struct spoe_appctx spoe_appctx; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1111 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1112 | memset(&appctx, 0, sizeof(appctx)); |
| 1113 | memset(&spoe_appctx, 0, sizeof(spoe_appctx)); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1114 | |
Willy Tarreau | 23a2407 | 2022-05-05 20:18:44 +0200 | [diff] [blame] | 1115 | appctx.svcctx = &spoe_appctx; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1116 | SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1117 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1118 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1119 | spoe_handle_agentdiscon_frame(&appctx, frame, size); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1120 | goto error; |
| 1121 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1122 | if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0) |
| 1123 | goto error; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1124 | |
| 1125 | return 0; |
| 1126 | |
| 1127 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1128 | if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS) |
| 1129 | SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN; |
| 1130 | strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1131 | return -1; |
| 1132 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1133 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1134 | /* 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] | 1135 | * 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] | 1136 | * success. */ |
| 1137 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1138 | spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1139 | { |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1140 | struct stconn *cs = appctx_cs(appctx); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1141 | int ret; |
| 1142 | uint32_t netint; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1143 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1144 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1145 | * length. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1146 | netint = htonl(framesz); |
| 1147 | memcpy(buf, (char *)&netint, 4); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1148 | ret = ci_putblk(cs_ic(cs), buf, framesz+4); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1149 | if (ret <= 0) { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1150 | if ((ret == -3 && b_is_null(&cs_ic(cs)->buf)) || ret == -1) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 1151 | cs_rx_room_blk(cs); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1152 | return 1; /* retry */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1153 | } |
| 1154 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1155 | return -1; /* error */ |
| 1156 | } |
| 1157 | return framesz; |
| 1158 | } |
| 1159 | |
| 1160 | /* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0 |
| 1161 | * when the frame can be ignored, 1 to retry later and the frame length on |
| 1162 | * success. */ |
| 1163 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1164 | spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1165 | { |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1166 | struct stconn *cs = appctx_cs(appctx); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1167 | int ret; |
| 1168 | uint32_t netint; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1169 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1170 | ret = co_getblk(cs_oc(cs), (char *)&netint, 4, 0); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1171 | if (ret > 0) { |
| 1172 | framesz = ntohl(netint); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1173 | if (framesz > SPOE_APPCTX(appctx)->max_frame_size) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1174 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1175 | return -1; |
| 1176 | } |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1177 | ret = co_getblk(cs_oc(cs), buf, framesz, 4); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1178 | } |
| 1179 | if (ret <= 0) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1180 | if (ret == 0) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1181 | return 1; /* retry */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1182 | } |
| 1183 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1184 | return -1; /* error */ |
| 1185 | } |
| 1186 | return framesz; |
| 1187 | } |
| 1188 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1189 | /******************************************************************** |
| 1190 | * Functions that manage the SPOE applet |
| 1191 | ********************************************************************/ |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1192 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1193 | spoe_wakeup_appctx(struct appctx *appctx) |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1194 | { |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 1195 | cs_want_get(appctx_cs(appctx)); |
| 1196 | cs_rx_endp_more(appctx_cs(appctx)); |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1197 | appctx_wakeup(appctx); |
| 1198 | return 1; |
| 1199 | } |
| 1200 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1201 | /* Callback function that catches applet timeouts. If a timeout occurred, we set |
| 1202 | * <appctx->st1> flag and the SPOE applet is woken up. */ |
| 1203 | static struct task * |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1204 | spoe_process_appctx(struct task * task, void *context, unsigned int state) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1205 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 1206 | struct appctx *appctx = context; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1207 | |
| 1208 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1209 | if (tick_is_expired(task->expire, now_ms)) { |
| 1210 | task->expire = TICK_ETERNITY; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1211 | appctx->st1 = SPOE_APPCTX_ERR_TOUT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1212 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1213 | spoe_wakeup_appctx(appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1214 | return task; |
| 1215 | } |
| 1216 | |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 1217 | static int |
| 1218 | spoe_init_appctx(struct appctx *appctx) |
| 1219 | { |
| 1220 | struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx); |
| 1221 | struct spoe_agent *agent = spoe_appctx->agent; |
| 1222 | struct task *task; |
| 1223 | struct stream *s; |
| 1224 | |
| 1225 | if ((task = task_new_here()) == NULL) |
Christopher Faulet | fa463af | 2022-05-18 07:42:49 +0200 | [diff] [blame] | 1226 | goto out_error; |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 1227 | task->process = spoe_process_appctx; |
| 1228 | task->context = appctx; |
| 1229 | |
| 1230 | if (appctx_finalize_startup(appctx, &agent->spoe_conf->agent_fe, &BUF_NULL) == -1) |
Christopher Faulet | fa463af | 2022-05-18 07:42:49 +0200 | [diff] [blame] | 1231 | goto out_free_task; |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 1232 | |
| 1233 | spoe_appctx->owner = appctx; |
| 1234 | spoe_appctx->task = task; |
| 1235 | |
| 1236 | LIST_INIT(&spoe_appctx->buffer_wait.list); |
| 1237 | spoe_appctx->buffer_wait.target = appctx; |
| 1238 | spoe_appctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx; |
| 1239 | |
| 1240 | s = appctx_strm(appctx); |
| 1241 | stream_set_backend(s, agent->b.be); |
| 1242 | |
| 1243 | /* applet is waiting for data */ |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1244 | cs_cant_get(s->scf); |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 1245 | |
| 1246 | s->do_log = NULL; |
| 1247 | s->res.flags |= CF_READ_DONTWAIT; |
| 1248 | |
| 1249 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
| 1250 | LIST_APPEND(&agent->rt[tid].applets, &spoe_appctx->list); |
| 1251 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
| 1252 | _HA_ATOMIC_INC(&agent->counters.applets); |
| 1253 | |
| 1254 | appctx->st0 = SPOE_APPCTX_ST_CONNECT; |
| 1255 | task_wakeup(spoe_appctx->task, TASK_WOKEN_INIT); |
| 1256 | return 0; |
| 1257 | out_free_task: |
| 1258 | task_destroy(task); |
| 1259 | out_error: |
| 1260 | return -1; |
| 1261 | } |
| 1262 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1263 | /* Callback function that releases a SPOE applet. This happens when the |
| 1264 | * connection with the agent is closed. */ |
| 1265 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1266 | spoe_release_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1267 | { |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1268 | struct stconn *cs = appctx_cs(appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1269 | struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx); |
| 1270 | struct spoe_agent *agent; |
| 1271 | struct spoe_context *ctx, *back; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1272 | |
| 1273 | if (spoe_appctx == NULL) |
| 1274 | return; |
| 1275 | |
Willy Tarreau | 23a2407 | 2022-05-05 20:18:44 +0200 | [diff] [blame] | 1276 | appctx->svcctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1277 | agent = spoe_appctx->agent; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1278 | |
| 1279 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n", |
| 1280 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1281 | __FUNCTION__, appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1282 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1283 | /* Remove applet from the list of running applets */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1284 | _HA_ATOMIC_DEC(&agent->counters.applets); |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1285 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1286 | if (!LIST_ISEMPTY(&spoe_appctx->list)) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1287 | LIST_DELETE(&spoe_appctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1288 | LIST_INIT(&spoe_appctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1289 | } |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1290 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1291 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1292 | /* Shutdown the server connection, if needed */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1293 | if (appctx->st0 != SPOE_APPCTX_ST_END) { |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1294 | if (appctx->st0 == SPOE_APPCTX_ST_IDLE) { |
| 1295 | eb32_delete(&spoe_appctx->node); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1296 | _HA_ATOMIC_DEC(&agent->counters.idles); |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1297 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1298 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1299 | appctx->st0 = SPOE_APPCTX_ST_END; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1300 | if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE) |
| 1301 | spoe_appctx->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1302 | |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 1303 | cs_shutw(cs); |
| 1304 | cs_shutr(cs); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1305 | cs_ic(cs)->flags |= CF_READ_NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1306 | } |
| 1307 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1308 | /* Destroy the task attached to this applet */ |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 1309 | task_destroy(spoe_appctx->task); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1310 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1311 | /* Notify all waiting streams */ |
| 1312 | list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1313 | LIST_DELETE(&ctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1314 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1315 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1316 | spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting); |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1317 | ctx->spoe_appctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1318 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1319 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1320 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1321 | } |
| 1322 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1323 | /* If the applet was processing a fragmented frame, notify the |
| 1324 | * corresponding stream. */ |
| 1325 | if (spoe_appctx->frag_ctx.ctx) { |
| 1326 | ctx = spoe_appctx->frag_ctx.ctx; |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1327 | ctx->spoe_appctx = NULL; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1328 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1329 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1330 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1331 | } |
| 1332 | |
Willy Tarreau | b042e4f | 2022-02-15 16:49:37 +0100 | [diff] [blame] | 1333 | if (!LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) { |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1334 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) { |
| 1335 | if (ctx->spoe_appctx == spoe_appctx) |
| 1336 | ctx->spoe_appctx = NULL; |
| 1337 | } |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1338 | goto end; |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1339 | } |
Christopher Faulet | 6f1296b | 2021-08-02 17:53:56 +0200 | [diff] [blame] | 1340 | else { |
| 1341 | /* It is the last running applet and the sending and waiting |
| 1342 | * queues are not empty. Try to start a new one if HAproxy is |
| 1343 | * not stopping. |
| 1344 | */ |
| 1345 | if (!stopping && |
| 1346 | (!LIST_ISEMPTY(&agent->rt[tid].sending_queue) || !LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) && |
| 1347 | spoe_create_appctx(agent->spoe_conf)) |
| 1348 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1349 | |
Christopher Faulet | 6f1296b | 2021-08-02 17:53:56 +0200 | [diff] [blame] | 1350 | /* otherwise, notify all waiting streams */ |
| 1351 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) { |
| 1352 | LIST_DELETE(&ctx->list); |
| 1353 | LIST_INIT(&ctx->list); |
| 1354 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
| 1355 | spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue); |
| 1356 | ctx->spoe_appctx = NULL; |
| 1357 | ctx->state = SPOE_CTX_ST_ERROR; |
| 1358 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
| 1359 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1360 | } |
| 1361 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) { |
| 1362 | LIST_DELETE(&ctx->list); |
| 1363 | LIST_INIT(&ctx->list); |
| 1364 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
| 1365 | spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting); |
| 1366 | ctx->spoe_appctx = NULL; |
| 1367 | ctx->state = SPOE_CTX_ST_ERROR; |
| 1368 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
| 1369 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1370 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1371 | } |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1372 | |
| 1373 | end: |
Christopher Faulet | 55ae8a6 | 2019-05-23 22:47:48 +0200 | [diff] [blame] | 1374 | /* Release allocated memory */ |
| 1375 | spoe_release_buffer(&spoe_appctx->buffer, |
| 1376 | &spoe_appctx->buffer_wait); |
| 1377 | pool_free(pool_head_spoe_appctx, spoe_appctx); |
| 1378 | |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1379 | /* Update runtinme agent info */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1380 | agent->rt[tid].frame_size = agent->max_frame_size; |
| 1381 | list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list) |
| 1382 | 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] | 1383 | } |
| 1384 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1385 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1386 | spoe_handle_connect_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1387 | { |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1388 | struct stconn *cs = appctx_cs(appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1389 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1390 | char *frame, *buf; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1391 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1392 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1393 | if (cs_state_in(cs->state, CS_SB_CER|CS_SB_DIS|CS_SB_CLO)) { |
Willy Tarreau | 7ab22adb | 2019-06-05 14:53:22 +0200 | [diff] [blame] | 1394 | /* closed */ |
| 1395 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
| 1396 | goto exit; |
| 1397 | } |
| 1398 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1399 | if (!cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST)) { |
Willy Tarreau | 7ab22adb | 2019-06-05 14:53:22 +0200 | [diff] [blame] | 1400 | /* not connected yet */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 1401 | cs_rx_endp_more(cs); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1402 | task_wakeup(__cs_strm(cs)->task, TASK_WOKEN_MSG); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1403 | goto stop; |
| 1404 | } |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1405 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1406 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1407 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1408 | " - Connection timed out\n", |
| 1409 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1410 | __FUNCTION__, appctx); |
| 1411 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1412 | goto exit; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1413 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1414 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1415 | if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1416 | SPOE_APPCTX(appctx)->task->expire = |
| 1417 | tick_add_ifset(now_ms, agent->timeout.hello); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1418 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1419 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1420 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1421 | buf = trash.area; frame = buf+4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1422 | ret = spoe_prepare_hahello_frame(appctx, frame, |
| 1423 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1424 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1425 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1426 | |
| 1427 | switch (ret) { |
| 1428 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1429 | case 0: /* ignore => an error, cannot be ignored */ |
| 1430 | goto exit; |
| 1431 | |
| 1432 | case 1: /* retry later */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1433 | goto stop; |
| 1434 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1435 | default: |
| 1436 | /* HELLO frame successfully sent, now wait for the |
| 1437 | * reply. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1438 | appctx->st0 = SPOE_APPCTX_ST_CONNECTING; |
| 1439 | goto next; |
| 1440 | } |
| 1441 | |
| 1442 | next: |
| 1443 | return 0; |
| 1444 | stop: |
| 1445 | return 1; |
| 1446 | exit: |
| 1447 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1448 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1449 | } |
| 1450 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1451 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1452 | spoe_handle_connecting_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1453 | { |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1454 | struct stconn *cs = appctx_cs(appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1455 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1456 | char *frame; |
| 1457 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1458 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1459 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1460 | if (cs->state == CS_ST_CLO || cs_opposite(cs)->state == CS_ST_CLO) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1461 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1462 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1463 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1464 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1465 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1466 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1467 | " - Connection timed out\n", |
| 1468 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1469 | __FUNCTION__, appctx); |
| 1470 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1471 | goto exit; |
| 1472 | } |
| 1473 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1474 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1475 | ret = spoe_recv_frame(appctx, frame, |
| 1476 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1477 | if (ret > 1) { |
| 1478 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1479 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1480 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1481 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1482 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1483 | ret = spoe_handle_agenthello_frame(appctx, frame, ret); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1484 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1485 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1486 | switch (ret) { |
| 1487 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1488 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1489 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1490 | goto next; |
| 1491 | |
| 1492 | case 1: /* retry later */ |
| 1493 | goto stop; |
| 1494 | |
| 1495 | default: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1496 | _HA_ATOMIC_INC(&agent->counters.idles); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1497 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1498 | SPOE_APPCTX(appctx)->node.key = 0; |
| 1499 | eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node); |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1500 | |
| 1501 | /* Update runtinme agent info */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1502 | 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] | 1503 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1504 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1505 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1506 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1507 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1508 | if (trash.data) |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1509 | co_skip(cs_oc(cs), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1510 | |
| 1511 | SPOE_APPCTX(appctx)->task->expire = |
| 1512 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1513 | return 0; |
| 1514 | stop: |
| 1515 | return 1; |
| 1516 | exit: |
| 1517 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1518 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1519 | } |
| 1520 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1521 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1522 | static int |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1523 | spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1524 | { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1525 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
| 1526 | struct spoe_context *ctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1527 | char *frame, *buf; |
| 1528 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1529 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1530 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1531 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1532 | buf = trash.area; frame = buf+4; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1533 | |
| 1534 | if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) { |
| 1535 | ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx; |
| 1536 | ret = spoe_prepare_hafrag_frame(appctx, ctx, frame, |
| 1537 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1538 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1539 | else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1540 | *skip = 1; |
| 1541 | ret = 1; |
| 1542 | goto end; |
| 1543 | } |
| 1544 | else { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1545 | ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1546 | ret = spoe_prepare_hanotify_frame(appctx, ctx, frame, |
| 1547 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1548 | |
| 1549 | } |
| 1550 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1551 | if (ret > 1) |
| 1552 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1553 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1554 | switch (ret) { |
| 1555 | case -1: /* error */ |
| 1556 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1557 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1558 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1559 | case 0: /* ignore */ |
| 1560 | if (ctx == NULL) |
| 1561 | goto abort_frag_frame; |
| 1562 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1563 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1564 | LIST_DELETE(&ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1565 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1566 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1567 | spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue); |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1568 | ctx->spoe_appctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1569 | ctx->state = SPOE_CTX_ST_ERROR; |
| 1570 | ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100); |
| 1571 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1572 | *skip = 1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1573 | break; |
| 1574 | |
| 1575 | case 1: /* retry */ |
| 1576 | *skip = 1; |
| 1577 | break; |
| 1578 | |
| 1579 | default: |
| 1580 | if (ctx == NULL) |
| 1581 | goto abort_frag_frame; |
| 1582 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1583 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1584 | LIST_DELETE(&ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1585 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1586 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1587 | spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue); |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1588 | ctx->spoe_appctx = SPOE_APPCTX(appctx); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1589 | if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) || |
| 1590 | (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN)) |
| 1591 | goto no_frag_frame_sent; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1592 | else |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1593 | goto frag_frame_sent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1594 | } |
| 1595 | goto end; |
| 1596 | |
| 1597 | frag_frame_sent: |
| 1598 | appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1599 | *skip = 1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1600 | SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx; |
| 1601 | SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id; |
| 1602 | SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1603 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
| 1604 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1605 | goto end; |
| 1606 | |
| 1607 | no_frag_frame_sent: |
| 1608 | if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) { |
| 1609 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1610 | LIST_APPEND(&agent->rt[tid].waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1611 | } |
| 1612 | else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) { |
| 1613 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1614 | LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1615 | } |
| 1616 | else { |
| 1617 | appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1618 | *skip = 1; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1619 | LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1620 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1621 | _HA_ATOMIC_INC(&agent->counters.nb_waiting); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1622 | ctx->stats.tv_wait = now; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1623 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1624 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1625 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1626 | SPOE_APPCTX(appctx)->cur_fpa++; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1627 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1628 | ctx->state = SPOE_CTX_ST_WAITING_ACK; |
| 1629 | goto end; |
| 1630 | |
| 1631 | abort_frag_frame: |
| 1632 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1633 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1634 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1635 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1636 | goto end; |
| 1637 | |
| 1638 | end: |
| 1639 | return ret; |
| 1640 | } |
| 1641 | |
| 1642 | static int |
| 1643 | spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip) |
| 1644 | { |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 1645 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1646 | struct spoe_context *ctx = NULL; |
| 1647 | char *frame; |
| 1648 | int ret; |
| 1649 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1650 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1651 | ret = spoe_recv_frame(appctx, frame, |
| 1652 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1653 | if (ret > 1) { |
| 1654 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1655 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1656 | ret = -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1657 | goto end; |
| 1658 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1659 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1660 | ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret); |
| 1661 | } |
| 1662 | switch (ret) { |
| 1663 | case -1: /* error */ |
| 1664 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1665 | break; |
| 1666 | |
| 1667 | case 0: /* ignore */ |
| 1668 | break; |
| 1669 | |
| 1670 | case 1: /* retry */ |
| 1671 | *skip = 1; |
| 1672 | break; |
| 1673 | |
| 1674 | default: |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1675 | LIST_DELETE(&ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1676 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1677 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1678 | spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting); |
| 1679 | ctx->stats.tv_response = now; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1680 | if (ctx->spoe_appctx) { |
| 1681 | ctx->spoe_appctx->cur_fpa--; |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1682 | ctx->spoe_appctx = NULL; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1683 | } |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1684 | if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY && |
| 1685 | ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) { |
| 1686 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1687 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1688 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1689 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1690 | } |
| 1691 | else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) |
| 1692 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1693 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1694 | break; |
| 1695 | } |
| 1696 | |
| 1697 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1698 | if (trash.data) |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 1699 | co_skip(cs_oc(appctx_cs(appctx)), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1700 | end: |
| 1701 | return ret; |
| 1702 | } |
| 1703 | |
| 1704 | static int |
| 1705 | spoe_handle_processing_appctx(struct appctx *appctx) |
| 1706 | { |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1707 | struct stconn *cs = appctx_cs(appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1708 | struct server *srv = objt_server(__cs_strm(cs)->target); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1709 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1710 | 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] | 1711 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1712 | if (cs->state == CS_ST_CLO || cs_opposite(cs)->state == CS_ST_CLO) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1713 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
| 1714 | goto exit; |
| 1715 | } |
| 1716 | |
| 1717 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
| 1718 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
| 1719 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1720 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1721 | goto next; |
| 1722 | } |
| 1723 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1724 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1725 | " - 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] | 1726 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1727 | __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa, |
| 1728 | agent->max_fpa, spoe_appctx_state_str[appctx->st0], |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1729 | SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1730 | |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1731 | |
| 1732 | /* Close the applet ASAP because some sessions are waiting for a free |
| 1733 | * connection slot. It is only an issue in multithreaded mode. |
| 1734 | */ |
| 1735 | close_asap = (global.nbthread > 1 && |
| 1736 | (agent->b.be->queue.length || |
| 1737 | (srv && (srv->queue.length || (srv->maxconn && srv->served >= srv_dynamic_maxconn(srv)))))); |
| 1738 | |
| 1739 | /* Don"t try to send new frame we are waiting for at lease a ack, in |
| 1740 | * sync mode or if applet must be closed ASAP |
| 1741 | */ |
| 1742 | 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] | 1743 | skip_sending = 1; |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1744 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1745 | /* receiving_frame loop */ |
| 1746 | while (!skip_receiving) { |
| 1747 | ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving); |
| 1748 | switch (ret) { |
| 1749 | case -1: /* error */ |
| 1750 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1751 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1752 | case 0: /* ignore */ |
| 1753 | active_r = 1; |
| 1754 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1755 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1756 | case 1: /* retry */ |
| 1757 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1758 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1759 | default: |
| 1760 | active_r = 1; |
| 1761 | break; |
| 1762 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1763 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1764 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1765 | /* send_frame loop */ |
| 1766 | while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) { |
| 1767 | ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending); |
| 1768 | switch (ret) { |
| 1769 | case -1: /* error */ |
| 1770 | goto next; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1771 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1772 | case 0: /* ignore */ |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1773 | if (SPOE_APPCTX(appctx)->node.key) |
| 1774 | SPOE_APPCTX(appctx)->node.key--; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1775 | active_s++; |
| 1776 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1777 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1778 | case 1: /* retry */ |
| 1779 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1780 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1781 | default: |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1782 | if (SPOE_APPCTX(appctx)->node.key) |
| 1783 | SPOE_APPCTX(appctx)->node.key--; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1784 | active_s++; |
| 1785 | break; |
| 1786 | } |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1787 | |
| 1788 | /* if applet must be close ASAP, don't send more than a frame */ |
| 1789 | if (close_asap) |
| 1790 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1791 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1792 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1793 | if (active_s || active_r) { |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1794 | update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s); |
| 1795 | SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle); |
| 1796 | } |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1797 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1798 | 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] | 1799 | /* If applet must be closed, don't switch it in IDLE state and |
| 1800 | * close it when the last waiting frame is acknowledged. |
Christopher Faulet | 9e647e5 | 2021-03-01 15:01:14 +0100 | [diff] [blame] | 1801 | */ |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1802 | if (close_asap) { |
| 1803 | if (SPOE_APPCTX(appctx)->cur_fpa) |
| 1804 | goto out; |
Christopher Faulet | 9e647e5 | 2021-03-01 15:01:14 +0100 | [diff] [blame] | 1805 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
| 1806 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1807 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1808 | goto next; |
| 1809 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1810 | _HA_ATOMIC_INC(&agent->counters.idles); |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1811 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1812 | eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1813 | } |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1814 | |
| 1815 | out: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1816 | return 1; |
| 1817 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1818 | next: |
| 1819 | SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle); |
| 1820 | return 0; |
| 1821 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1822 | exit: |
| 1823 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1824 | return 0; |
| 1825 | } |
| 1826 | |
| 1827 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1828 | spoe_handle_disconnect_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1829 | { |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1830 | struct stconn *cs = appctx_cs(appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1831 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1832 | char *frame, *buf; |
| 1833 | int ret; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1834 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1835 | if (cs->state == CS_ST_CLO || cs_opposite(cs)->state == CS_ST_CLO) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1836 | goto exit; |
| 1837 | |
| 1838 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) |
| 1839 | goto exit; |
| 1840 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1841 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1842 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1843 | buf = trash.area; frame = buf+4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1844 | ret = spoe_prepare_hadiscon_frame(appctx, frame, |
| 1845 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1846 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1847 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1848 | |
| 1849 | switch (ret) { |
| 1850 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1851 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1852 | goto exit; |
| 1853 | |
| 1854 | case 1: /* retry */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1855 | goto stop; |
| 1856 | |
| 1857 | default: |
| 1858 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1859 | " - disconnected by HAProxy (%d): %s\n", |
| 1860 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1861 | __FUNCTION__, appctx, |
| 1862 | SPOE_APPCTX(appctx)->status_code, |
| 1863 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1864 | |
| 1865 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1866 | goto next; |
| 1867 | } |
| 1868 | |
| 1869 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1870 | SPOE_APPCTX(appctx)->task->expire = |
| 1871 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1872 | return 0; |
| 1873 | stop: |
| 1874 | return 1; |
| 1875 | exit: |
| 1876 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1877 | return 0; |
| 1878 | } |
| 1879 | |
| 1880 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1881 | spoe_handle_disconnecting_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1882 | { |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1883 | struct stconn *cs = appctx_cs(appctx); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1884 | char *frame; |
| 1885 | int ret; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1886 | |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1887 | if (cs->state == CS_ST_CLO || cs_opposite(cs)->state == CS_ST_CLO) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1888 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1889 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1890 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1891 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1892 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1893 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1894 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1895 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1896 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1897 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1898 | ret = spoe_recv_frame(appctx, frame, |
| 1899 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1900 | if (ret > 1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1901 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1902 | ret = spoe_handle_agentdiscon_frame(appctx, frame, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | switch (ret) { |
| 1906 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1907 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1908 | " - error on frame (%s)\n", |
| 1909 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1910 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1911 | __FUNCTION__, appctx, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1912 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1913 | goto exit; |
| 1914 | |
| 1915 | case 0: /* ignore */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1916 | goto next; |
| 1917 | |
| 1918 | case 1: /* retry */ |
| 1919 | goto stop; |
| 1920 | |
| 1921 | default: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1922 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1923 | " - disconnected by peer (%d): %.*s\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1924 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1925 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1926 | __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code, |
| 1927 | SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1928 | goto exit; |
| 1929 | } |
| 1930 | |
| 1931 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1932 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1933 | if (trash.data) |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1934 | co_skip(cs_oc(cs), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1935 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1936 | return 0; |
| 1937 | stop: |
| 1938 | return 1; |
| 1939 | exit: |
| 1940 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1941 | return 0; |
| 1942 | } |
| 1943 | |
| 1944 | /* I/O Handler processing messages exchanged with the agent */ |
| 1945 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1946 | spoe_handle_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1947 | { |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1948 | struct stconn *cs = appctx_cs(appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1949 | struct spoe_agent *agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1950 | |
| 1951 | if (SPOE_APPCTX(appctx) == NULL) |
| 1952 | return; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1953 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1954 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
| 1955 | agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1956 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1957 | switchstate: |
| 1958 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1959 | " - appctx-state=%s\n", |
| 1960 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1961 | __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]); |
| 1962 | |
| 1963 | switch (appctx->st0) { |
| 1964 | case SPOE_APPCTX_ST_CONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1965 | if (spoe_handle_connect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1966 | goto out; |
| 1967 | goto switchstate; |
| 1968 | |
| 1969 | case SPOE_APPCTX_ST_CONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1970 | if (spoe_handle_connecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1971 | goto out; |
| 1972 | goto switchstate; |
| 1973 | |
| 1974 | case SPOE_APPCTX_ST_IDLE: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1975 | _HA_ATOMIC_DEC(&agent->counters.idles); |
Christopher Faulet | 7d9f1ba | 2018-02-28 13:33:26 +0100 | [diff] [blame] | 1976 | eb32_delete(&SPOE_APPCTX(appctx)->node); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1977 | if (stopping && |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1978 | LIST_ISEMPTY(&agent->rt[tid].sending_queue) && |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1979 | LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1980 | SPOE_APPCTX(appctx)->task->expire = |
| 1981 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1982 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1983 | goto switchstate; |
| 1984 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1985 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1986 | /* fall through */ |
| 1987 | |
| 1988 | case SPOE_APPCTX_ST_PROCESSING: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1989 | case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY: |
| 1990 | case SPOE_APPCTX_ST_WAITING_SYNC_ACK: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1991 | if (spoe_handle_processing_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1992 | goto out; |
| 1993 | goto switchstate; |
| 1994 | |
| 1995 | case SPOE_APPCTX_ST_DISCONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1996 | if (spoe_handle_disconnect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1997 | goto out; |
| 1998 | goto switchstate; |
| 1999 | |
| 2000 | case SPOE_APPCTX_ST_DISCONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2001 | if (spoe_handle_disconnecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2002 | goto out; |
| 2003 | goto switchstate; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2004 | |
| 2005 | case SPOE_APPCTX_ST_EXIT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2006 | appctx->st0 = SPOE_APPCTX_ST_END; |
| 2007 | SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY; |
| 2008 | |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 2009 | cs_shutw(cs); |
| 2010 | cs_shutr(cs); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 2011 | cs_ic(cs)->flags |= CF_READ_NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2012 | /* fall through */ |
| 2013 | |
| 2014 | case SPOE_APPCTX_ST_END: |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2015 | return; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2016 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2017 | out: |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2018 | if (stopping) |
| 2019 | spoe_wakeup_appctx(appctx); |
| 2020 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2021 | if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY) |
| 2022 | task_queue(SPOE_APPCTX(appctx)->task); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | struct applet spoe_applet = { |
| 2026 | .obj_type = OBJ_TYPE_APPLET, |
| 2027 | .name = "<SPOE>", /* used for logging */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2028 | .fct = spoe_handle_appctx, |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 2029 | .init = spoe_init_appctx, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2030 | .release = spoe_release_appctx, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2031 | }; |
| 2032 | |
| 2033 | /* Create a SPOE applet. On success, the created applet is returned, else |
| 2034 | * NULL. */ |
| 2035 | static struct appctx * |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2036 | spoe_create_appctx(struct spoe_config *conf) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2037 | { |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 2038 | struct spoe_appctx *spoe_appctx; |
| 2039 | struct appctx *appctx; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2040 | |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 2041 | spoe_appctx = pool_zalloc(pool_head_spoe_appctx); |
| 2042 | if (spoe_appctx == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2043 | goto out_error; |
| 2044 | |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 2045 | spoe_appctx->agent = conf->agent; |
| 2046 | spoe_appctx->version = 0; |
| 2047 | spoe_appctx->max_frame_size = conf->agent->max_frame_size; |
| 2048 | spoe_appctx->flags = 0; |
| 2049 | spoe_appctx->status_code = SPOE_FRM_ERR_NONE; |
| 2050 | spoe_appctx->buffer = BUF_NULL; |
| 2051 | spoe_appctx->cur_fpa = 0; |
| 2052 | LIST_INIT(&spoe_appctx->list); |
| 2053 | LIST_INIT(&spoe_appctx->waiting_queue); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2054 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2055 | |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 2056 | if ((appctx = appctx_new_here(&spoe_applet, NULL)) == NULL) |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 2057 | goto out_free_spoe_appctx; |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2058 | |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 2059 | appctx->svcctx = spoe_appctx; |
| 2060 | if (appctx_init(appctx) == -1) |
| 2061 | goto out_free_appctx; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2062 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2063 | appctx_wakeup(appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2064 | return appctx; |
| 2065 | |
| 2066 | /* Error unrolling */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2067 | out_free_appctx: |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 2068 | appctx_free_on_early_error(appctx); |
| 2069 | out_free_spoe_appctx: |
| 2070 | pool_free(pool_head_spoe_appctx, spoe_appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2071 | out_error: |
| 2072 | return NULL; |
| 2073 | } |
| 2074 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2075 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2076 | spoe_queue_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2077 | { |
| 2078 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2079 | struct spoe_agent *agent = conf->agent; |
| 2080 | struct appctx *appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2081 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2082 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2083 | /* Check if we need to create a new SPOE applet or not. */ |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 2084 | if (!eb_is_empty(&agent->rt[tid].idle_applets) && |
Christopher Faulet | eccfa61 | 2020-06-22 15:32:14 +0200 | [diff] [blame] | 2085 | (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] | 2086 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2087 | |
| 2088 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2089 | " - try to create new SPOE appctx\n", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2090 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__, |
| 2091 | ctx->strm); |
| 2092 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2093 | /* Do not try to create a new applet if there is no server up for the |
| 2094 | * agent's backend. */ |
| 2095 | if (!agent->b.be->srv_act && !agent->b.be->srv_bck) { |
| 2096 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2097 | " - cannot create SPOE appctx: no server up\n", |
| 2098 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2099 | __FUNCTION__, ctx->strm); |
| 2100 | goto end; |
| 2101 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2102 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2103 | /* Do not try to create a new applet if we have reached the maximum of |
| 2104 | * connection per seconds */ |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 2105 | if (agent->cps_max > 0) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2106 | 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] | 2107 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2108 | " - cannot create SPOE appctx: max CPS reached\n", |
| 2109 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2110 | __FUNCTION__, ctx->strm); |
| 2111 | goto end; |
| 2112 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2113 | } |
| 2114 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2115 | appctx = spoe_create_appctx(conf); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2116 | if (appctx == NULL) { |
| 2117 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2118 | " - failed to create SPOE appctx\n", |
| 2119 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2120 | __FUNCTION__, ctx->strm); |
Christopher Faulet | 3b8e349 | 2018-03-26 17:20:58 +0200 | [diff] [blame] | 2121 | send_log(&conf->agent_fe, LOG_EMERG, |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2122 | "SPOE: [%s] failed to create SPOE applet\n", |
| 2123 | agent->id); |
| 2124 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2125 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2126 | } |
| 2127 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2128 | /* Increase the per-process number of cumulated connections */ |
| 2129 | if (agent->cps_max > 0) |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2130 | update_freq_ctr(&agent->rt[tid].conn_per_sec, 1); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2131 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2132 | end: |
| 2133 | /* 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] | 2134 | if (LIST_ISEMPTY(&agent->rt[tid].applets)) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2135 | ctx->status_code = SPOE_CTX_ERR_RES; |
| 2136 | return -1; |
| 2137 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2138 | |
Christopher Faulet | 3e86cec | 2019-04-10 14:02:12 +0200 | [diff] [blame] | 2139 | /* Add the SPOE context in the sending queue if the stream has no applet |
| 2140 | * already assigned and wakeup all idle applets. Otherwise, don't queue |
| 2141 | * it. */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2142 | _HA_ATOMIC_INC(&agent->counters.nb_sending); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2143 | spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request); |
| 2144 | ctx->stats.tv_queue = now; |
Christopher Faulet | 3e86cec | 2019-04-10 14:02:12 +0200 | [diff] [blame] | 2145 | if (ctx->spoe_appctx) |
| 2146 | return 1; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2147 | LIST_APPEND(&agent->rt[tid].sending_queue, &ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2148 | |
| 2149 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2150 | " - Add stream in sending queue" |
Christopher Faulet | 68db023 | 2018-04-06 11:34:12 +0200 | [diff] [blame] | 2151 | " - applets=%u - idles=%u - processing=%u\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2152 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__, |
Christopher Faulet | 68db023 | 2018-04-06 11:34:12 +0200 | [diff] [blame] | 2153 | ctx->strm, agent->counters.applets, agent->counters.idles, |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2154 | agent->rt[tid].processing); |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2155 | |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 2156 | /* Finally try to wakeup an IDLE applet. */ |
| 2157 | if (!eb_is_empty(&agent->rt[tid].idle_applets)) { |
| 2158 | struct eb32_node *node; |
| 2159 | |
| 2160 | node = eb32_first(&agent->rt[tid].idle_applets); |
| 2161 | spoe_appctx = eb32_entry(node, struct spoe_appctx, node); |
| 2162 | if (node && spoe_appctx) { |
| 2163 | eb32_delete(&spoe_appctx->node); |
| 2164 | spoe_appctx->node.key++; |
| 2165 | eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node); |
| 2166 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2167 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2168 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2169 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | /*************************************************************************** |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2173 | * Functions that encode SPOE messages |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2174 | **************************************************************************/ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2175 | /* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle |
| 2176 | * fragmented_content. If the next message can be processed, it returns 0. If |
| 2177 | * the message is too big, it returns -1.*/ |
| 2178 | static int |
| 2179 | spoe_encode_message(struct stream *s, struct spoe_context *ctx, |
| 2180 | struct spoe_message *msg, int dir, |
| 2181 | char **buf, char *end) |
| 2182 | { |
| 2183 | struct sample *smp; |
| 2184 | struct spoe_arg *arg; |
| 2185 | int ret; |
| 2186 | |
| 2187 | if (msg->cond) { |
| 2188 | ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2189 | ret = acl_pass(ret); |
| 2190 | if (msg->cond->pol == ACL_COND_UNLESS) |
| 2191 | ret = !ret; |
| 2192 | |
| 2193 | /* the rule does not match */ |
| 2194 | if (!ret) |
| 2195 | goto next; |
| 2196 | } |
| 2197 | |
| 2198 | /* Resume encoding of a SPOE argument */ |
| 2199 | if (ctx->frag_ctx.curarg != NULL) { |
| 2200 | arg = ctx->frag_ctx.curarg; |
| 2201 | goto encode_argument; |
| 2202 | } |
| 2203 | |
| 2204 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2205 | goto encode_msg_payload; |
| 2206 | |
| 2207 | /* Check if there is enough space for the message name and the |
| 2208 | * number of arguments. It implies <msg->id_len> is encoded on 2 |
| 2209 | * bytes, at most (< 2288). */ |
| 2210 | if (*buf + 2 + msg->id_len + 1 > end) |
| 2211 | goto too_big; |
| 2212 | |
| 2213 | /* Encode the message name */ |
| 2214 | if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1) |
| 2215 | goto too_big; |
| 2216 | |
| 2217 | /* Set the number of arguments for this message */ |
| 2218 | **buf = msg->nargs; |
| 2219 | (*buf)++; |
| 2220 | |
| 2221 | ctx->frag_ctx.curoff = 0; |
| 2222 | encode_msg_payload: |
| 2223 | |
| 2224 | /* Loop on arguments */ |
| 2225 | list_for_each_entry(arg, &msg->args, list) { |
| 2226 | ctx->frag_ctx.curarg = arg; |
| 2227 | ctx->frag_ctx.curoff = UINT_MAX; |
Kevin Zhu | f7f5428 | 2019-04-26 14:00:01 +0800 | [diff] [blame] | 2228 | ctx->frag_ctx.curlen = 0; |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2229 | |
| 2230 | encode_argument: |
| 2231 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2232 | goto encode_arg_value; |
| 2233 | |
Joseph Herlant | f7f6031 | 2018-11-15 13:46:49 -0800 | [diff] [blame] | 2234 | /* Encode the argument name as a string. It can by NULL */ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2235 | if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1) |
| 2236 | goto too_big; |
| 2237 | |
| 2238 | ctx->frag_ctx.curoff = 0; |
| 2239 | encode_arg_value: |
| 2240 | |
Joseph Herlant | f7f6031 | 2018-11-15 13:46:49 -0800 | [diff] [blame] | 2241 | /* Fetch the argument value */ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2242 | 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] | 2243 | if (smp) { |
| 2244 | smp->ctx.a[0] = &ctx->frag_ctx.curlen; |
| 2245 | smp->ctx.a[1] = &ctx->frag_ctx.curoff; |
| 2246 | } |
Christopher Faulet | 85db321 | 2019-04-26 14:30:15 +0200 | [diff] [blame] | 2247 | ret = spoe_encode_data(smp, buf, end); |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2248 | if (ret == -1 || ctx->frag_ctx.curoff) |
| 2249 | goto too_big; |
| 2250 | } |
| 2251 | |
| 2252 | next: |
| 2253 | return 0; |
| 2254 | |
| 2255 | too_big: |
| 2256 | return -1; |
| 2257 | } |
| 2258 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2259 | /* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to |
| 2260 | * handle fragmented content. On success it returns 1. If an error occurred, -1 |
| 2261 | * is returned. If nothing has been encoded, it returns 0 (this is only possible |
| 2262 | * for unfragmented payload). */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2263 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2264 | spoe_encode_messages(struct stream *s, struct spoe_context *ctx, |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2265 | struct list *messages, int dir, int type) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2266 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2267 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2268 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2269 | struct spoe_message *msg; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2270 | char *p, *end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2271 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2272 | p = b_head(&ctx->buffer); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2273 | end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2274 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2275 | if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */ |
| 2276 | /* Resume encoding of a SPOE message */ |
| 2277 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2278 | msg = ctx->frag_ctx.curmsg; |
| 2279 | goto encode_evt_message; |
| 2280 | } |
| 2281 | |
| 2282 | list_for_each_entry(msg, messages, by_evt) { |
| 2283 | ctx->frag_ctx.curmsg = msg; |
| 2284 | ctx->frag_ctx.curarg = NULL; |
| 2285 | ctx->frag_ctx.curoff = UINT_MAX; |
| 2286 | |
| 2287 | encode_evt_message: |
| 2288 | if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1) |
| 2289 | goto too_big; |
| 2290 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2291 | } |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2292 | else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */ |
| 2293 | /* Resume encoding of a SPOE message */ |
| 2294 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2295 | msg = ctx->frag_ctx.curmsg; |
| 2296 | goto encode_grp_message; |
| 2297 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2298 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2299 | list_for_each_entry(msg, messages, by_grp) { |
| 2300 | ctx->frag_ctx.curmsg = msg; |
| 2301 | ctx->frag_ctx.curarg = NULL; |
| 2302 | ctx->frag_ctx.curoff = UINT_MAX; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2303 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2304 | encode_grp_message: |
| 2305 | if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1) |
| 2306 | goto too_big; |
| 2307 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2308 | } |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2309 | else |
| 2310 | goto skip; |
| 2311 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2312 | |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2313 | /* nothing has been encoded for an unfragmented payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2314 | if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer)) |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2315 | goto skip; |
| 2316 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2317 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2318 | " - encode %s messages - spoe_appctx=%p" |
| 2319 | "- max_size=%u - encoded=%ld\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2320 | (int)now.tv_sec, (int)now.tv_usec, |
| 2321 | agent->id, __FUNCTION__, s, |
| 2322 | ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"), |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2323 | ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE), |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2324 | p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2325 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2326 | b_set_data(&ctx->buffer, p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2327 | ctx->frag_ctx.curmsg = NULL; |
| 2328 | ctx->frag_ctx.curarg = NULL; |
| 2329 | ctx->frag_ctx.curoff = 0; |
| 2330 | ctx->frag_ctx.flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2331 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2332 | return 1; |
| 2333 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2334 | too_big: |
Christopher Faulet | a715ea8 | 2019-04-10 14:21:51 +0200 | [diff] [blame] | 2335 | /* Return an error if fragmentation is unsupported or if nothing has |
| 2336 | * been encoded because its too big and not splittable. */ |
| 2337 | if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) { |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 2338 | ctx->status_code = SPOE_CTX_ERR_TOO_BIG; |
| 2339 | return -1; |
| 2340 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2341 | |
| 2342 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2343 | " - encode fragmented messages - spoe_appctx=%p" |
| 2344 | " - curmsg=%p - curarg=%p - curoff=%u" |
| 2345 | " - max_size=%u - encoded=%ld\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2346 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2347 | agent->id, __FUNCTION__, s, ctx->spoe_appctx, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2348 | ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff, |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2349 | (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] | 2350 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2351 | b_set_data(&ctx->buffer, p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2352 | ctx->flags |= SPOE_CTX_FL_FRAGMENTED; |
| 2353 | ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN; |
| 2354 | return 1; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2355 | |
| 2356 | skip: |
| 2357 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2358 | " - skip the frame because nothing has been encoded\n", |
| 2359 | (int)now.tv_sec, (int)now.tv_usec, |
| 2360 | agent->id, __FUNCTION__, s); |
| 2361 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2362 | } |
| 2363 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2364 | |
| 2365 | /*************************************************************************** |
| 2366 | * Functions that handle SPOE actions |
| 2367 | **************************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2368 | /* Helper function to set a variable */ |
| 2369 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2370 | 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] | 2371 | struct sample *smp) |
| 2372 | { |
| 2373 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2374 | struct spoe_agent *agent = conf->agent; |
| 2375 | char varname[64]; |
| 2376 | |
| 2377 | memset(varname, 0, sizeof(varname)); |
| 2378 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2379 | scope, agent->var_pfx, len, name); |
Etienne Carriere | aec8989 | 2017-12-14 09:36:40 +0000 | [diff] [blame] | 2380 | if (agent->flags & SPOE_FL_FORCE_SET_VAR) |
| 2381 | vars_set_by_name(varname, len, smp); |
| 2382 | else |
| 2383 | vars_set_by_name_ifexist(varname, len, smp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | /* Helper function to unset a variable */ |
| 2387 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2388 | 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] | 2389 | struct sample *smp) |
| 2390 | { |
| 2391 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2392 | struct spoe_agent *agent = conf->agent; |
| 2393 | char varname[64]; |
| 2394 | |
| 2395 | memset(varname, 0, sizeof(varname)); |
| 2396 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2397 | scope, agent->var_pfx, len, name); |
| 2398 | vars_unset_by_name_ifexist(varname, len, smp); |
| 2399 | } |
| 2400 | |
| 2401 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2402 | static inline int |
| 2403 | spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx, |
| 2404 | char **buf, char *end, int dir) |
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 | char *str, *scope, *p = *buf; |
| 2407 | struct sample smp; |
| 2408 | uint64_t sz; |
| 2409 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2410 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2411 | if (p + 2 >= end) |
| 2412 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2413 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2414 | /* SET-VAR requires 3 arguments */ |
| 2415 | if (*p++ != 3) |
| 2416 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2417 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2418 | switch (*p++) { |
| 2419 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2420 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2421 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2422 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2423 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2424 | default: goto skip; |
| 2425 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2426 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2427 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2428 | goto skip; |
| 2429 | memset(&smp, 0, sizeof(smp)); |
| 2430 | 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] | 2431 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2432 | if (spoe_decode_data(&p, end, &smp) == -1) |
| 2433 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2434 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2435 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2436 | " - set-var '%s.%s.%.*s'\n", |
| 2437 | (int)now.tv_sec, (int)now.tv_usec, |
| 2438 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2439 | __FUNCTION__, s, scope, |
| 2440 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2441 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2442 | |
Christopher Faulet | 2469eba | 2020-10-15 16:08:30 +0200 | [diff] [blame] | 2443 | if (smp.data.type == SMP_T_ANY) |
| 2444 | spoe_unset_var(ctx, scope, str, sz, &smp); |
| 2445 | else |
| 2446 | spoe_set_var(ctx, scope, str, sz, &smp); |
Christopher Faulet | b5cff60 | 2016-11-24 14:53:22 +0100 | [diff] [blame] | 2447 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2448 | ret = (p - *buf); |
| 2449 | *buf = p; |
| 2450 | return ret; |
| 2451 | skip: |
| 2452 | return 0; |
| 2453 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2454 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2455 | static inline int |
| 2456 | spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx, |
| 2457 | char **buf, char *end, int dir) |
| 2458 | { |
| 2459 | char *str, *scope, *p = *buf; |
| 2460 | struct sample smp; |
| 2461 | uint64_t sz; |
| 2462 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2463 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2464 | if (p + 2 >= end) |
| 2465 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2466 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2467 | /* UNSET-VAR requires 2 arguments */ |
| 2468 | if (*p++ != 2) |
| 2469 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2470 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2471 | switch (*p++) { |
| 2472 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2473 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2474 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2475 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2476 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2477 | default: goto skip; |
| 2478 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2479 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2480 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2481 | goto skip; |
| 2482 | memset(&smp, 0, sizeof(smp)); |
| 2483 | 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] | 2484 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2485 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2486 | " - unset-var '%s.%s.%.*s'\n", |
| 2487 | (int)now.tv_sec, (int)now.tv_usec, |
| 2488 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2489 | __FUNCTION__, s, scope, |
| 2490 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2491 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2492 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2493 | spoe_unset_var(ctx, scope, str, sz, &smp); |
| 2494 | |
| 2495 | ret = (p - *buf); |
| 2496 | *buf = p; |
| 2497 | return ret; |
| 2498 | skip: |
| 2499 | return 0; |
| 2500 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2501 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2502 | /* Process SPOE actions for a specific event. It returns 1 on success. If an |
| 2503 | * error occurred, 0 is returned. */ |
| 2504 | static int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2505 | spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2506 | { |
| 2507 | char *p, *end; |
| 2508 | int ret; |
| 2509 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2510 | p = b_head(&ctx->buffer); |
| 2511 | end = p + b_data(&ctx->buffer); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2512 | |
| 2513 | while (p < end) { |
| 2514 | enum spoe_action_type type; |
| 2515 | |
| 2516 | type = *p++; |
| 2517 | switch (type) { |
| 2518 | case SPOE_ACT_T_SET_VAR: |
| 2519 | ret = spoe_decode_action_set_var(s, ctx, &p, end, dir); |
| 2520 | if (!ret) |
| 2521 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2522 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2523 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2524 | case SPOE_ACT_T_UNSET_VAR: |
| 2525 | ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir); |
| 2526 | if (!ret) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2527 | goto skip; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2528 | break; |
| 2529 | |
| 2530 | default: |
| 2531 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2532 | } |
| 2533 | } |
| 2534 | |
| 2535 | return 1; |
| 2536 | skip: |
| 2537 | return 0; |
| 2538 | } |
| 2539 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2540 | /*************************************************************************** |
| 2541 | * Functions that process SPOE events |
| 2542 | **************************************************************************/ |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2543 | static void |
| 2544 | spoe_update_stats(struct stream *s, struct spoe_agent *agent, |
| 2545 | struct spoe_context *ctx, int dir) |
| 2546 | { |
| 2547 | if (!tv_iszero(&ctx->stats.tv_start)) { |
| 2548 | spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process); |
| 2549 | ctx->stats.t_total += ctx->stats.t_process; |
| 2550 | tv_zero(&ctx->stats.tv_request); |
| 2551 | tv_zero(&ctx->stats.tv_queue); |
| 2552 | tv_zero(&ctx->stats.tv_wait); |
| 2553 | tv_zero(&ctx->stats.tv_response); |
| 2554 | } |
| 2555 | |
| 2556 | if (agent->var_t_process) { |
| 2557 | struct sample smp; |
| 2558 | |
| 2559 | memset(&smp, 0, sizeof(smp)); |
| 2560 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2561 | smp.data.u.sint = ctx->stats.t_process; |
| 2562 | smp.data.type = SMP_T_SINT; |
| 2563 | |
| 2564 | spoe_set_var(ctx, "txn", agent->var_t_process, |
| 2565 | strlen(agent->var_t_process), &smp); |
| 2566 | } |
| 2567 | |
| 2568 | if (agent->var_t_total) { |
| 2569 | struct sample smp; |
| 2570 | |
| 2571 | memset(&smp, 0, sizeof(smp)); |
| 2572 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2573 | smp.data.u.sint = ctx->stats.t_total; |
| 2574 | smp.data.type = SMP_T_SINT; |
| 2575 | |
| 2576 | spoe_set_var(ctx, "txn", agent->var_t_total, |
| 2577 | strlen(agent->var_t_total), &smp); |
| 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | static void |
| 2582 | spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent, |
| 2583 | struct spoe_context *ctx, int dir) |
| 2584 | { |
| 2585 | if (agent->eps_max > 0) |
| 2586 | update_freq_ctr(&agent->rt[tid].err_per_sec, 1); |
| 2587 | |
| 2588 | if (agent->var_on_error) { |
| 2589 | struct sample smp; |
| 2590 | |
| 2591 | memset(&smp, 0, sizeof(smp)); |
| 2592 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2593 | smp.data.u.sint = ctx->status_code; |
| 2594 | smp.data.type = SMP_T_BOOL; |
| 2595 | |
| 2596 | spoe_set_var(ctx, "txn", agent->var_on_error, |
| 2597 | strlen(agent->var_on_error), &smp); |
| 2598 | } |
| 2599 | |
| 2600 | ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR) |
| 2601 | ? SPOE_CTX_ST_READY |
| 2602 | : SPOE_CTX_ST_NONE); |
| 2603 | } |
| 2604 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2605 | static inline int |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2606 | 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] | 2607 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2608 | /* If a process is already started for this SPOE context, retry |
| 2609 | * later. */ |
| 2610 | if (ctx->flags & SPOE_CTX_FL_PROCESS) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2611 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2612 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2613 | agent->rt[tid].processing++; |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2614 | ctx->stats.tv_start = now; |
| 2615 | ctx->stats.tv_request = now; |
| 2616 | ctx->stats.t_request = -1; |
| 2617 | ctx->stats.t_queue = -1; |
| 2618 | ctx->stats.t_waiting = -1; |
| 2619 | ctx->stats.t_response = -1; |
| 2620 | ctx->stats.t_process = -1; |
| 2621 | |
| 2622 | ctx->status_code = 0; |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2623 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2624 | /* Set the right flag to prevent request and response processing |
| 2625 | * in same time. */ |
| 2626 | ctx->flags |= ((dir == SMP_OPT_DIR_REQ) |
| 2627 | ? SPOE_CTX_FL_REQ_PROCESS |
| 2628 | : SPOE_CTX_FL_RSP_PROCESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2629 | return 1; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2630 | } |
| 2631 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2632 | static inline void |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2633 | spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2634 | { |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2635 | struct spoe_appctx *sa = ctx->spoe_appctx; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2636 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2637 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) |
| 2638 | return; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2639 | _HA_ATOMIC_INC(&agent->counters.nb_processed); |
Christopher Faulet | 879dca9 | 2018-03-23 11:53:24 +0100 | [diff] [blame] | 2640 | if (sa) { |
| 2641 | if (sa->frag_ctx.ctx == ctx) { |
| 2642 | sa->frag_ctx.ctx = NULL; |
| 2643 | spoe_wakeup_appctx(sa->owner); |
| 2644 | } |
| 2645 | else |
| 2646 | sa->cur_fpa--; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2647 | } |
| 2648 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2649 | /* Reset the flag to allow next processing */ |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2650 | agent->rt[tid].processing--; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2651 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2652 | |
| 2653 | /* Reset processing timer */ |
| 2654 | ctx->process_exp = TICK_ETERNITY; |
| 2655 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2656 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2657 | |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2658 | ctx->spoe_appctx = NULL; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2659 | ctx->frag_ctx.curmsg = NULL; |
| 2660 | ctx->frag_ctx.curarg = NULL; |
| 2661 | ctx->frag_ctx.curoff = 0; |
| 2662 | ctx->frag_ctx.flags = 0; |
| 2663 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2664 | if (!LIST_ISEMPTY(&ctx->list)) { |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2665 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2666 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2667 | else |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2668 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2669 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2670 | LIST_DELETE(&ctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2671 | LIST_INIT(&ctx->list); |
| 2672 | } |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2673 | } |
| 2674 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2675 | /* Process a list of SPOE messages. First, this functions will process messages |
| 2676 | * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame |
| 2677 | * to process corresponding actions. During all the processing, it returns 0 |
| 2678 | * and it returns 1 when the processing is finished. If an error occurred, -1 |
| 2679 | * is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2680 | static int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2681 | spoe_process_messages(struct stream *s, struct spoe_context *ctx, |
| 2682 | struct list *messages, int dir, int type) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2683 | { |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2684 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2685 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2686 | int ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2687 | |
| 2688 | if (ctx->state == SPOE_CTX_ST_ERROR) |
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 | |
| 2691 | if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) { |
| 2692 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2693 | " - failed to process messages: timeout\n", |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2694 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2695 | agent->id, __FUNCTION__, s); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2696 | ctx->status_code = SPOE_CTX_ERR_TOUT; |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2697 | goto end; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2698 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2699 | |
| 2700 | if (ctx->state == SPOE_CTX_ST_READY) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2701 | if (agent->eps_max > 0) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2702 | 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] | 2703 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2704 | " - skip processing of messages: max EPS reached\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2705 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2706 | agent->id, __FUNCTION__, s); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2707 | goto skip; |
| 2708 | } |
| 2709 | } |
| 2710 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2711 | if (!tick_isset(ctx->process_exp)) { |
| 2712 | ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing); |
| 2713 | s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire), |
| 2714 | ctx->process_exp); |
| 2715 | } |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2716 | ret = spoe_start_processing(agent, ctx, dir); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2717 | if (!ret) |
| 2718 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2719 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2720 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2721 | /* fall through */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2722 | } |
| 2723 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2724 | if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) { |
Christopher Faulet | 63263e5 | 2019-04-10 14:47:18 +0200 | [diff] [blame] | 2725 | if (tv_iszero(&ctx->stats.tv_request)) |
| 2726 | ctx->stats.tv_request = now; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2727 | if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait)) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2728 | goto out; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2729 | ret = spoe_encode_messages(s, ctx, messages, dir, type); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2730 | if (ret < 0) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2731 | goto end; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2732 | if (!ret) |
| 2733 | goto skip; |
Christopher Faulet | 333694d | 2018-01-12 10:45:47 +0100 | [diff] [blame] | 2734 | if (spoe_queue_context(ctx) < 0) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2735 | goto end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2736 | ctx->state = SPOE_CTX_ST_SENDING_MSGS; |
| 2737 | } |
| 2738 | |
| 2739 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) { |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2740 | if (ctx->spoe_appctx) |
| 2741 | spoe_wakeup_appctx(ctx->spoe_appctx->owner); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2742 | ret = 0; |
| 2743 | goto out; |
| 2744 | } |
| 2745 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2746 | if (ctx->state == SPOE_CTX_ST_WAITING_ACK) { |
| 2747 | ret = 0; |
| 2748 | goto out; |
| 2749 | } |
| 2750 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2751 | if (ctx->state == SPOE_CTX_ST_DONE) { |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2752 | spoe_process_actions(s, ctx, dir); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2753 | ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2754 | ctx->frame_id++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2755 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2756 | spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2757 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | out: |
| 2761 | return ret; |
| 2762 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2763 | skip: |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2764 | tv_zero(&ctx->stats.tv_start); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2765 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2766 | spoe_stop_processing(agent, ctx); |
| 2767 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2768 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2769 | end: |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2770 | spoe_update_stats(s, agent, ctx, dir); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2771 | spoe_stop_processing(agent, ctx); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2772 | if (ctx->status_code) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2773 | _HA_ATOMIC_INC(&agent->counters.nb_errors); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2774 | spoe_handle_processing_error(s, agent, ctx, dir); |
| 2775 | ret = 1; |
| 2776 | } |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2777 | return ret; |
| 2778 | } |
| 2779 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2780 | /* Process a SPOE group, ie the list of messages attached to the group <grp>. |
| 2781 | * See spoe_process_message for details. */ |
| 2782 | static int |
| 2783 | spoe_process_group(struct stream *s, struct spoe_context *ctx, |
| 2784 | struct spoe_group *group, int dir) |
| 2785 | { |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2786 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2787 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2788 | int ret; |
| 2789 | |
| 2790 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2791 | " - ctx-state=%s - Process messages for group=%s\n", |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2792 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2793 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2794 | group->id); |
| 2795 | |
| 2796 | if (LIST_ISEMPTY(&group->messages)) |
| 2797 | return 1; |
| 2798 | |
| 2799 | 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] | 2800 | if (ret && ctx->stats.t_process != -1) { |
| 2801 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2802 | " - <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] | 2803 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2804 | __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code, |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2805 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2806 | ctx->stats.t_response, ctx->stats.t_process, |
| 2807 | agent->counters.idles, agent->counters.applets, |
| 2808 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2809 | agent->counters.nb_errors, agent->counters.nb_processed, |
| 2810 | agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec)); |
| 2811 | if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM)) |
| 2812 | send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING), |
| 2813 | "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n", |
| 2814 | agent->id, group->id, s->uniq_id, ctx->status_code, |
| 2815 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
| 2816 | ctx->stats.t_response, ctx->stats.t_process, |
| 2817 | agent->counters.idles, agent->counters.applets, |
| 2818 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2819 | agent->counters.nb_errors, agent->counters.nb_processed); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2820 | } |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2821 | return ret; |
| 2822 | } |
| 2823 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2824 | /* Process a SPOE event, ie the list of messages attached to the event <ev>. |
| 2825 | * See spoe_process_message for details. */ |
| 2826 | static int |
| 2827 | spoe_process_event(struct stream *s, struct spoe_context *ctx, |
| 2828 | enum spoe_event ev) |
| 2829 | { |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2830 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2831 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2832 | int dir, ret; |
| 2833 | |
| 2834 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2835 | " - ctx-state=%s - Process messages for event=%s\n", |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2836 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2837 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2838 | spoe_event_str[ev]); |
| 2839 | |
| 2840 | dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 2841 | |
| 2842 | if (LIST_ISEMPTY(&(ctx->events[ev]))) |
| 2843 | return 1; |
| 2844 | |
| 2845 | 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] | 2846 | if (ret && ctx->stats.t_process != -1) { |
| 2847 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2848 | " - <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] | 2849 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2850 | __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code, |
| 2851 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2852 | ctx->stats.t_response, ctx->stats.t_process, |
| 2853 | agent->counters.idles, agent->counters.applets, |
| 2854 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2855 | agent->counters.nb_errors, agent->counters.nb_processed, |
| 2856 | agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec)); |
| 2857 | if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM)) |
| 2858 | send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING), |
| 2859 | "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n", |
| 2860 | agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code, |
| 2861 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
| 2862 | ctx->stats.t_response, ctx->stats.t_process, |
| 2863 | agent->counters.idles, agent->counters.applets, |
| 2864 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2865 | agent->counters.nb_errors, agent->counters.nb_processed); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2866 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2867 | return ret; |
| 2868 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2869 | |
| 2870 | /*************************************************************************** |
| 2871 | * Functions that create/destroy SPOE contexts |
| 2872 | **************************************************************************/ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2873 | static int |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2874 | spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2875 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2876 | if (buf->size) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2877 | return 1; |
| 2878 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2879 | if (LIST_INLIST(&buffer_wait->list)) |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2880 | LIST_DEL_INIT(&buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2881 | |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 2882 | if (b_alloc(buf)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2883 | return 1; |
| 2884 | |
Willy Tarreau | b4e3476 | 2021-09-30 19:02:18 +0200 | [diff] [blame] | 2885 | LIST_APPEND(&th_ctx->buffer_wq, &buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2886 | return 0; |
| 2887 | } |
| 2888 | |
| 2889 | static void |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2890 | spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2891 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2892 | if (LIST_INLIST(&buffer_wait->list)) |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2893 | LIST_DEL_INIT(&buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2894 | |
| 2895 | /* Release the buffer if needed */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2896 | if (buf->size) { |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2897 | b_free(buf); |
Willy Tarreau | 4d77bbf | 2021-02-20 12:02:46 +0100 | [diff] [blame] | 2898 | offer_buffers(buffer_wait->target, 1); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2899 | } |
| 2900 | } |
| 2901 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2902 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2903 | spoe_wakeup_context(struct spoe_context *ctx) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2904 | { |
| 2905 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 2906 | return 1; |
| 2907 | } |
| 2908 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2909 | static struct spoe_context * |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2910 | spoe_create_context(struct stream *s, struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2911 | { |
| 2912 | struct spoe_config *conf = FLT_CONF(filter); |
| 2913 | struct spoe_context *ctx; |
| 2914 | |
Willy Tarreau | c9ef9bc | 2021-03-22 21:04:50 +0100 | [diff] [blame] | 2915 | ctx = pool_zalloc(pool_head_spoe_ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2916 | if (ctx == NULL) { |
| 2917 | return NULL; |
| 2918 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2919 | ctx->filter = filter; |
| 2920 | ctx->state = SPOE_CTX_ST_NONE; |
| 2921 | ctx->status_code = SPOE_CTX_ERR_NONE; |
| 2922 | ctx->flags = 0; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2923 | ctx->events = conf->agent->events; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 2924 | ctx->groups = &conf->agent->groups; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2925 | ctx->buffer = BUF_NULL; |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2926 | LIST_INIT(&ctx->buffer_wait.list); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2927 | ctx->buffer_wait.target = ctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2928 | ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2929 | LIST_INIT(&ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2930 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2931 | ctx->stream_id = 0; |
| 2932 | ctx->frame_id = 1; |
| 2933 | ctx->process_exp = TICK_ETERNITY; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2934 | |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2935 | tv_zero(&ctx->stats.tv_start); |
| 2936 | tv_zero(&ctx->stats.tv_request); |
| 2937 | tv_zero(&ctx->stats.tv_queue); |
| 2938 | tv_zero(&ctx->stats.tv_wait); |
| 2939 | tv_zero(&ctx->stats.tv_response); |
| 2940 | ctx->stats.t_request = -1; |
| 2941 | ctx->stats.t_queue = -1; |
| 2942 | ctx->stats.t_waiting = -1; |
| 2943 | ctx->stats.t_response = -1; |
| 2944 | ctx->stats.t_process = -1; |
| 2945 | ctx->stats.t_total = 0; |
| 2946 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2947 | ctx->strm = s; |
| 2948 | ctx->state = SPOE_CTX_ST_READY; |
| 2949 | filter->ctx = ctx; |
| 2950 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2951 | return ctx; |
| 2952 | } |
| 2953 | |
| 2954 | static void |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2955 | spoe_destroy_context(struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2956 | { |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2957 | struct spoe_config *conf = FLT_CONF(filter); |
| 2958 | struct spoe_context *ctx = filter->ctx; |
| 2959 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2960 | if (!ctx) |
| 2961 | return; |
| 2962 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2963 | spoe_stop_processing(conf->agent, ctx); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2964 | pool_free(pool_head_spoe_ctx, ctx); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2965 | filter->ctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2966 | } |
| 2967 | |
| 2968 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2969 | spoe_reset_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2970 | { |
| 2971 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 2972 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2973 | |
| 2974 | tv_zero(&ctx->stats.tv_start); |
| 2975 | tv_zero(&ctx->stats.tv_request); |
| 2976 | tv_zero(&ctx->stats.tv_queue); |
| 2977 | tv_zero(&ctx->stats.tv_wait); |
| 2978 | tv_zero(&ctx->stats.tv_response); |
| 2979 | ctx->stats.t_request = -1; |
| 2980 | ctx->stats.t_queue = -1; |
| 2981 | ctx->stats.t_waiting = -1; |
| 2982 | ctx->stats.t_response = -1; |
| 2983 | ctx->stats.t_process = -1; |
| 2984 | ctx->stats.t_total = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | |
| 2988 | /*************************************************************************** |
| 2989 | * Hooks that manage the filter lifecycle (init/check/deinit) |
| 2990 | **************************************************************************/ |
| 2991 | /* Signal handler: Do a soft stop, wakeup SPOE applet */ |
| 2992 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2993 | spoe_sig_stop(struct sig_handler *sh) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2994 | { |
| 2995 | struct proxy *p; |
| 2996 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 2997 | p = proxies_list; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2998 | while (p) { |
| 2999 | struct flt_conf *fconf; |
| 3000 | |
| 3001 | list_for_each_entry(fconf, &p->filter_configs, list) { |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 3002 | struct spoe_config *conf; |
| 3003 | struct spoe_agent *agent; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 3004 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3005 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3006 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 3007 | if (fconf->id != spoe_filter_id) |
| 3008 | continue; |
| 3009 | |
| 3010 | conf = fconf->conf; |
| 3011 | agent = conf->agent; |
| 3012 | |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3013 | for (i = 0; i < global.nbthread; ++i) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3014 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3015 | list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list) |
| 3016 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3017 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3018 | } |
| 3019 | } |
| 3020 | p = p->next; |
| 3021 | } |
| 3022 | } |
| 3023 | |
| 3024 | |
| 3025 | /* Initialize the SPOE filter. Returns -1 on error, else 0. */ |
| 3026 | static int |
| 3027 | spoe_init(struct proxy *px, struct flt_conf *fconf) |
| 3028 | { |
| 3029 | struct spoe_config *conf = fconf->conf; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3030 | |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3031 | /* conf->agent_fe was already initialized during the config |
| 3032 | * parsing. Finish initialization. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3033 | conf->agent_fe.last_change = now.tv_sec; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3034 | conf->agent_fe.cap = PR_CAP_FE; |
| 3035 | conf->agent_fe.mode = PR_MODE_TCP; |
| 3036 | conf->agent_fe.maxconn = 0; |
| 3037 | conf->agent_fe.options2 |= PR_O2_INDEPSTR; |
| 3038 | conf->agent_fe.conn_retries = CONN_RETRIES; |
| 3039 | conf->agent_fe.accept = frontend_accept; |
| 3040 | conf->agent_fe.srv = NULL; |
| 3041 | conf->agent_fe.timeout.client = TICK_ETERNITY; |
| 3042 | conf->agent_fe.default_target = &spoe_applet.obj_type; |
| 3043 | conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES; |
| 3044 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3045 | if (!sighandler_registered) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3046 | signal_register_fct(0, spoe_sig_stop, 0); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3047 | sighandler_registered = 1; |
| 3048 | } |
| 3049 | |
Christopher Faulet | 0029235 | 2019-01-09 15:05:10 +0100 | [diff] [blame] | 3050 | fconf->flags |= FLT_CFG_FL_HTX; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3051 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3052 | } |
| 3053 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3054 | /* Free resources allocated by the SPOE filter. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3055 | static void |
| 3056 | spoe_deinit(struct proxy *px, struct flt_conf *fconf) |
| 3057 | { |
| 3058 | struct spoe_config *conf = fconf->conf; |
| 3059 | |
| 3060 | if (conf) { |
| 3061 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3062 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3063 | spoe_release_agent(agent); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3064 | free(conf->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3065 | free(conf); |
| 3066 | } |
| 3067 | fconf->conf = NULL; |
| 3068 | } |
| 3069 | |
| 3070 | /* Check configuration of a SPOE filter for a specified proxy. |
| 3071 | * Return 1 on error, else 0. */ |
| 3072 | static int |
| 3073 | spoe_check(struct proxy *px, struct flt_conf *fconf) |
| 3074 | { |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3075 | struct flt_conf *f; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3076 | struct spoe_config *conf = fconf->conf; |
| 3077 | struct proxy *target; |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3078 | struct logsrv *logsrv; |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3079 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3080 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3081 | /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names |
| 3082 | * are uniq */ |
| 3083 | list_for_each_entry(f, &px->filter_configs, list) { |
| 3084 | struct spoe_config *c = f->conf; |
| 3085 | |
| 3086 | /* This is not an SPOE filter */ |
| 3087 | if (f->id != spoe_filter_id) |
| 3088 | continue; |
| 3089 | /* This is the current SPOE filter */ |
| 3090 | if (f == fconf) |
| 3091 | continue; |
| 3092 | |
| 3093 | /* Check engine Id. It should be uniq */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3094 | if (strcmp(conf->id, c->id) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3095 | ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n", |
| 3096 | px->id, conf->id); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3097 | return 1; |
| 3098 | } |
| 3099 | } |
| 3100 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3101 | target = proxy_be_by_name(conf->agent->b.name); |
| 3102 | if (target == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3103 | ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'" |
| 3104 | " declared at %s:%d.\n", |
| 3105 | px->id, conf->agent->b.name, conf->agent->id, |
| 3106 | conf->agent->conf.file, conf->agent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3107 | return 1; |
| 3108 | } |
| 3109 | if (target->mode != PR_MODE_TCP) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3110 | ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared" |
| 3111 | " at %s:%d does not support HTTP mode.\n", |
| 3112 | px->id, target->id, conf->agent->id, |
| 3113 | conf->agent->conf.file, conf->agent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3114 | return 1; |
| 3115 | } |
| 3116 | |
Christopher Faulet | fe26155 | 2019-03-18 13:57:42 +0100 | [diff] [blame] | 3117 | if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) { |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3118 | ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n", |
| 3119 | px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line); |
| 3120 | return 1; |
| 3121 | } |
| 3122 | for (i = 0; i < global.nbthread; ++i) { |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3123 | conf->agent->rt[i].engine_id = NULL; |
Christopher Faulet | fe26155 | 2019-03-18 13:57:42 +0100 | [diff] [blame] | 3124 | conf->agent->rt[i].frame_size = conf->agent->max_frame_size; |
| 3125 | conf->agent->rt[i].processing = 0; |
| 3126 | LIST_INIT(&conf->agent->rt[i].applets); |
| 3127 | LIST_INIT(&conf->agent->rt[i].sending_queue); |
| 3128 | LIST_INIT(&conf->agent->rt[i].waiting_queue); |
| 3129 | HA_SPIN_INIT(&conf->agent->rt[i].lock); |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3130 | } |
| 3131 | |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3132 | list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) { |
| 3133 | if (logsrv->type == LOG_TARGET_BUFFER) { |
| 3134 | struct sink *sink = sink_find(logsrv->ring_name); |
| 3135 | |
| 3136 | if (!sink || sink->type != SINK_TYPE_BUFFER) { |
| 3137 | ha_alert("Proxy %s : log server used by SPOE agent '%s' declared" |
Ilya Shipitsin | d7a988c | 2021-03-04 23:26:15 +0500 | [diff] [blame] | 3138 | " at %s:%d uses unknown ring named '%s'.\n", |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3139 | px->id, conf->agent->id, conf->agent->conf.file, |
| 3140 | conf->agent->conf.line, logsrv->ring_name); |
| 3141 | return 1; |
| 3142 | } |
| 3143 | logsrv->sink = sink; |
| 3144 | } |
| 3145 | } |
| 3146 | |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3147 | ha_free(&conf->agent->b.name); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3148 | conf->agent->b.be = target; |
| 3149 | return 0; |
| 3150 | } |
| 3151 | |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3152 | /* Initializes the SPOE filter for a proxy for a specific thread. |
| 3153 | * Returns a negative value if an error occurs. */ |
| 3154 | static int |
| 3155 | spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf) |
| 3156 | { |
| 3157 | struct spoe_config *conf = fconf->conf; |
| 3158 | struct spoe_agent *agent = conf->agent; |
| 3159 | |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3160 | agent->rt[tid].engine_id = generate_pseudo_uuid(); |
| 3161 | if (agent->rt[tid].engine_id == NULL) |
| 3162 | return -1; |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3163 | return 0; |
| 3164 | } |
| 3165 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3166 | /************************************************************************** |
| 3167 | * Hooks attached to a stream |
| 3168 | *************************************************************************/ |
| 3169 | /* Called when a filter instance is created and attach to a stream. It creates |
| 3170 | * the context that will be used to process this stream. */ |
| 3171 | static int |
| 3172 | spoe_start(struct stream *s, struct filter *filter) |
| 3173 | { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3174 | struct spoe_config *conf = FLT_CONF(filter); |
| 3175 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3176 | struct spoe_context *ctx; |
| 3177 | |
| 3178 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3179 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3180 | __FUNCTION__, s); |
| 3181 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 3182 | if ((ctx = spoe_create_context(s, filter)) == NULL) { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3183 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 3184 | " - failed to create SPOE context\n", |
| 3185 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | ccbc3fd | 2017-09-15 11:51:18 +0200 | [diff] [blame] | 3186 | __FUNCTION__, s); |
Christopher Faulet | 3b8e349 | 2018-03-26 17:20:58 +0200 | [diff] [blame] | 3187 | send_log(&conf->agent_fe, LOG_EMERG, |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3188 | "SPOE: [%s] failed to create SPOE context\n", |
| 3189 | agent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3190 | return 0; |
| 3191 | } |
| 3192 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3193 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3194 | filter->pre_analyzers |= AN_REQ_INSPECT_FE; |
| 3195 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3196 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3197 | filter->pre_analyzers |= AN_REQ_INSPECT_BE; |
| 3198 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3199 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3200 | filter->pre_analyzers |= AN_RES_INSPECT; |
| 3201 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3202 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3203 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE; |
| 3204 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3205 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3206 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE; |
| 3207 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3208 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3209 | filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE; |
| 3210 | |
| 3211 | return 1; |
| 3212 | } |
| 3213 | |
| 3214 | /* Called when a filter instance is detached from a stream. It release the |
| 3215 | * attached SPOE context. */ |
| 3216 | static void |
| 3217 | spoe_stop(struct stream *s, struct filter *filter) |
| 3218 | { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3219 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
| 3220 | (int)now.tv_sec, (int)now.tv_usec, |
| 3221 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3222 | __FUNCTION__, s); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 3223 | spoe_destroy_context(filter); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3224 | } |
| 3225 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3226 | |
| 3227 | /* |
| 3228 | * Called when the stream is woken up because of expired timer. |
| 3229 | */ |
| 3230 | static void |
| 3231 | spoe_check_timeouts(struct stream *s, struct filter *filter) |
| 3232 | { |
| 3233 | struct spoe_context *ctx = filter->ctx; |
| 3234 | |
Christopher Faulet | ac58060 | 2018-03-20 16:09:20 +0100 | [diff] [blame] | 3235 | if (tick_is_expired(ctx->process_exp, now_ms)) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3236 | s->pending_events |= TASK_WOKEN_MSG; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3237 | } |
| 3238 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3239 | /* Called when we are ready to filter data on a channel */ |
| 3240 | static int |
| 3241 | spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 3242 | { |
| 3243 | struct spoe_context *ctx = filter->ctx; |
| 3244 | int ret = 1; |
| 3245 | |
| 3246 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3247 | " - ctx-flags=0x%08x\n", |
| 3248 | (int)now.tv_sec, (int)now.tv_usec, |
| 3249 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3250 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 3251 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3252 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 3253 | goto out; |
| 3254 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3255 | if (!(chn->flags & CF_ISRESP)) { |
| 3256 | if (filter->pre_analyzers & AN_REQ_INSPECT_FE) |
| 3257 | chn->analysers |= AN_REQ_INSPECT_FE; |
| 3258 | if (filter->pre_analyzers & AN_REQ_INSPECT_BE) |
| 3259 | chn->analysers |= AN_REQ_INSPECT_BE; |
| 3260 | |
| 3261 | if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED) |
| 3262 | goto out; |
| 3263 | |
| 3264 | ctx->stream_id = s->uniq_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3265 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3266 | if (!ret) |
| 3267 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3268 | ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED; |
| 3269 | } |
| 3270 | else { |
Miroslav Zagorac | 8840326 | 2020-06-19 22:17:17 +0200 | [diff] [blame] | 3271 | if (filter->pre_analyzers & AN_RES_INSPECT) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3272 | chn->analysers |= AN_RES_INSPECT; |
| 3273 | |
| 3274 | if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED) |
| 3275 | goto out; |
| 3276 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3277 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3278 | if (!ret) { |
| 3279 | channel_dont_read(chn); |
| 3280 | channel_dont_close(chn); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3281 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3282 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3283 | ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | out: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3287 | return ret; |
| 3288 | } |
| 3289 | |
| 3290 | /* Called before a processing happens on a given channel */ |
| 3291 | static int |
| 3292 | spoe_chn_pre_analyze(struct stream *s, struct filter *filter, |
| 3293 | struct channel *chn, unsigned an_bit) |
| 3294 | { |
| 3295 | struct spoe_context *ctx = filter->ctx; |
| 3296 | int ret = 1; |
| 3297 | |
| 3298 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3299 | " - ctx-flags=0x%08x - ana=0x%08x\n", |
| 3300 | (int)now.tv_sec, (int)now.tv_usec, |
| 3301 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3302 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 3303 | ctx->flags, an_bit); |
| 3304 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3305 | if (ctx->state == SPOE_CTX_ST_NONE) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3306 | goto out; |
| 3307 | |
| 3308 | switch (an_bit) { |
| 3309 | case AN_REQ_INSPECT_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3310 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3311 | break; |
| 3312 | case AN_REQ_INSPECT_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3313 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3314 | break; |
| 3315 | case AN_RES_INSPECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3316 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3317 | break; |
| 3318 | case AN_REQ_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3319 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3320 | break; |
| 3321 | case AN_REQ_HTTP_PROCESS_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3322 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3323 | break; |
| 3324 | case AN_RES_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3325 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3326 | break; |
| 3327 | } |
| 3328 | |
| 3329 | out: |
Christopher Faulet | 9cdca97 | 2018-02-01 08:45:45 +0100 | [diff] [blame] | 3330 | if (!ret && (chn->flags & CF_ISRESP)) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3331 | channel_dont_read(chn); |
| 3332 | channel_dont_close(chn); |
| 3333 | } |
| 3334 | return ret; |
| 3335 | } |
| 3336 | |
| 3337 | /* Called when the filtering on the channel ends. */ |
| 3338 | static int |
| 3339 | spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 3340 | { |
| 3341 | struct spoe_context *ctx = filter->ctx; |
| 3342 | |
| 3343 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3344 | " - ctx-flags=0x%08x\n", |
| 3345 | (int)now.tv_sec, (int)now.tv_usec, |
| 3346 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3347 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 3348 | |
| 3349 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3350 | spoe_reset_context(ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | return 1; |
| 3354 | } |
| 3355 | |
| 3356 | /******************************************************************** |
| 3357 | * Functions that manage the filter initialization |
| 3358 | ********************************************************************/ |
| 3359 | struct flt_ops spoe_ops = { |
| 3360 | /* Manage SPOE filter, called for each filter declaration */ |
| 3361 | .init = spoe_init, |
| 3362 | .deinit = spoe_deinit, |
| 3363 | .check = spoe_check, |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3364 | .init_per_thread = spoe_init_per_thread, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3365 | |
| 3366 | /* Handle start/stop of SPOE */ |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3367 | .attach = spoe_start, |
| 3368 | .detach = spoe_stop, |
| 3369 | .check_timeouts = spoe_check_timeouts, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3370 | |
| 3371 | /* Handle channels activity */ |
| 3372 | .channel_start_analyze = spoe_start_analyze, |
| 3373 | .channel_pre_analyze = spoe_chn_pre_analyze, |
| 3374 | .channel_end_analyze = spoe_end_analyze, |
| 3375 | }; |
| 3376 | |
| 3377 | |
| 3378 | static int |
| 3379 | cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm) |
| 3380 | { |
| 3381 | const char *err; |
| 3382 | int i, err_code = 0; |
| 3383 | |
| 3384 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3385 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3386 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3387 | goto out; |
| 3388 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3389 | if (strcmp(args[0], "spoe-agent") == 0) { /* new spoe-agent section */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3390 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3391 | ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n", |
| 3392 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3393 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3394 | goto out; |
| 3395 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3396 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3397 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3398 | goto out; |
| 3399 | } |
| 3400 | |
| 3401 | err = invalid_char(args[1]); |
| 3402 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3403 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3404 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3405 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3406 | goto out; |
| 3407 | } |
| 3408 | |
| 3409 | if (curagent != NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3410 | ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n", |
| 3411 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3412 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3413 | goto out; |
| 3414 | } |
| 3415 | if ((curagent = calloc(1, sizeof(*curagent))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3416 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3417 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3418 | goto out; |
| 3419 | } |
| 3420 | |
| 3421 | curagent->id = strdup(args[1]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3422 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3423 | curagent->conf.file = strdup(file); |
| 3424 | curagent->conf.line = linenum; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3425 | |
| 3426 | curagent->timeout.hello = TICK_ETERNITY; |
| 3427 | curagent->timeout.idle = TICK_ETERNITY; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3428 | curagent->timeout.processing = TICK_ETERNITY; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3429 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3430 | curagent->var_pfx = NULL; |
| 3431 | curagent->var_on_error = NULL; |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3432 | curagent->var_t_process = NULL; |
| 3433 | curagent->var_t_total = NULL; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3434 | curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3435 | curagent->cps_max = 0; |
| 3436 | curagent->eps_max = 0; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 3437 | curagent->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 3438 | curagent->max_fpa = 20; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3439 | |
| 3440 | for (i = 0; i < SPOE_EV_EVENTS; ++i) |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3441 | LIST_INIT(&curagent->events[i]); |
| 3442 | LIST_INIT(&curagent->groups); |
| 3443 | LIST_INIT(&curagent->messages); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3444 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3445 | else if (strcmp(args[0], "use-backend") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3446 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3447 | ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n", |
| 3448 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3449 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3450 | goto out; |
| 3451 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3452 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3453 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3454 | free(curagent->b.name); |
| 3455 | curagent->b.name = strdup(args[1]); |
| 3456 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3457 | else if (strcmp(args[0], "messages") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3458 | int cur_arg = 1; |
| 3459 | while (*args[cur_arg]) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3460 | struct spoe_placeholder *ph = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3461 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3462 | list_for_each_entry(ph, &curmphs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3463 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3464 | ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3465 | file, linenum, args[cur_arg]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3466 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3467 | goto out; |
| 3468 | } |
| 3469 | } |
| 3470 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3471 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3472 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3473 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3474 | goto out; |
| 3475 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3476 | ph->id = strdup(args[cur_arg]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3477 | LIST_APPEND(&curmphs, &ph->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3478 | cur_arg++; |
| 3479 | } |
| 3480 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3481 | else if (strcmp(args[0], "groups") == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3482 | int cur_arg = 1; |
| 3483 | while (*args[cur_arg]) { |
| 3484 | struct spoe_placeholder *ph = NULL; |
| 3485 | |
| 3486 | list_for_each_entry(ph, &curgphs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3487 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3488 | ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n", |
| 3489 | file, linenum, args[cur_arg]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3490 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3491 | goto out; |
| 3492 | } |
| 3493 | } |
| 3494 | |
| 3495 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3496 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3497 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3498 | goto out; |
| 3499 | } |
| 3500 | ph->id = strdup(args[cur_arg]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3501 | LIST_APPEND(&curgphs, &ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3502 | cur_arg++; |
| 3503 | } |
| 3504 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3505 | else if (strcmp(args[0], "timeout") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3506 | unsigned int *tv = NULL; |
| 3507 | const char *res; |
| 3508 | unsigned timeout; |
| 3509 | |
| 3510 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3511 | ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n", |
| 3512 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3513 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3514 | goto out; |
| 3515 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3516 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3517 | goto out; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3518 | if (strcmp(args[1], "hello") == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3519 | tv = &curagent->timeout.hello; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3520 | else if (strcmp(args[1], "idle") == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3521 | tv = &curagent->timeout.idle; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3522 | else if (strcmp(args[1], "processing") == 0) |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3523 | tv = &curagent->timeout.processing; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3524 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3525 | ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n", |
| 3526 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3527 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3528 | goto out; |
| 3529 | } |
| 3530 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3531 | ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n", |
| 3532 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3533 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3534 | goto out; |
| 3535 | } |
| 3536 | res = parse_time_err(args[2], &timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 3537 | if (res == PARSE_TIME_OVER) { |
| 3538 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n", |
| 3539 | file, linenum, args[2], args[0], args[1]); |
| 3540 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3541 | goto out; |
| 3542 | } |
| 3543 | else if (res == PARSE_TIME_UNDER) { |
| 3544 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n", |
| 3545 | file, linenum, args[2], args[0], args[1]); |
| 3546 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3547 | goto out; |
| 3548 | } |
| 3549 | else if (res) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3550 | ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n", |
| 3551 | file, linenum, *res, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3552 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3553 | goto out; |
| 3554 | } |
| 3555 | *tv = MS_TO_TICKS(timeout); |
| 3556 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3557 | else if (strcmp(args[0], "option") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3558 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3559 | ha_alert("parsing [%s:%d]: '%s' expects an option name.\n", |
| 3560 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3561 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3562 | goto out; |
| 3563 | } |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3564 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3565 | if (strcmp(args[1], "pipelining") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3566 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3567 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3568 | if (kwm == 1) |
| 3569 | curagent->flags &= ~SPOE_FL_PIPELINING; |
| 3570 | else |
| 3571 | curagent->flags |= SPOE_FL_PIPELINING; |
| 3572 | goto out; |
| 3573 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3574 | else if (strcmp(args[1], "async") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3575 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3576 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3577 | if (kwm == 1) |
| 3578 | curagent->flags &= ~SPOE_FL_ASYNC; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3579 | else |
| 3580 | curagent->flags |= SPOE_FL_ASYNC; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3581 | goto out; |
| 3582 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3583 | else if (strcmp(args[1], "send-frag-payload") == 0) { |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 3584 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3585 | goto out; |
| 3586 | if (kwm == 1) |
| 3587 | curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION; |
| 3588 | else |
| 3589 | curagent->flags |= SPOE_FL_SND_FRAGMENTATION; |
| 3590 | goto out; |
| 3591 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3592 | else if (strcmp(args[1], "dontlog-normal") == 0) { |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 3593 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3594 | goto out; |
| 3595 | if (kwm == 1) |
| 3596 | curpxopts2 &= ~PR_O2_NOLOGNORM; |
| 3597 | else |
| 3598 | curpxopts2 |= PR_O2_NOLOGNORM; |
Christopher Faulet | 799f518 | 2018-04-26 11:36:34 +0200 | [diff] [blame] | 3599 | goto out; |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 3600 | } |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3601 | |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3602 | /* Following options does not support negation */ |
| 3603 | if (kwm == 1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3604 | ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n", |
| 3605 | file, linenum, args[1]); |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3606 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3607 | goto out; |
| 3608 | } |
| 3609 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3610 | if (strcmp(args[1], "var-prefix") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3611 | char *tmp; |
| 3612 | |
| 3613 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3614 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3615 | file, linenum, args[0], |
| 3616 | args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3617 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3618 | goto out; |
| 3619 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3620 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3621 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3622 | tmp = args[2]; |
| 3623 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3624 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3625 | 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] | 3626 | file, linenum, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3627 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3628 | goto out; |
| 3629 | } |
| 3630 | tmp++; |
| 3631 | } |
| 3632 | curagent->var_pfx = strdup(args[2]); |
| 3633 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3634 | else if (strcmp(args[1], "force-set-var") == 0) { |
Etienne Carriere | aec8989 | 2017-12-14 09:36:40 +0000 | [diff] [blame] | 3635 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3636 | goto out; |
| 3637 | curagent->flags |= SPOE_FL_FORCE_SET_VAR; |
| 3638 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3639 | else if (strcmp(args[1], "continue-on-error") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3640 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3641 | goto out; |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3642 | curagent->flags |= SPOE_FL_CONT_ON_ERR; |
| 3643 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3644 | else if (strcmp(args[1], "set-on-error") == 0) { |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3645 | char *tmp; |
| 3646 | |
| 3647 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3648 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3649 | file, linenum, args[0], |
| 3650 | args[1]); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3651 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3652 | goto out; |
| 3653 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3654 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3655 | goto out; |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3656 | tmp = args[2]; |
| 3657 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3658 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3659 | 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] | 3660 | file, linenum, args[0], args[1]); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3661 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3662 | goto out; |
| 3663 | } |
| 3664 | tmp++; |
| 3665 | } |
| 3666 | curagent->var_on_error = strdup(args[2]); |
| 3667 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3668 | else if (strcmp(args[1], "set-process-time") == 0) { |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3669 | char *tmp; |
| 3670 | |
| 3671 | if (!*args[2]) { |
| 3672 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3673 | file, linenum, args[0], |
| 3674 | args[1]); |
| 3675 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3676 | goto out; |
| 3677 | } |
| 3678 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3679 | goto out; |
| 3680 | tmp = args[2]; |
| 3681 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3682 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3683 | 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] | 3684 | file, linenum, args[0], args[1]); |
| 3685 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3686 | goto out; |
| 3687 | } |
| 3688 | tmp++; |
| 3689 | } |
| 3690 | curagent->var_t_process = strdup(args[2]); |
| 3691 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3692 | else if (strcmp(args[1], "set-total-time") == 0) { |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3693 | char *tmp; |
| 3694 | |
| 3695 | if (!*args[2]) { |
| 3696 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3697 | file, linenum, args[0], |
| 3698 | args[1]); |
| 3699 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3700 | goto out; |
| 3701 | } |
| 3702 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3703 | goto out; |
| 3704 | tmp = args[2]; |
| 3705 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3706 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3707 | 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] | 3708 | file, linenum, args[0], args[1]); |
| 3709 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3710 | goto out; |
| 3711 | } |
| 3712 | tmp++; |
| 3713 | } |
| 3714 | curagent->var_t_total = strdup(args[2]); |
| 3715 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3716 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3717 | ha_alert("parsing [%s:%d]: option '%s' is not supported.\n", |
| 3718 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3719 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3720 | goto out; |
| 3721 | } |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3722 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3723 | else if (strcmp(args[0], "maxconnrate") == 0) { |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3724 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3725 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3726 | file, linenum, args[0]); |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3727 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3728 | goto out; |
| 3729 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3730 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3731 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3732 | curagent->cps_max = atol(args[1]); |
| 3733 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3734 | else if (strcmp(args[0], "maxerrrate") == 0) { |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3735 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3736 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3737 | file, linenum, args[0]); |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3738 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3739 | goto out; |
| 3740 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3741 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3742 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3743 | curagent->eps_max = atol(args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3744 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3745 | else if (strcmp(args[0], "max-frame-size") == 0) { |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3746 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3747 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3748 | file, linenum, args[0]); |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3749 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3750 | goto out; |
| 3751 | } |
| 3752 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3753 | goto out; |
| 3754 | curagent->max_frame_size = atol(args[1]); |
| 3755 | if (curagent->max_frame_size < MIN_FRAME_SIZE || |
| 3756 | curagent->max_frame_size > MAX_FRAME_SIZE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3757 | ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n", |
| 3758 | file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE); |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3759 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3760 | goto out; |
| 3761 | } |
| 3762 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3763 | else if (strcmp(args[0], "max-waiting-frames") == 0) { |
Christopher Faulet | e8ade38 | 2018-01-25 15:32:22 +0100 | [diff] [blame] | 3764 | if (!*args[1]) { |
| 3765 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3766 | file, linenum, args[0]); |
| 3767 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3768 | goto out; |
| 3769 | } |
| 3770 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3771 | goto out; |
| 3772 | curagent->max_fpa = atol(args[1]); |
| 3773 | if (curagent->max_fpa < 1) { |
| 3774 | ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n", |
| 3775 | file, linenum, args[0]); |
| 3776 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3777 | goto out; |
| 3778 | } |
| 3779 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3780 | else if (strcmp(args[0], "register-var-names") == 0) { |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3781 | int cur_arg; |
| 3782 | |
| 3783 | if (!*args[1]) { |
| 3784 | ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n", |
| 3785 | file, linenum, args[0]); |
| 3786 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3787 | goto out; |
| 3788 | } |
| 3789 | cur_arg = 1; |
| 3790 | while (*args[cur_arg]) { |
| 3791 | struct spoe_var_placeholder *vph; |
| 3792 | |
| 3793 | if ((vph = calloc(1, sizeof(*vph))) == NULL) { |
| 3794 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3795 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3796 | goto out; |
| 3797 | } |
| 3798 | if ((vph->name = strdup(args[cur_arg])) == NULL) { |
Tim Duesterhus | b298613 | 2019-06-23 22:10:13 +0200 | [diff] [blame] | 3799 | free(vph); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3800 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3801 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3802 | goto out; |
| 3803 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3804 | LIST_APPEND(&curvars, &vph->list); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3805 | cur_arg++; |
| 3806 | } |
| 3807 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3808 | else if (strcmp(args[0], "log") == 0) { |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3809 | char *errmsg = NULL; |
| 3810 | |
Emeric Brun | 9533a70 | 2021-04-02 10:13:43 +0200 | [diff] [blame] | 3811 | if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), file, linenum, &errmsg)) { |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3812 | ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); |
| 3813 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3814 | goto out; |
| 3815 | } |
| 3816 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3817 | else if (*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3818 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n", |
| 3819 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3820 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3821 | goto out; |
| 3822 | } |
| 3823 | out: |
| 3824 | return err_code; |
| 3825 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3826 | static int |
| 3827 | cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm) |
| 3828 | { |
| 3829 | struct spoe_group *grp; |
| 3830 | const char *err; |
| 3831 | int err_code = 0; |
| 3832 | |
| 3833 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3834 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3835 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3836 | goto out; |
| 3837 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3838 | if (strcmp(args[0], "spoe-group") == 0) { /* new spoe-group section */ |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3839 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3840 | ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n", |
| 3841 | file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3842 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3843 | goto out; |
| 3844 | } |
| 3845 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3846 | err_code |= ERR_ABORT; |
| 3847 | goto out; |
| 3848 | } |
| 3849 | |
| 3850 | err = invalid_char(args[1]); |
| 3851 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3852 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3853 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3854 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3855 | goto out; |
| 3856 | } |
| 3857 | |
| 3858 | list_for_each_entry(grp, &curgrps, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3859 | if (strcmp(grp->id, args[1]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3860 | ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same" |
| 3861 | " name as another one declared at %s:%d.\n", |
| 3862 | file, linenum, args[1], grp->conf.file, grp->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3863 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3864 | goto out; |
| 3865 | } |
| 3866 | } |
| 3867 | |
| 3868 | if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3869 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3870 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3871 | goto out; |
| 3872 | } |
| 3873 | |
| 3874 | curgrp->id = strdup(args[1]); |
| 3875 | curgrp->conf.file = strdup(file); |
| 3876 | curgrp->conf.line = linenum; |
| 3877 | LIST_INIT(&curgrp->phs); |
| 3878 | LIST_INIT(&curgrp->messages); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3879 | LIST_APPEND(&curgrps, &curgrp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3880 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3881 | else if (strcmp(args[0], "messages") == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3882 | int cur_arg = 1; |
| 3883 | while (*args[cur_arg]) { |
| 3884 | struct spoe_placeholder *ph = NULL; |
| 3885 | |
| 3886 | list_for_each_entry(ph, &curgrp->phs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3887 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3888 | ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3889 | file, linenum, args[cur_arg]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3890 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3891 | goto out; |
| 3892 | } |
| 3893 | } |
| 3894 | |
| 3895 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3896 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3897 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3898 | goto out; |
| 3899 | } |
| 3900 | ph->id = strdup(args[cur_arg]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3901 | LIST_APPEND(&curgrp->phs, &ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3902 | cur_arg++; |
| 3903 | } |
| 3904 | } |
| 3905 | else if (*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3906 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n", |
| 3907 | file, linenum, args[0]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3908 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3909 | goto out; |
| 3910 | } |
| 3911 | out: |
| 3912 | return err_code; |
| 3913 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3914 | |
| 3915 | static int |
| 3916 | cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm) |
| 3917 | { |
| 3918 | struct spoe_message *msg; |
| 3919 | struct spoe_arg *arg; |
| 3920 | const char *err; |
| 3921 | char *errmsg = NULL; |
| 3922 | int err_code = 0; |
| 3923 | |
| 3924 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3925 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3926 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3927 | goto out; |
| 3928 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3929 | if (strcmp(args[0], "spoe-message") == 0) { /* new spoe-message section */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3930 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3931 | ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n", |
| 3932 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3933 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3934 | goto out; |
| 3935 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3936 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3937 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3938 | goto out; |
| 3939 | } |
| 3940 | |
| 3941 | err = invalid_char(args[1]); |
| 3942 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3943 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3944 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3945 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3946 | goto out; |
| 3947 | } |
| 3948 | |
| 3949 | list_for_each_entry(msg, &curmsgs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3950 | if (strcmp(msg->id, args[1]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3951 | ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same" |
| 3952 | " name as another one declared at %s:%d.\n", |
| 3953 | file, linenum, args[1], msg->conf.file, msg->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3954 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3955 | goto out; |
| 3956 | } |
| 3957 | } |
| 3958 | |
| 3959 | if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3960 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3961 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3962 | goto out; |
| 3963 | } |
| 3964 | |
| 3965 | curmsg->id = strdup(args[1]); |
| 3966 | curmsg->id_len = strlen(curmsg->id); |
| 3967 | curmsg->event = SPOE_EV_NONE; |
| 3968 | curmsg->conf.file = strdup(file); |
| 3969 | curmsg->conf.line = linenum; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 3970 | curmsg->nargs = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3971 | LIST_INIT(&curmsg->args); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3972 | LIST_INIT(&curmsg->acls); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3973 | LIST_INIT(&curmsg->by_evt); |
| 3974 | LIST_INIT(&curmsg->by_grp); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3975 | LIST_APPEND(&curmsgs, &curmsg->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3976 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3977 | else if (strcmp(args[0], "args") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3978 | int cur_arg = 1; |
| 3979 | |
| 3980 | curproxy->conf.args.ctx = ARGC_SPOE; |
| 3981 | curproxy->conf.args.file = file; |
| 3982 | curproxy->conf.args.line = linenum; |
| 3983 | while (*args[cur_arg]) { |
| 3984 | char *delim = strchr(args[cur_arg], '='); |
| 3985 | int idx = 0; |
| 3986 | |
| 3987 | if ((arg = calloc(1, sizeof(*arg))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3988 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3989 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3990 | goto out; |
| 3991 | } |
| 3992 | |
| 3993 | if (!delim) { |
| 3994 | arg->name = NULL; |
| 3995 | arg->name_len = 0; |
| 3996 | delim = args[cur_arg]; |
| 3997 | } |
| 3998 | else { |
| 3999 | arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]); |
| 4000 | arg->name_len = delim - args[cur_arg]; |
| 4001 | delim++; |
| 4002 | } |
Christopher Faulet | b0b4238 | 2017-02-23 22:41:09 +0100 | [diff] [blame] | 4003 | arg->expr = sample_parse_expr((char*[]){delim, NULL}, |
| 4004 | &idx, file, linenum, &errmsg, |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 4005 | &curproxy->conf.args, NULL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4006 | if (arg->expr == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4007 | 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] | 4008 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4009 | free(arg->name); |
| 4010 | free(arg); |
| 4011 | goto out; |
| 4012 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 4013 | curmsg->nargs++; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4014 | LIST_APPEND(&curmsg->args, &arg->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4015 | cur_arg++; |
| 4016 | } |
| 4017 | curproxy->conf.args.file = NULL; |
| 4018 | curproxy->conf.args.line = 0; |
| 4019 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4020 | else if (strcmp(args[0], "acl") == 0) { |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4021 | err = invalid_char(args[1]); |
| 4022 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4023 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n", |
| 4024 | file, linenum, *err, args[1]); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4025 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4026 | goto out; |
| 4027 | } |
Tim Duesterhus | 0cf811a | 2020-02-05 21:00:50 +0100 | [diff] [blame] | 4028 | if (strcasecmp(args[1], "or") == 0) { |
Tim Duesterhus | f1bc24c | 2020-02-06 22:04:03 +0100 | [diff] [blame] | 4029 | 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] | 4030 | "logical disjunction within a condition.\n", |
| 4031 | file, linenum, args[1]); |
| 4032 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4033 | goto out; |
| 4034 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4035 | 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] | 4036 | ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n", |
| 4037 | file, linenum, args[1], errmsg); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4038 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4039 | goto out; |
| 4040 | } |
| 4041 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4042 | else if (strcmp(args[0], "event") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4043 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4044 | ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4045 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4046 | goto out; |
| 4047 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4048 | /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */ |
| 4049 | /* goto out; */ |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4050 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4051 | 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] | 4052 | curmsg->event = SPOE_EV_ON_CLIENT_SESS; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4053 | 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] | 4054 | curmsg->event = SPOE_EV_ON_SERVER_SESS; |
| 4055 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4056 | 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] | 4057 | curmsg->event = SPOE_EV_ON_TCP_REQ_FE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4058 | 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] | 4059 | curmsg->event = SPOE_EV_ON_TCP_REQ_BE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4060 | 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] | 4061 | curmsg->event = SPOE_EV_ON_TCP_RSP; |
| 4062 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4063 | 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] | 4064 | curmsg->event = SPOE_EV_ON_HTTP_REQ_FE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4065 | 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] | 4066 | curmsg->event = SPOE_EV_ON_HTTP_REQ_BE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4067 | 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] | 4068 | curmsg->event = SPOE_EV_ON_HTTP_RSP; |
| 4069 | else { |
Joseph Herlant | f1da69d | 2018-11-15 13:49:02 -0800 | [diff] [blame] | 4070 | ha_alert("parsing [%s:%d] : unknown event '%s'.\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4071 | file, linenum, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4072 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4073 | goto out; |
| 4074 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4075 | |
| 4076 | if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) { |
| 4077 | struct acl_cond *cond; |
| 4078 | |
| 4079 | cond = build_acl_cond(file, linenum, &curmsg->acls, |
| 4080 | curproxy, (const char **)args+2, |
| 4081 | &errmsg); |
| 4082 | if (cond == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4083 | ha_alert("parsing [%s:%d] : error detected while " |
| 4084 | "parsing an 'event %s' condition : %s.\n", |
| 4085 | file, linenum, args[1], errmsg); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4086 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4087 | goto out; |
| 4088 | } |
| 4089 | curmsg->cond = cond; |
| 4090 | } |
| 4091 | else if (*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4092 | ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' " |
| 4093 | "or 'unless' followed by a condition but found '%s'.\n", |
| 4094 | file, linenum, args[1], args[2]); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4095 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4096 | goto out; |
| 4097 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4098 | } |
| 4099 | else if (!*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4100 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n", |
| 4101 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4102 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4103 | goto out; |
| 4104 | } |
| 4105 | out: |
| 4106 | free(errmsg); |
| 4107 | return err_code; |
| 4108 | } |
| 4109 | |
| 4110 | /* Return -1 on error, else 0 */ |
| 4111 | static int |
| 4112 | parse_spoe_flt(char **args, int *cur_arg, struct proxy *px, |
| 4113 | struct flt_conf *fconf, char **err, void *private) |
| 4114 | { |
| 4115 | struct list backup_sections; |
| 4116 | struct spoe_config *conf; |
| 4117 | struct spoe_message *msg, *msgback; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4118 | struct spoe_group *grp, *grpback; |
| 4119 | struct spoe_placeholder *ph, *phback; |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4120 | struct spoe_var_placeholder *vph, *vphback; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4121 | struct logsrv *logsrv, *logsrvback; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4122 | char *file = NULL, *engine = NULL; |
| 4123 | int ret, pos = *cur_arg + 1; |
| 4124 | |
Christopher Faulet | 84c844e | 2018-03-23 14:37:14 +0100 | [diff] [blame] | 4125 | LIST_INIT(&curmsgs); |
| 4126 | LIST_INIT(&curgrps); |
| 4127 | LIST_INIT(&curmphs); |
| 4128 | LIST_INIT(&curgphs); |
| 4129 | LIST_INIT(&curvars); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4130 | LIST_INIT(&curlogsrvs); |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 4131 | curpxopts = 0; |
| 4132 | curpxopts2 = 0; |
Christopher Faulet | 84c844e | 2018-03-23 14:37:14 +0100 | [diff] [blame] | 4133 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4134 | conf = calloc(1, sizeof(*conf)); |
| 4135 | if (conf == NULL) { |
| 4136 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 4137 | goto error; |
| 4138 | } |
| 4139 | conf->proxy = px; |
| 4140 | |
| 4141 | while (*args[pos]) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4142 | if (strcmp(args[pos], "config") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4143 | if (!*args[pos+1]) { |
| 4144 | memprintf(err, "'%s' : '%s' option without value", |
| 4145 | args[*cur_arg], args[pos]); |
| 4146 | goto error; |
| 4147 | } |
| 4148 | file = args[pos+1]; |
| 4149 | pos += 2; |
| 4150 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4151 | else if (strcmp(args[pos], "engine") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4152 | if (!*args[pos+1]) { |
| 4153 | memprintf(err, "'%s' : '%s' option without value", |
| 4154 | args[*cur_arg], args[pos]); |
| 4155 | goto error; |
| 4156 | } |
| 4157 | engine = args[pos+1]; |
| 4158 | pos += 2; |
| 4159 | } |
| 4160 | else { |
| 4161 | memprintf(err, "unknown keyword '%s'", args[pos]); |
| 4162 | goto error; |
| 4163 | } |
| 4164 | } |
| 4165 | if (file == NULL) { |
| 4166 | memprintf(err, "'%s' : missing config file", args[*cur_arg]); |
| 4167 | goto error; |
| 4168 | } |
| 4169 | |
| 4170 | /* backup sections and register SPOE sections */ |
| 4171 | LIST_INIT(&backup_sections); |
| 4172 | cfg_backup_sections(&backup_sections); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4173 | cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL); |
| 4174 | cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL); |
William Lallemand | d2ff56d | 2017-10-16 11:06:50 +0200 | [diff] [blame] | 4175 | cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4176 | |
| 4177 | /* Parse SPOE filter configuration file */ |
| 4178 | curengine = engine; |
| 4179 | curproxy = px; |
| 4180 | curagent = NULL; |
| 4181 | curmsg = NULL; |
| 4182 | ret = readcfgfile(file); |
| 4183 | curproxy = NULL; |
| 4184 | |
| 4185 | /* unregister SPOE sections and restore previous sections */ |
| 4186 | cfg_unregister_sections(); |
| 4187 | cfg_restore_sections(&backup_sections); |
| 4188 | |
| 4189 | if (ret == -1) { |
| 4190 | memprintf(err, "Could not open configuration file %s : %s", |
| 4191 | file, strerror(errno)); |
| 4192 | goto error; |
| 4193 | } |
| 4194 | if (ret & (ERR_ABORT|ERR_FATAL)) { |
| 4195 | memprintf(err, "Error(s) found in configuration file %s", file); |
| 4196 | goto error; |
| 4197 | } |
| 4198 | |
| 4199 | /* Check SPOE agent */ |
| 4200 | if (curagent == NULL) { |
| 4201 | memprintf(err, "No SPOE agent found in file %s", file); |
| 4202 | goto error; |
| 4203 | } |
| 4204 | if (curagent->b.name == NULL) { |
| 4205 | memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d", |
| 4206 | curagent->id, curagent->conf.file, curagent->conf.line); |
| 4207 | goto error; |
| 4208 | } |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 4209 | if (curagent->timeout.hello == TICK_ETERNITY || |
| 4210 | curagent->timeout.idle == TICK_ETERNITY || |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 4211 | curagent->timeout.processing == TICK_ETERNITY) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4212 | ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n" |
| 4213 | " | While not properly invalid, you will certainly encounter various problems\n" |
| 4214 | " | with such a configuration. To fix this, please ensure that all following\n" |
| 4215 | " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n", |
| 4216 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4217 | } |
| 4218 | if (curagent->var_pfx == NULL) { |
| 4219 | char *tmp = curagent->id; |
| 4220 | |
| 4221 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4222 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4223 | memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. " |
| 4224 | "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n", |
| 4225 | curagent->id, curagent->id, curagent->conf.file, curagent->conf.line); |
| 4226 | goto error; |
| 4227 | } |
| 4228 | tmp++; |
| 4229 | } |
| 4230 | curagent->var_pfx = strdup(curagent->id); |
| 4231 | } |
| 4232 | |
Christopher Faulet | b7426d1 | 2018-03-21 14:12:17 +0100 | [diff] [blame] | 4233 | if (curagent->var_on_error) { |
| 4234 | struct arg arg; |
| 4235 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4236 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | b7426d1 | 2018-03-21 14:12:17 +0100 | [diff] [blame] | 4237 | curagent->var_pfx, curagent->var_on_error); |
| 4238 | |
| 4239 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4240 | arg.data.str.area = trash.area; |
| 4241 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4242 | 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] | 4243 | if (!vars_check_arg(&arg, err)) { |
| 4244 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4245 | curagent->id, curagent->var_pfx, curagent->var_on_error, *err); |
| 4246 | goto error; |
| 4247 | } |
| 4248 | } |
| 4249 | |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4250 | if (curagent->var_t_process) { |
| 4251 | struct arg arg; |
| 4252 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4253 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4254 | curagent->var_pfx, curagent->var_t_process); |
| 4255 | |
| 4256 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4257 | arg.data.str.area = trash.area; |
| 4258 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4259 | 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] | 4260 | if (!vars_check_arg(&arg, err)) { |
| 4261 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4262 | curagent->id, curagent->var_pfx, curagent->var_t_process, *err); |
| 4263 | goto error; |
| 4264 | } |
| 4265 | } |
| 4266 | |
| 4267 | if (curagent->var_t_total) { |
| 4268 | struct arg arg; |
| 4269 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4270 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4271 | curagent->var_pfx, curagent->var_t_total); |
| 4272 | |
| 4273 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4274 | arg.data.str.area = trash.area; |
| 4275 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4276 | 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] | 4277 | if (!vars_check_arg(&arg, err)) { |
| 4278 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4279 | curagent->id, curagent->var_pfx, curagent->var_t_process, *err); |
| 4280 | goto error; |
| 4281 | } |
| 4282 | } |
| 4283 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4284 | if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4285 | ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n", |
| 4286 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4287 | goto finish; |
| 4288 | } |
| 4289 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4290 | /* Replace placeholders by the corresponding messages for the SPOE |
| 4291 | * agent */ |
| 4292 | list_for_each_entry(ph, &curmphs, list) { |
| 4293 | list_for_each_entry(msg, &curmsgs, list) { |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4294 | struct spoe_arg *arg; |
| 4295 | unsigned int where; |
| 4296 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4297 | if (strcmp(msg->id, ph->id) == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4298 | if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) { |
| 4299 | if (msg->event == SPOE_EV_ON_TCP_REQ_BE) |
| 4300 | msg->event = SPOE_EV_ON_TCP_REQ_FE; |
| 4301 | if (msg->event == SPOE_EV_ON_HTTP_REQ_BE) |
| 4302 | msg->event = SPOE_EV_ON_HTTP_REQ_FE; |
| 4303 | } |
| 4304 | if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS || |
| 4305 | msg->event == SPOE_EV_ON_TCP_REQ_FE || |
| 4306 | msg->event == SPOE_EV_ON_HTTP_REQ_FE)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4307 | ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n", |
| 4308 | px->id, msg->conf.file, msg->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4309 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4310 | } |
| 4311 | if (msg->event == SPOE_EV_NONE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4312 | ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n", |
| 4313 | px->id, msg->id, msg->conf.file, msg->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4314 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4315 | } |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4316 | |
| 4317 | where = 0; |
| 4318 | switch (msg->event) { |
| 4319 | case SPOE_EV_ON_CLIENT_SESS: |
| 4320 | where |= SMP_VAL_FE_CON_ACC; |
| 4321 | break; |
| 4322 | |
| 4323 | case SPOE_EV_ON_TCP_REQ_FE: |
| 4324 | where |= SMP_VAL_FE_REQ_CNT; |
| 4325 | break; |
| 4326 | |
| 4327 | case SPOE_EV_ON_HTTP_REQ_FE: |
| 4328 | where |= SMP_VAL_FE_HRQ_HDR; |
| 4329 | break; |
| 4330 | |
| 4331 | case SPOE_EV_ON_TCP_REQ_BE: |
| 4332 | if (px->cap & PR_CAP_FE) |
| 4333 | where |= SMP_VAL_FE_REQ_CNT; |
| 4334 | if (px->cap & PR_CAP_BE) |
| 4335 | where |= SMP_VAL_BE_REQ_CNT; |
| 4336 | break; |
| 4337 | |
| 4338 | case SPOE_EV_ON_HTTP_REQ_BE: |
| 4339 | if (px->cap & PR_CAP_FE) |
| 4340 | where |= SMP_VAL_FE_HRQ_HDR; |
| 4341 | if (px->cap & PR_CAP_BE) |
| 4342 | where |= SMP_VAL_BE_HRQ_HDR; |
| 4343 | break; |
| 4344 | |
| 4345 | case SPOE_EV_ON_SERVER_SESS: |
| 4346 | where |= SMP_VAL_BE_SRV_CON; |
| 4347 | break; |
| 4348 | |
| 4349 | case SPOE_EV_ON_TCP_RSP: |
| 4350 | if (px->cap & PR_CAP_FE) |
| 4351 | where |= SMP_VAL_FE_RES_CNT; |
| 4352 | if (px->cap & PR_CAP_BE) |
| 4353 | where |= SMP_VAL_BE_RES_CNT; |
| 4354 | break; |
| 4355 | |
| 4356 | case SPOE_EV_ON_HTTP_RSP: |
| 4357 | if (px->cap & PR_CAP_FE) |
| 4358 | where |= SMP_VAL_FE_HRS_HDR; |
| 4359 | if (px->cap & PR_CAP_BE) |
| 4360 | where |= SMP_VAL_BE_HRS_HDR; |
| 4361 | break; |
| 4362 | |
| 4363 | default: |
| 4364 | break; |
| 4365 | } |
| 4366 | |
| 4367 | list_for_each_entry(arg, &msg->args, list) { |
| 4368 | if (!(arg->expr->fetch->val & where)) { |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4369 | memprintf(err, "Ignore SPOE message '%s' at %s:%d: " |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4370 | "some args extract information from '%s', " |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4371 | "none of which is available here ('%s')", |
| 4372 | msg->id, msg->conf.file, msg->conf.line, |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4373 | sample_ckp_names(arg->expr->fetch->use), |
| 4374 | sample_ckp_names(where)); |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4375 | goto error; |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4376 | } |
| 4377 | } |
| 4378 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4379 | msg->agent = curagent; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4380 | LIST_APPEND(&curagent->events[msg->event], &msg->by_evt); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4381 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4382 | } |
| 4383 | } |
| 4384 | 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] | 4385 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4386 | goto error; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4387 | next_mph: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4388 | continue; |
| 4389 | } |
| 4390 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4391 | /* Replace placeholders by the corresponding groups for the SPOE |
| 4392 | * agent */ |
| 4393 | list_for_each_entry(ph, &curgphs, list) { |
| 4394 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4395 | if (strcmp(grp->id, ph->id) == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4396 | grp->agent = curagent; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4397 | LIST_DELETE(&grp->list); |
| 4398 | LIST_APPEND(&curagent->groups, &grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4399 | goto next_aph; |
| 4400 | } |
| 4401 | } |
| 4402 | memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d", |
| 4403 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 4404 | goto error; |
| 4405 | next_aph: |
| 4406 | continue; |
| 4407 | } |
| 4408 | |
| 4409 | /* Replace placeholders by the corresponding message for each SPOE |
| 4410 | * group of the SPOE agent */ |
| 4411 | list_for_each_entry(grp, &curagent->groups, list) { |
| 4412 | list_for_each_entry_safe(ph, phback, &grp->phs, list) { |
| 4413 | list_for_each_entry(msg, &curmsgs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4414 | if (strcmp(msg->id, ph->id) == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4415 | if (msg->group != NULL) { |
| 4416 | memprintf(err, "SPOE message '%s' already belongs to " |
| 4417 | "the SPOE group '%s' declare at %s:%d", |
| 4418 | msg->id, msg->group->id, |
| 4419 | msg->group->conf.file, |
| 4420 | msg->group->conf.line); |
| 4421 | goto error; |
| 4422 | } |
| 4423 | |
| 4424 | /* Scope for arguments are not checked for now. We will check |
| 4425 | * them only if a rule use the corresponding SPOE group. */ |
| 4426 | msg->agent = curagent; |
| 4427 | msg->group = grp; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4428 | LIST_DELETE(&ph->list); |
| 4429 | LIST_APPEND(&grp->messages, &msg->by_grp); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4430 | goto next_mph_grp; |
| 4431 | } |
| 4432 | } |
| 4433 | memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d", |
| 4434 | grp->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 4435 | goto error; |
| 4436 | next_mph_grp: |
| 4437 | continue; |
| 4438 | } |
| 4439 | } |
| 4440 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4441 | finish: |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4442 | /* move curmsgs to the agent message list */ |
| 4443 | curmsgs.n->p = &curagent->messages; |
| 4444 | curmsgs.p->n = &curagent->messages; |
| 4445 | curagent->messages = curmsgs; |
| 4446 | LIST_INIT(&curmsgs); |
| 4447 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 4448 | conf->id = strdup(engine ? engine : curagent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4449 | conf->agent = curagent; |
Christopher Faulet | 434b852 | 2021-08-02 17:51:01 +0200 | [diff] [blame] | 4450 | curagent->spoe_conf = conf; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4451 | |
| 4452 | /* Start agent's proxy initialization here. It will be finished during |
| 4453 | * the filter init. */ |
| 4454 | memset(&conf->agent_fe, 0, sizeof(conf->agent_fe)); |
| 4455 | init_new_proxy(&conf->agent_fe); |
| 4456 | conf->agent_fe.id = conf->agent->id; |
| 4457 | conf->agent_fe.parent = conf->agent; |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 4458 | conf->agent_fe.options |= curpxopts; |
| 4459 | conf->agent_fe.options2 |= curpxopts2; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4460 | |
| 4461 | list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4462 | LIST_DELETE(&logsrv->list); |
| 4463 | LIST_APPEND(&conf->agent_fe.logsrvs, &logsrv->list); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4464 | } |
| 4465 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4466 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4467 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4468 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4469 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4470 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4471 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4472 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4473 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4474 | list_for_each_entry_safe(vph, vphback, &curvars, list) { |
| 4475 | struct arg arg; |
| 4476 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4477 | trash.data = snprintf(trash.area, trash.size, "proc.%s.%s", |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4478 | curagent->var_pfx, vph->name); |
| 4479 | |
| 4480 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4481 | arg.data.str.area = trash.area; |
| 4482 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4483 | 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] | 4484 | if (!vars_check_arg(&arg, err)) { |
| 4485 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4486 | curagent->id, curagent->var_pfx, vph->name, *err); |
| 4487 | goto error; |
| 4488 | } |
| 4489 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4490 | LIST_DELETE(&vph->list); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4491 | free(vph->name); |
| 4492 | free(vph); |
| 4493 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4494 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4495 | LIST_DELETE(&grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4496 | spoe_release_group(grp); |
| 4497 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4498 | *cur_arg = pos; |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 4499 | fconf->id = spoe_filter_id; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4500 | fconf->ops = &spoe_ops; |
| 4501 | fconf->conf = conf; |
| 4502 | return 0; |
| 4503 | |
| 4504 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4505 | spoe_release_agent(curagent); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4506 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4507 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4508 | spoe_release_placeholder(ph); |
| 4509 | } |
| 4510 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4511 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4512 | spoe_release_placeholder(ph); |
| 4513 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4514 | list_for_each_entry_safe(vph, vphback, &curvars, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4515 | LIST_DELETE(&vph->list); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4516 | free(vph->name); |
| 4517 | free(vph); |
| 4518 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4519 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4520 | LIST_DELETE(&grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4521 | spoe_release_group(grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4522 | } |
| 4523 | list_for_each_entry_safe(msg, msgback, &curmsgs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4524 | LIST_DELETE(&msg->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4525 | spoe_release_message(msg); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4526 | } |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4527 | list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4528 | LIST_DELETE(&logsrv->list); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4529 | free(logsrv); |
| 4530 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4531 | free(conf); |
| 4532 | return -1; |
| 4533 | } |
| 4534 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4535 | /* Send message of a SPOE group. This is the action_ptr callback of a rule |
| 4536 | * associated to a "send-spoe-group" action. |
| 4537 | * |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4538 | * It returns ACT_RET_CONT if processing is finished (with error or not), it returns |
| 4539 | * ACT_RET_YIELD if the action is in progress. */ |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4540 | static enum act_return |
| 4541 | spoe_send_group(struct act_rule *rule, struct proxy *px, |
| 4542 | struct session *sess, struct stream *s, int flags) |
| 4543 | { |
| 4544 | struct filter *filter; |
| 4545 | struct spoe_agent *agent = NULL; |
| 4546 | struct spoe_group *group = NULL; |
| 4547 | struct spoe_context *ctx = NULL; |
| 4548 | int ret, dir; |
| 4549 | |
| 4550 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 4551 | if (filter->config == rule->arg.act.p[0]) { |
| 4552 | agent = rule->arg.act.p[2]; |
| 4553 | group = rule->arg.act.p[3]; |
| 4554 | ctx = filter->ctx; |
| 4555 | break; |
| 4556 | } |
| 4557 | } |
| 4558 | if (agent == NULL || group == NULL || ctx == NULL) |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4559 | return ACT_RET_CONT; |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4560 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 4561 | return ACT_RET_CONT; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4562 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4563 | switch (rule->from) { |
| 4564 | case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break; |
| 4565 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 4566 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 4567 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 4568 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
| 4569 | default: |
| 4570 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 4571 | " - internal error while execute spoe-send-group\n", |
| 4572 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 4573 | __FUNCTION__, s); |
| 4574 | send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n", |
| 4575 | agent->id); |
| 4576 | return ACT_RET_CONT; |
| 4577 | } |
| 4578 | |
| 4579 | ret = spoe_process_group(s, ctx, group, dir); |
| 4580 | if (ret == 1) |
| 4581 | return ACT_RET_CONT; |
| 4582 | else if (ret == 0) { |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 4583 | if (flags & ACT_OPT_FINAL) { |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4584 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 4585 | " - failed to process group '%s': interrupted by caller\n", |
| 4586 | (int)now.tv_sec, (int)now.tv_usec, |
| 4587 | agent->id, __FUNCTION__, s, group->id); |
| 4588 | ctx->status_code = SPOE_CTX_ERR_INTERRUPT; |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 4589 | spoe_stop_processing(agent, ctx); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 4590 | spoe_handle_processing_error(s, agent, ctx, dir); |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4591 | return ACT_RET_CONT; |
| 4592 | } |
| 4593 | return ACT_RET_YIELD; |
| 4594 | } |
| 4595 | else |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4596 | return ACT_RET_CONT; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4597 | } |
| 4598 | |
| 4599 | /* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE |
| 4600 | * group associated to <rule>. The format of an rule using 'send-spoe-group' |
| 4601 | * action should be: |
| 4602 | * |
| 4603 | * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id> |
| 4604 | * |
| 4605 | * So, we'll loop on each configured SPOE filter for the proxy <px> to find the |
| 4606 | * SPOE engine matching <engine-id>. And then, we'll try to find the good group |
| 4607 | * matching <group-id>. Finally, we'll check all messages referenced by the SPOE |
| 4608 | * group. |
| 4609 | * |
| 4610 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 4611 | * filled. |
| 4612 | */ |
| 4613 | static int |
| 4614 | check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err) |
| 4615 | { |
| 4616 | struct flt_conf *fconf; |
| 4617 | struct spoe_config *conf; |
| 4618 | struct spoe_agent *agent = NULL; |
| 4619 | struct spoe_group *group; |
| 4620 | struct spoe_message *msg; |
| 4621 | char *engine_id = rule->arg.act.p[0]; |
| 4622 | char *group_id = rule->arg.act.p[1]; |
| 4623 | unsigned int where = 0; |
| 4624 | |
| 4625 | switch (rule->from) { |
| 4626 | case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break; |
| 4627 | case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break; |
| 4628 | case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break; |
| 4629 | case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break; |
| 4630 | case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break; |
| 4631 | default: |
| 4632 | memprintf(err, |
| 4633 | "internal error, unexpected rule->from=%d, please report this bug!", |
| 4634 | rule->from); |
| 4635 | goto error; |
| 4636 | } |
| 4637 | |
| 4638 | /* Try to find the SPOE engine by checking all SPOE filters for proxy |
| 4639 | * <px> */ |
| 4640 | list_for_each_entry(fconf, &px->filter_configs, list) { |
| 4641 | conf = fconf->conf; |
| 4642 | |
| 4643 | /* This is not an SPOE filter */ |
| 4644 | if (fconf->id != spoe_filter_id) |
| 4645 | continue; |
| 4646 | |
| 4647 | /* This is the good engine */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4648 | if (strcmp(conf->id, engine_id) == 0) { |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4649 | agent = conf->agent; |
| 4650 | break; |
| 4651 | } |
| 4652 | } |
| 4653 | if (agent == NULL) { |
| 4654 | memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'", |
| 4655 | engine_id, group_id); |
| 4656 | goto error; |
| 4657 | } |
| 4658 | |
| 4659 | /* Try to find the right group */ |
| 4660 | list_for_each_entry(group, &agent->groups, list) { |
| 4661 | /* This is the good group */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4662 | if (strcmp(group->id, group_id) == 0) |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4663 | break; |
| 4664 | } |
| 4665 | if (&group->list == &agent->groups) { |
| 4666 | memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration", |
| 4667 | group_id, engine_id); |
| 4668 | goto error; |
| 4669 | } |
| 4670 | |
| 4671 | /* Ok, we found the group, we need to check messages and their |
| 4672 | * arguments */ |
| 4673 | list_for_each_entry(msg, &group->messages, by_grp) { |
| 4674 | struct spoe_arg *arg; |
| 4675 | |
| 4676 | list_for_each_entry(arg, &msg->args, list) { |
| 4677 | if (!(arg->expr->fetch->val & where)) { |
| 4678 | memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: " |
| 4679 | "some args extract information from '%s'," |
| 4680 | "none of which is available here ('%s')", |
| 4681 | msg->id, group->id, msg->conf.file, msg->conf.line, |
| 4682 | sample_ckp_names(arg->expr->fetch->use), |
| 4683 | sample_ckp_names(where)); |
| 4684 | goto error; |
| 4685 | } |
| 4686 | } |
| 4687 | } |
| 4688 | |
| 4689 | free(engine_id); |
| 4690 | free(group_id); |
| 4691 | rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */ |
| 4692 | rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */ |
| 4693 | rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */ |
| 4694 | rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */ |
| 4695 | return 1; |
| 4696 | |
| 4697 | error: |
| 4698 | free(engine_id); |
| 4699 | free(group_id); |
| 4700 | return 0; |
| 4701 | } |
| 4702 | |
| 4703 | /* Parse 'send-spoe-group' action following the format: |
| 4704 | * |
| 4705 | * ... send-spoe-group <engine-id> <group-id> |
| 4706 | * |
| 4707 | * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error |
| 4708 | * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group |
| 4709 | * ids are saved and used later, when the rule will be checked. |
| 4710 | */ |
| 4711 | static enum act_parse_ret |
| 4712 | parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px, |
| 4713 | struct act_rule *rule, char **err) |
| 4714 | { |
| 4715 | if (!*args[*orig_arg] || !*args[*orig_arg+1] || |
| 4716 | (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) { |
| 4717 | memprintf(err, "expects 2 arguments: <engine-id> <group-id>"); |
| 4718 | return ACT_RET_PRS_ERR; |
| 4719 | } |
| 4720 | rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */ |
| 4721 | rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */ |
| 4722 | |
| 4723 | (*orig_arg) += 2; |
| 4724 | |
| 4725 | rule->action = ACT_CUSTOM; |
| 4726 | rule->action_ptr = spoe_send_group; |
| 4727 | rule->check_ptr = check_send_spoe_group; |
| 4728 | return ACT_RET_PRS_OK; |
| 4729 | } |
| 4730 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4731 | |
| 4732 | /* Declare the filter parser for "spoe" keyword */ |
| 4733 | static struct flt_kw_list flt_kws = { "SPOE", { }, { |
| 4734 | { "spoe", parse_spoe_flt, NULL }, |
| 4735 | { NULL, NULL, NULL }, |
| 4736 | } |
| 4737 | }; |
| 4738 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4739 | INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws); |
| 4740 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4741 | /* Delcate the action parser for "spoe-action" keyword */ |
| 4742 | static struct action_kw_list tcp_req_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_req_cont_keywords_register, &tcp_req_action_kws); |
| 4749 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4750 | static struct action_kw_list tcp_res_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, tcp_res_cont_keywords_register, &tcp_res_action_kws); |
| 4757 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4758 | static struct action_kw_list http_req_action_kws = { { }, { |
| 4759 | { "send-spoe-group", parse_send_spoe_group }, |
| 4760 | { /* END */ }, |
| 4761 | } |
| 4762 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4763 | |
| 4764 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws); |
| 4765 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4766 | static struct action_kw_list http_res_action_kws = { { }, { |
| 4767 | { "send-spoe-group", parse_send_spoe_group }, |
| 4768 | { /* END */ }, |
| 4769 | } |
| 4770 | }; |
| 4771 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4772 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws); |