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