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 | } |
Aurelien DARRAGON | c610095 | 2023-05-11 12:29:51 +0200 | [diff] [blame] | 144 | free_acl_cond(msg->cond); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 145 | free(msg); |
| 146 | } |
| 147 | |
| 148 | static void |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 149 | spoe_release_group(struct spoe_group *grp) |
| 150 | { |
| 151 | if (!grp) |
| 152 | return; |
| 153 | free(grp->id); |
| 154 | free(grp->conf.file); |
| 155 | free(grp); |
| 156 | } |
| 157 | |
| 158 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 159 | spoe_release_agent(struct spoe_agent *agent) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 160 | { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 161 | struct spoe_message *msg, *msgback; |
| 162 | struct spoe_group *grp, *grpback; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 163 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 164 | |
| 165 | if (!agent) |
| 166 | return; |
| 167 | free(agent->id); |
| 168 | free(agent->conf.file); |
| 169 | free(agent->var_pfx); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 170 | free(agent->var_on_error); |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 171 | free(agent->var_t_process); |
| 172 | free(agent->var_t_total); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 173 | list_for_each_entry_safe(msg, msgback, &agent->messages, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 174 | LIST_DELETE(&msg->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 175 | spoe_release_message(msg); |
| 176 | } |
| 177 | list_for_each_entry_safe(grp, grpback, &agent->groups, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 178 | LIST_DELETE(&grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 179 | spoe_release_group(grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 180 | } |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 181 | if (agent->rt) { |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 182 | for (i = 0; i < global.nbthread; ++i) { |
| 183 | free(agent->rt[i].engine_id); |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 184 | HA_SPIN_DESTROY(&agent->rt[i].lock); |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 185 | } |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 186 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 187 | free(agent->rt); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 188 | free(agent); |
| 189 | } |
| 190 | |
| 191 | static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 192 | [SPOE_FRM_ERR_NONE] = "normal", |
| 193 | [SPOE_FRM_ERR_IO] = "I/O error", |
| 194 | [SPOE_FRM_ERR_TOUT] = "a timeout occurred", |
| 195 | [SPOE_FRM_ERR_TOO_BIG] = "frame is too big", |
| 196 | [SPOE_FRM_ERR_INVALID] = "invalid frame received", |
| 197 | [SPOE_FRM_ERR_NO_VSN] = "version value not found", |
| 198 | [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found", |
| 199 | [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found", |
| 200 | [SPOE_FRM_ERR_BAD_VSN] = "unsupported version", |
| 201 | [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small", |
| 202 | [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported", |
| 203 | [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames", |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 204 | [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 205 | [SPOE_FRM_ERR_RES] = "resource allocation error", |
| 206 | [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | static const char *spoe_event_str[SPOE_EV_EVENTS] = { |
| 210 | [SPOE_EV_ON_CLIENT_SESS] = "on-client-session", |
| 211 | [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request", |
| 212 | [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request", |
| 213 | [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request", |
| 214 | [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request", |
| 215 | |
| 216 | [SPOE_EV_ON_SERVER_SESS] = "on-server-session", |
| 217 | [SPOE_EV_ON_TCP_RSP] = "on-tcp-response", |
| 218 | [SPOE_EV_ON_HTTP_RSP] = "on-http-response", |
| 219 | }; |
| 220 | |
| 221 | |
| 222 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 223 | |
| 224 | static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 225 | [SPOE_CTX_ST_NONE] = "NONE", |
| 226 | [SPOE_CTX_ST_READY] = "READY", |
| 227 | [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS", |
| 228 | [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS", |
| 229 | [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK", |
| 230 | [SPOE_CTX_ST_DONE] = "DONE", |
| 231 | [SPOE_CTX_ST_ERROR] = "ERROR", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 235 | [SPOE_APPCTX_ST_CONNECT] = "CONNECT", |
| 236 | [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING", |
| 237 | [SPOE_APPCTX_ST_IDLE] = "IDLE", |
| 238 | [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING", |
| 239 | [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY", |
| 240 | [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK", |
| 241 | [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT", |
| 242 | [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING", |
| 243 | [SPOE_APPCTX_ST_EXIT] = "EXIT", |
| 244 | [SPOE_APPCTX_ST_END] = "END", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | #endif |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 248 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 249 | /* Used to generates a unique id for an engine. On success, it returns a |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 250 | * allocated string. So it is the caller's responsibility to release it. If the |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 251 | * allocation failed, it returns NULL. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 252 | static char * |
| 253 | generate_pseudo_uuid() |
| 254 | { |
Willy Tarreau | ee3bcdd | 2020-03-08 17:48:17 +0100 | [diff] [blame] | 255 | ha_generate_uuid(&trash); |
Kevin Zhu | 079f808 | 2020-03-13 10:39:51 +0800 | [diff] [blame] | 256 | return my_strndup(trash.area, trash.data); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Willy Tarreau | aaebcae | 2023-04-27 11:54:11 +0200 | [diff] [blame] | 259 | /* set/add to <t> the elapsed time since <since> and now */ |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 260 | static inline void |
Willy Tarreau | aaebcae | 2023-04-27 11:54:11 +0200 | [diff] [blame] | 261 | spoe_update_stat_time(ullong *since, long *t) |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 262 | { |
| 263 | if (*t == -1) |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 264 | *t = ns_to_ms(now_ns - *since); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 265 | else |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 266 | *t += ns_to_ms(now_ns - *since); |
Willy Tarreau | aaebcae | 2023-04-27 11:54:11 +0200 | [diff] [blame] | 267 | *since = 0; |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 270 | /******************************************************************** |
| 271 | * Functions that encode/decode SPOE frames |
| 272 | ********************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 273 | /* Helper to get static string length, excluding the terminating null byte */ |
| 274 | #define SLEN(str) (sizeof(str)-1) |
| 275 | |
| 276 | /* Predefined key used in HELLO/DISCONNECT frames */ |
| 277 | #define SUPPORTED_VERSIONS_KEY "supported-versions" |
| 278 | #define VERSION_KEY "version" |
| 279 | #define MAX_FRAME_SIZE_KEY "max-frame-size" |
| 280 | #define CAPABILITIES_KEY "capabilities" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 281 | #define ENGINE_ID_KEY "engine-id" |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 282 | #define HEALTHCHECK_KEY "healthcheck" |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 283 | #define STATUS_CODE_KEY "status-code" |
| 284 | #define MSG_KEY "message" |
| 285 | |
| 286 | struct spoe_version { |
| 287 | char *str; |
| 288 | int min; |
| 289 | int max; |
| 290 | }; |
| 291 | |
| 292 | /* All supported versions */ |
| 293 | static struct spoe_version supported_versions[] = { |
Christopher Faulet | 6381650 | 2018-05-31 14:56:42 +0200 | [diff] [blame] | 294 | /* 1.0 is now unsupported because of a bug about frame's flags*/ |
| 295 | {"2.0", 2000, 2000}, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 296 | {NULL, 0, 0} |
| 297 | }; |
| 298 | |
| 299 | /* Comma-separated list of supported versions */ |
Christopher Faulet | 6381650 | 2018-05-31 14:56:42 +0200 | [diff] [blame] | 300 | #define SUPPORTED_VERSIONS_VAL "2.0" |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 301 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 302 | /* Convert a string to a SPOE version value. The string must follow the format |
| 303 | * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR). |
| 304 | * If an error occurred, -1 is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 305 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 306 | spoe_str_to_vsn(const char *str, size_t len) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 307 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 308 | const char *p, *end; |
| 309 | int maj, min, vsn; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 310 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 311 | p = str; |
| 312 | end = str+len; |
| 313 | maj = min = 0; |
| 314 | vsn = -1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 315 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 316 | /* skip leading spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 317 | while (p < end && isspace((unsigned char)*p)) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 318 | p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 319 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 320 | /* parse Major number, until the '.' */ |
| 321 | while (*p != '.') { |
| 322 | if (p >= end || *p < '0' || *p > '9') |
| 323 | goto out; |
| 324 | maj *= 10; |
| 325 | maj += (*p - '0'); |
| 326 | p++; |
| 327 | } |
| 328 | |
| 329 | /* check Major version */ |
| 330 | if (!maj) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 331 | goto out; |
| 332 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 333 | p++; /* skip the '.' */ |
| 334 | if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */ |
| 335 | goto out; |
| 336 | |
| 337 | /* Parse Minor number */ |
| 338 | while (p < end) { |
| 339 | if (*p < '0' || *p > '9') |
| 340 | break; |
| 341 | min *= 10; |
| 342 | min += (*p - '0'); |
| 343 | p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 344 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 345 | |
| 346 | /* check Minor number */ |
| 347 | if (min > 999) |
| 348 | goto out; |
| 349 | |
| 350 | /* skip trailing spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 351 | while (p < end && isspace((unsigned char)*p)) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 352 | p++; |
| 353 | if (p != end) |
| 354 | goto out; |
| 355 | |
| 356 | vsn = maj * 1000 + min; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 357 | out: |
| 358 | return vsn; |
| 359 | } |
| 360 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 361 | /* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of |
Joseph Herlant | f7f6031 | 2018-11-15 13:46:49 -0800 | [diff] [blame] | 362 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 363 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 364 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 365 | spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 366 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 367 | struct buffer *chk; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 368 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 369 | char *p, *end; |
| 370 | unsigned int flags = SPOE_FRM_FL_FIN; |
| 371 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 372 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 373 | p = frame; |
| 374 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 375 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 376 | /* Set Frame type */ |
| 377 | *p++ = SPOE_FRM_T_HAPROXY_HELLO; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 378 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 379 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 380 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 381 | memcpy(p, (char *)&flags, 4); |
| 382 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 383 | |
| 384 | /* No stream-id and frame-id for HELLO frames */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 385 | *p++ = 0; *p++ = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 386 | |
| 387 | /* There are 3 mandatory items: "supported-versions", "max-frame-size" |
| 388 | * and "capabilities" */ |
| 389 | |
| 390 | /* "supported-versions" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 391 | sz = SLEN(SUPPORTED_VERSIONS_KEY); |
| 392 | if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1) |
| 393 | goto too_big; |
| 394 | |
| 395 | *p++ = SPOE_DATA_T_STR; |
| 396 | sz = SLEN(SUPPORTED_VERSIONS_VAL); |
| 397 | if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1) |
| 398 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 399 | |
| 400 | /* "max-fram-size" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 401 | sz = SLEN(MAX_FRAME_SIZE_KEY); |
| 402 | if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1) |
| 403 | goto too_big; |
| 404 | |
| 405 | *p++ = SPOE_DATA_T_UINT32; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 406 | if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 407 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 408 | |
| 409 | /* "capabilities" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 410 | sz = SLEN(CAPABILITIES_KEY); |
| 411 | if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1) |
| 412 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 413 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 414 | *p++ = SPOE_DATA_T_STR; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 415 | chk = get_trash_chunk(); |
| 416 | if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 417 | memcpy(chk->area, "pipelining", 10); |
| 418 | chk->data += 10; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 419 | } |
| 420 | if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 421 | if (chk->data) chk->area[chk->data++] = ','; |
| 422 | memcpy(chk->area+chk->data, "async", 5); |
| 423 | chk->data += 5; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 424 | } |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 425 | if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 426 | if (chk->data) chk->area[chk->data++] = ','; |
| 427 | memcpy(chk->area+chk->data, "fragmentation", 13); |
Miroslav Zagorac | 6b3690b | 2019-01-13 16:55:01 +0100 | [diff] [blame] | 428 | chk->data += 13; |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 429 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 430 | if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 431 | goto too_big; |
| 432 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 433 | /* (optional) "engine-id" K/V item, if present */ |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 434 | if (agent != NULL && agent->rt[tid].engine_id != NULL) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 435 | sz = SLEN(ENGINE_ID_KEY); |
| 436 | if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1) |
| 437 | goto too_big; |
| 438 | |
| 439 | *p++ = SPOE_DATA_T_STR; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 440 | sz = strlen(agent->rt[tid].engine_id); |
| 441 | if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 442 | goto too_big; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 443 | } |
| 444 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 445 | return (p - frame); |
| 446 | |
| 447 | too_big: |
| 448 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 449 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 450 | } |
| 451 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 452 | /* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of |
| 453 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
| 454 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 455 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 456 | spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 457 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 458 | const char *reason; |
| 459 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 460 | unsigned int flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 461 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 462 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 463 | p = frame; |
| 464 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 465 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 466 | /* Set Frame type */ |
| 467 | *p++ = SPOE_FRM_T_HAPROXY_DISCON; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 468 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 469 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 470 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 471 | memcpy(p, (char *)&flags, 4); |
| 472 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 473 | |
| 474 | /* No stream-id and frame-id for DISCONNECT frames */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 475 | *p++ = 0; *p++ = 0; |
| 476 | |
| 477 | if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS) |
| 478 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 479 | |
| 480 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 481 | |
| 482 | /* "status-code" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 483 | sz = SLEN(STATUS_CODE_KEY); |
| 484 | if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1) |
| 485 | goto too_big; |
| 486 | |
| 487 | *p++ = SPOE_DATA_T_UINT32; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 488 | if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 489 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 490 | |
| 491 | /* "message" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 492 | sz = SLEN(MSG_KEY); |
| 493 | if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1) |
| 494 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 495 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 496 | /*Get the message corresponding to the status code */ |
| 497 | reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]; |
| 498 | |
| 499 | *p++ = SPOE_DATA_T_STR; |
| 500 | sz = strlen(reason); |
| 501 | if (spoe_encode_buffer(reason, sz, &p, end) == -1) |
| 502 | goto too_big; |
| 503 | |
| 504 | return (p - frame); |
| 505 | |
| 506 | too_big: |
| 507 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 508 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 509 | } |
| 510 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 511 | /* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of |
| 512 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
| 513 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 514 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 515 | spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 516 | char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 517 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 518 | char *p, *end; |
| 519 | unsigned int stream_id, frame_id; |
| 520 | unsigned int flags = SPOE_FRM_FL_FIN; |
| 521 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 522 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 523 | p = frame; |
| 524 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 525 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 526 | stream_id = ctx->stream_id; |
| 527 | frame_id = ctx->frame_id; |
| 528 | |
| 529 | if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) { |
| 530 | /* The fragmentation is not supported by the applet */ |
| 531 | if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) { |
| 532 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
| 533 | return -1; |
| 534 | } |
| 535 | flags = ctx->frag_ctx.flags; |
| 536 | } |
| 537 | |
| 538 | /* Set Frame type */ |
| 539 | *p++ = SPOE_FRM_T_HAPROXY_NOTIFY; |
| 540 | |
| 541 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 542 | flags = htonl(flags); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 543 | memcpy(p, (char *)&flags, 4); |
| 544 | p += 4; |
| 545 | |
| 546 | /* Set stream-id and frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 547 | if (encode_varint(stream_id, &p, end) == -1) |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 548 | goto too_big; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 549 | if (encode_varint(frame_id, &p, end) == -1) |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 550 | goto too_big; |
| 551 | |
| 552 | /* Copy encoded messages, if possible */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 553 | sz = b_data(&ctx->buffer); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 554 | if (p + sz >= end) |
| 555 | goto too_big; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 556 | memcpy(p, b_head(&ctx->buffer), sz); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 557 | p += sz; |
| 558 | |
| 559 | return (p - frame); |
| 560 | |
| 561 | too_big: |
| 562 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | /* Encode next part of a fragmented frame sent by HAProxy to an agent. It |
| 567 | * returns the number of encoded bytes in the frame on success, 0 if an encoding |
| 568 | * error occurred and -1 if a fatal error occurred. */ |
| 569 | static int |
| 570 | spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx, |
| 571 | char *frame, size_t size) |
| 572 | { |
| 573 | char *p, *end; |
| 574 | unsigned int stream_id, frame_id; |
| 575 | unsigned int flags; |
| 576 | size_t sz; |
| 577 | |
| 578 | p = frame; |
| 579 | end = frame+size; |
| 580 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 581 | /* <ctx> is null when the stream has aborted the processing of a |
| 582 | * fragmented frame. In this case, we must notify the corresponding |
| 583 | * agent using ids stored in <frag_ctx>. */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 584 | if (ctx == NULL) { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 585 | flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 586 | stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid; |
| 587 | frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid; |
| 588 | } |
| 589 | else { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 590 | flags = ctx->frag_ctx.flags; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 591 | stream_id = ctx->stream_id; |
| 592 | frame_id = ctx->frame_id; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 593 | } |
| 594 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 595 | /* Set Frame type */ |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 596 | *p++ = SPOE_FRM_T_UNSET; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 597 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 598 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 599 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 600 | memcpy(p, (char *)&flags, 4); |
| 601 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 602 | |
| 603 | /* Set stream-id and frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 604 | if (encode_varint(stream_id, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 605 | goto too_big; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 606 | if (encode_varint(frame_id, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 607 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 608 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 609 | if (ctx == NULL) |
| 610 | goto end; |
| 611 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 612 | /* Copy encoded messages, if possible */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 613 | sz = b_data(&ctx->buffer); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 614 | if (p + sz >= end) |
| 615 | goto too_big; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 616 | memcpy(p, b_head(&ctx->buffer), sz); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 617 | p += sz; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 618 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 619 | end: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 620 | return (p - frame); |
| 621 | |
| 622 | too_big: |
| 623 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 624 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 625 | } |
| 626 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 627 | /* Decode and process the HELLO frame sent by an agent. It returns the number of |
| 628 | * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal |
| 629 | * error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 630 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 631 | spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 632 | { |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 633 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
| 634 | char *p, *end; |
| 635 | int vsn, max_frame_size; |
| 636 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 637 | |
| 638 | p = frame; |
| 639 | end = frame + size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 640 | |
| 641 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 642 | if (*p++ != SPOE_FRM_T_AGENT_HELLO) { |
| 643 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 644 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 645 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 646 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 647 | if (size < 7 /* TYPE + METADATA */) { |
| 648 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 649 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 652 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 653 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 654 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 655 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 656 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 657 | /* Fragmentation is not supported for HELLO frame */ |
| 658 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 659 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 660 | return -1; |
| 661 | } |
| 662 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 663 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 664 | if (*p != 0 || *(p+1) != 0) { |
| 665 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 666 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 667 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 668 | p += 2; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 669 | |
| 670 | /* There are 3 mandatory items: "version", "max-frame-size" and |
| 671 | * "capabilities" */ |
| 672 | |
| 673 | /* Loop on K/V items */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 674 | vsn = max_frame_size = flags = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 675 | while (p < end) { |
| 676 | char *str; |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 677 | uint64_t sz; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 678 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 679 | |
| 680 | /* Decode the item key */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 681 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 682 | if (ret == -1 || !sz) { |
| 683 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 684 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 685 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 686 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 687 | /* Check "version" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 688 | if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 689 | int i, type = *p++; |
| 690 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 691 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 692 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 693 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 694 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 695 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 696 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) { |
| 697 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 698 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 699 | } |
| 700 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 701 | vsn = spoe_str_to_vsn(str, sz); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 702 | if (vsn == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 703 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 704 | return -1; |
| 705 | } |
| 706 | for (i = 0; supported_versions[i].str != NULL; ++i) { |
| 707 | if (vsn >= supported_versions[i].min && |
| 708 | vsn <= supported_versions[i].max) |
| 709 | break; |
| 710 | } |
| 711 | if (supported_versions[i].str == NULL) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 712 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 713 | return -1; |
| 714 | } |
| 715 | } |
| 716 | /* Check "max-frame-size" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 717 | else if (sz >= strlen(MAX_FRAME_SIZE_KEY) && !memcmp(str, MAX_FRAME_SIZE_KEY, strlen(MAX_FRAME_SIZE_KEY))) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 718 | int type = *p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 719 | |
| 720 | /* The value must be integer */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 721 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 722 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 723 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 724 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 725 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 726 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 727 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 728 | if (decode_varint(&p, end, &sz) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 729 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 730 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 731 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 732 | if (sz < MIN_FRAME_SIZE || |
| 733 | sz > SPOE_APPCTX(appctx)->max_frame_size) { |
| 734 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 735 | return -1; |
| 736 | } |
| 737 | max_frame_size = sz; |
| 738 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 739 | /* Check "capabilities" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 740 | else if (sz >= strlen(CAPABILITIES_KEY) && !memcmp(str, CAPABILITIES_KEY, strlen(CAPABILITIES_KEY))) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 741 | int type = *p++; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 742 | |
| 743 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 744 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 745 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 746 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 747 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 748 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) { |
| 749 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 750 | return 0; |
| 751 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 752 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 753 | while (sz) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 754 | char *delim; |
| 755 | |
| 756 | /* Skip leading spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 757 | for (; isspace((unsigned char)*str) && sz; str++, sz--); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 758 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 759 | if (sz >= 10 && !strncmp(str, "pipelining", 10)) { |
| 760 | str += 10; sz -= 10; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 761 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 762 | flags |= SPOE_APPCTX_FL_PIPELINING; |
| 763 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 764 | else if (sz >= 5 && !strncmp(str, "async", 5)) { |
| 765 | str += 5; sz -= 5; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 766 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 767 | flags |= SPOE_APPCTX_FL_ASYNC; |
| 768 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 769 | else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) { |
| 770 | str += 13; sz -= 13; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 771 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 772 | flags |= SPOE_APPCTX_FL_FRAGMENTATION; |
| 773 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 774 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 775 | /* Get the next comma or break */ |
| 776 | if (!sz || (delim = memchr(str, ',', sz)) == NULL) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 777 | break; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 778 | delim++; |
| 779 | sz -= (delim - str); |
| 780 | str = delim; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 781 | } |
| 782 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 783 | else { |
| 784 | /* Silently ignore unknown item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 785 | if (spoe_skip_data(&p, end) == -1) { |
| 786 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 787 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 788 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
| 792 | /* Final checks */ |
| 793 | if (!vsn) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 794 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 795 | return -1; |
| 796 | } |
| 797 | if (!max_frame_size) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 798 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 799 | return -1; |
| 800 | } |
Christopher Faulet | 1138901 | 2019-02-07 16:13:26 +0100 | [diff] [blame] | 801 | if (!agent) |
| 802 | flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC); |
| 803 | else { |
| 804 | if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING)) |
| 805 | flags &= ~SPOE_APPCTX_FL_PIPELINING; |
| 806 | if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC)) |
| 807 | flags &= ~SPOE_APPCTX_FL_ASYNC; |
| 808 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 809 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 810 | SPOE_APPCTX(appctx)->version = (unsigned int)vsn; |
| 811 | SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size; |
| 812 | SPOE_APPCTX(appctx)->flags |= flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 813 | |
| 814 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | /* Decode DISCONNECT frame sent by an agent. It returns the number of by read |
| 818 | * bytes on success, 0 if the frame can be ignored and -1 if an error |
| 819 | * occurred. */ |
| 820 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 821 | spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 822 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 823 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 824 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 825 | |
| 826 | p = frame; |
| 827 | end = frame + size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 828 | |
| 829 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 830 | if (*p++ != SPOE_FRM_T_AGENT_DISCON) { |
| 831 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 832 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 833 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 834 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 835 | if (size < 7 /* TYPE + METADATA */) { |
| 836 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 837 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 838 | } |
| 839 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 840 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 841 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 842 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 843 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 844 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 845 | /* Fragmentation is not supported for DISCONNECT frame */ |
| 846 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 847 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 848 | return -1; |
| 849 | } |
| 850 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 851 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 852 | if (*p != 0 || *(p+1) != 0) { |
| 853 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 854 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 855 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 856 | p += 2; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 857 | |
| 858 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 859 | |
| 860 | /* Loop on K/V items */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 861 | while (p < end) { |
| 862 | char *str; |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 863 | uint64_t sz; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 864 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 865 | |
| 866 | /* Decode the item key */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 867 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 868 | if (ret == -1 || !sz) { |
| 869 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 870 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | /* Check "status-code" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 874 | if (sz >= strlen(STATUS_CODE_KEY) && !memcmp(str, STATUS_CODE_KEY, strlen(STATUS_CODE_KEY))) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 875 | int type = *p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 876 | |
| 877 | /* The value must be an integer */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 878 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 879 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 880 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 881 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 882 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 883 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 884 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 885 | if (decode_varint(&p, end, &sz) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 886 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 887 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 888 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 889 | SPOE_APPCTX(appctx)->status_code = sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | /* Check "message" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 893 | else if (sz >= strlen(MSG_KEY) && !memcmp(str, MSG_KEY, strlen(MSG_KEY))) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 894 | int type = *p++; |
| 895 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 896 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 897 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 898 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 899 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 900 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 901 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 902 | if (ret == -1 || sz > 255) { |
| 903 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 904 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 905 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 906 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 907 | SPOE_APPCTX(appctx)->reason = str; |
| 908 | SPOE_APPCTX(appctx)->rlen = sz; |
| 909 | #endif |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 910 | } |
| 911 | else { |
| 912 | /* Silently ignore unknown item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 913 | if (spoe_skip_data(&p, end) == -1) { |
| 914 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 915 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 916 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 920 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 924 | /* Decode ACK frame sent by an agent. It returns the number of read bytes on |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 925 | * success, 0 if the frame can be ignored and -1 if an error occurred. */ |
| 926 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 927 | spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 928 | char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 929 | { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 930 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 931 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 932 | uint64_t stream_id, frame_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 933 | int len; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 934 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 935 | |
| 936 | p = frame; |
| 937 | end = frame + size; |
| 938 | *ctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 939 | |
| 940 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 941 | if (*p++ != SPOE_FRM_T_AGENT_ACK) { |
| 942 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 943 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 944 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 945 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 946 | if (size < 7 /* TYPE + METADATA */) { |
| 947 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 948 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 949 | } |
| 950 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 951 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 952 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 953 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 954 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 955 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 956 | /* Fragmentation is not supported for now */ |
| 957 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 958 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 959 | return -1; |
| 960 | } |
| 961 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 962 | /* Get the stream-id and the frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 963 | if (decode_varint(&p, end, &stream_id) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 964 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 965 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 966 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 967 | if (decode_varint(&p, end, &frame_id) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 968 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 969 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 970 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 971 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 972 | /* Try to find the corresponding SPOE context */ |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 973 | if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 974 | list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 975 | if ((*ctx)->stream_id == (unsigned int)stream_id && |
| 976 | (*ctx)->frame_id == (unsigned int)frame_id) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 977 | goto found; |
| 978 | } |
| 979 | } |
| 980 | else { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 981 | list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) { |
| 982 | if ((*ctx)->stream_id == (unsigned int)stream_id && |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 983 | (*ctx)->frame_id == (unsigned int)frame_id) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 984 | goto found; |
| 985 | } |
| 986 | } |
| 987 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 988 | if (SPOE_APPCTX(appctx)->frag_ctx.ctx && |
| 989 | SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id && |
| 990 | SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) { |
| 991 | |
| 992 | /* ABRT bit is set for an unfinished fragmented frame */ |
| 993 | if (flags & SPOE_FRM_FL_ABRT) { |
| 994 | *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx; |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 995 | (*ctx)->state = SPOE_CTX_ST_ERROR; |
| 996 | (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT; |
| 997 | /* Ignore the payload */ |
| 998 | goto end; |
| 999 | } |
| 1000 | /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */ |
| 1001 | /* For now, we ignore the ack */ |
| 1002 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 1003 | return 0; |
| 1004 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1005 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1006 | /* No Stream found, ignore the frame */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1007 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1008 | " - Ignore ACK frame" |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1009 | " - stream-id=%u - frame-id=%u\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 1010 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1011 | __FUNCTION__, appctx, |
| 1012 | (unsigned int)stream_id, (unsigned int)frame_id); |
| 1013 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1014 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND; |
Christopher Faulet | c7ba910 | 2020-11-10 14:31:39 +0100 | [diff] [blame] | 1015 | if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) { |
| 1016 | /* Report an error if we are waiting the ack for another frame, |
| 1017 | * but not if there is no longer frame waiting for a ack |
| 1018 | * (timeout) |
| 1019 | */ |
| 1020 | if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) || |
| 1021 | SPOE_APPCTX(appctx)->frag_ctx.ctx) |
| 1022 | return -1; |
| 1023 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1024 | SPOE_APPCTX(appctx)->cur_fpa = 0; |
| 1025 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1026 | return 0; |
| 1027 | |
| 1028 | found: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1029 | if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer, |
| 1030 | &SPOE_APPCTX(appctx)->buffer_wait)) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1031 | *ctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1032 | return 1; /* Retry later */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1033 | } |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1034 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1035 | /* Copy encoded actions */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1036 | len = (end - p); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1037 | memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len); |
| 1038 | b_set_data(&SPOE_APPCTX(appctx)->buffer, len); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1039 | p += len; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1040 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1041 | /* Transfer the buffer ownership to the SPOE context */ |
| 1042 | (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1043 | SPOE_APPCTX(appctx)->buffer = BUF_NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1044 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1045 | (*ctx)->state = SPOE_CTX_ST_DONE; |
| 1046 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1047 | end: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1048 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1049 | " - ACK frame received" |
| 1050 | " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 1051 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1052 | __FUNCTION__, appctx, *ctx, (*ctx)->stream_id, |
| 1053 | (*ctx)->frame_id, flags); |
| 1054 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1055 | } |
| 1056 | |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1057 | /* This function is used in cfgparse.c and declared in proto/checks.h. It |
| 1058 | * prepare the request to send to agents during a healthcheck. It returns 0 on |
| 1059 | * success and -1 if an error occurred. */ |
| 1060 | int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1061 | spoe_prepare_healthcheck_request(char **req, int *len) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1062 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1063 | struct appctx appctx; |
| 1064 | struct spoe_appctx spoe_appctx; |
| 1065 | char *frame, *end, buf[MAX_FRAME_SIZE+4]; |
| 1066 | size_t sz; |
| 1067 | int ret; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1068 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1069 | memset(&appctx, 0, sizeof(appctx)); |
| 1070 | memset(&spoe_appctx, 0, sizeof(spoe_appctx)); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1071 | memset(buf, 0, sizeof(buf)); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1072 | |
Willy Tarreau | 23a2407 | 2022-05-05 20:18:44 +0200 | [diff] [blame] | 1073 | appctx.svcctx = &spoe_appctx; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1074 | SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1075 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1076 | frame = buf+4; /* Reserved the 4 first bytes for the frame size */ |
| 1077 | end = frame + MAX_FRAME_SIZE; |
| 1078 | |
| 1079 | ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE); |
| 1080 | if (ret <= 0) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1081 | return -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1082 | frame += ret; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1083 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1084 | /* Add "healthcheck" K/V item */ |
| 1085 | sz = SLEN(HEALTHCHECK_KEY); |
| 1086 | if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1) |
| 1087 | return -1; |
| 1088 | *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1089 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1090 | *len = frame - buf; |
| 1091 | sz = htonl(*len - 4); |
| 1092 | memcpy(buf, (char *)&sz, 4); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1093 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1094 | if ((*req = malloc(*len)) == NULL) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1095 | return -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1096 | memcpy(*req, buf, *len); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | /* This function is used in checks.c and declared in proto/checks.h. It decode |
| 1101 | * the response received from an agent during a healthcheck. It returns 0 on |
| 1102 | * success and -1 if an error occurred. */ |
| 1103 | int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1104 | spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1105 | { |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1106 | struct appctx appctx; |
| 1107 | struct spoe_appctx spoe_appctx; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1108 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1109 | memset(&appctx, 0, sizeof(appctx)); |
| 1110 | memset(&spoe_appctx, 0, sizeof(spoe_appctx)); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1111 | |
Willy Tarreau | 23a2407 | 2022-05-05 20:18:44 +0200 | [diff] [blame] | 1112 | appctx.svcctx = &spoe_appctx; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1113 | SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1114 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1115 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1116 | spoe_handle_agentdiscon_frame(&appctx, frame, size); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1117 | goto error; |
| 1118 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1119 | if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0) |
| 1120 | goto error; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1121 | |
| 1122 | return 0; |
| 1123 | |
| 1124 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1125 | if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS) |
| 1126 | SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN; |
| 1127 | strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1128 | return -1; |
| 1129 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1130 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1131 | /* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1132 | * the frame can be ignored, 1 to retry later, and the frame length on |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1133 | * success. */ |
| 1134 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1135 | spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1136 | { |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 1137 | struct stconn *sc = appctx_sc(appctx); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1138 | int ret; |
| 1139 | uint32_t netint; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1140 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1141 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1142 | * length. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1143 | netint = htonl(framesz); |
| 1144 | memcpy(buf, (char *)&netint, 4); |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 1145 | ret = applet_putblk(appctx, buf, framesz+4); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1146 | if (ret <= 0) { |
Christopher Faulet | 7b3d38a | 2023-05-05 11:28:45 +0200 | [diff] [blame] | 1147 | if (ret == -3 && b_is_null(&sc_ic(sc)->buf)) { |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 1148 | /* WT: is this still needed for the case ret==-3 ? */ |
Christopher Faulet | 7b3d38a | 2023-05-05 11:28:45 +0200 | [diff] [blame] | 1149 | sc_need_room(sc, 0); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1150 | return 1; /* retry */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1151 | } |
| 1152 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1153 | return -1; /* error */ |
| 1154 | } |
| 1155 | return framesz; |
| 1156 | } |
| 1157 | |
| 1158 | /* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0 |
| 1159 | * when the frame can be ignored, 1 to retry later and the frame length on |
| 1160 | * success. */ |
| 1161 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1162 | spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1163 | { |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 1164 | struct stconn *sc = appctx_sc(appctx); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1165 | int ret; |
| 1166 | uint32_t netint; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1167 | |
Willy Tarreau | 9002a2e | 2022-05-27 10:34:25 +0200 | [diff] [blame] | 1168 | ret = co_getblk(sc_oc(sc), (char *)&netint, 4, 0); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1169 | if (ret > 0) { |
| 1170 | framesz = ntohl(netint); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1171 | if (framesz > SPOE_APPCTX(appctx)->max_frame_size) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1172 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1173 | return -1; |
| 1174 | } |
Willy Tarreau | 9002a2e | 2022-05-27 10:34:25 +0200 | [diff] [blame] | 1175 | ret = co_getblk(sc_oc(sc), buf, framesz, 4); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1176 | } |
| 1177 | if (ret <= 0) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1178 | if (ret == 0) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1179 | return 1; /* retry */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1180 | } |
| 1181 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1182 | return -1; /* error */ |
| 1183 | } |
| 1184 | return framesz; |
| 1185 | } |
| 1186 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1187 | /******************************************************************** |
| 1188 | * Functions that manage the SPOE applet |
| 1189 | ********************************************************************/ |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1190 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1191 | spoe_wakeup_appctx(struct appctx *appctx) |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1192 | { |
Willy Tarreau | 75a8f8e | 2022-05-25 18:12:11 +0200 | [diff] [blame] | 1193 | applet_will_consume(appctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 1194 | applet_have_more_data(appctx); |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1195 | appctx_wakeup(appctx); |
| 1196 | return 1; |
| 1197 | } |
| 1198 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1199 | /* Callback function that catches applet timeouts. If a timeout occurred, we set |
| 1200 | * <appctx->st1> flag and the SPOE applet is woken up. */ |
| 1201 | static struct task * |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1202 | spoe_process_appctx(struct task * task, void *context, unsigned int state) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1203 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 1204 | struct appctx *appctx = context; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1205 | |
| 1206 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1207 | if (tick_is_expired(task->expire, now_ms)) { |
| 1208 | task->expire = TICK_ETERNITY; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1209 | appctx->st1 = SPOE_APPCTX_ERR_TOUT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1210 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1211 | spoe_wakeup_appctx(appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1212 | return task; |
| 1213 | } |
| 1214 | |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 1215 | static int |
| 1216 | spoe_init_appctx(struct appctx *appctx) |
| 1217 | { |
| 1218 | struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx); |
| 1219 | struct spoe_agent *agent = spoe_appctx->agent; |
| 1220 | struct task *task; |
| 1221 | struct stream *s; |
| 1222 | |
| 1223 | if ((task = task_new_here()) == NULL) |
Christopher Faulet | fa463af | 2022-05-18 07:42:49 +0200 | [diff] [blame] | 1224 | goto out_error; |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 1225 | task->process = spoe_process_appctx; |
| 1226 | task->context = appctx; |
| 1227 | |
| 1228 | 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] | 1229 | goto out_free_task; |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 1230 | |
| 1231 | spoe_appctx->owner = appctx; |
| 1232 | spoe_appctx->task = task; |
| 1233 | |
| 1234 | LIST_INIT(&spoe_appctx->buffer_wait.list); |
| 1235 | spoe_appctx->buffer_wait.target = appctx; |
| 1236 | spoe_appctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx; |
| 1237 | |
| 1238 | s = appctx_strm(appctx); |
| 1239 | stream_set_backend(s, agent->b.be); |
| 1240 | |
| 1241 | /* applet is waiting for data */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 1242 | applet_need_more_data(appctx); |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 1243 | |
| 1244 | s->do_log = NULL; |
Christopher Faulet | 9a790f6 | 2023-03-16 14:40:03 +0100 | [diff] [blame] | 1245 | s->scb->flags |= SC_FL_RCV_ONCE; |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 1246 | |
| 1247 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
| 1248 | LIST_APPEND(&agent->rt[tid].applets, &spoe_appctx->list); |
| 1249 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
| 1250 | _HA_ATOMIC_INC(&agent->counters.applets); |
| 1251 | |
| 1252 | appctx->st0 = SPOE_APPCTX_ST_CONNECT; |
| 1253 | task_wakeup(spoe_appctx->task, TASK_WOKEN_INIT); |
| 1254 | return 0; |
| 1255 | out_free_task: |
| 1256 | task_destroy(task); |
| 1257 | out_error: |
| 1258 | return -1; |
| 1259 | } |
| 1260 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1261 | /* Callback function that releases a SPOE applet. This happens when the |
| 1262 | * connection with the agent is closed. */ |
| 1263 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1264 | spoe_release_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1265 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1266 | struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx); |
| 1267 | struct spoe_agent *agent; |
| 1268 | struct spoe_context *ctx, *back; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1269 | |
| 1270 | if (spoe_appctx == NULL) |
| 1271 | return; |
| 1272 | |
Willy Tarreau | 23a2407 | 2022-05-05 20:18:44 +0200 | [diff] [blame] | 1273 | appctx->svcctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1274 | agent = spoe_appctx->agent; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1275 | |
| 1276 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 1277 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1278 | __FUNCTION__, appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1279 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1280 | /* Remove applet from the list of running applets */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1281 | _HA_ATOMIC_DEC(&agent->counters.applets); |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1282 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1283 | if (!LIST_ISEMPTY(&spoe_appctx->list)) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1284 | LIST_DELETE(&spoe_appctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1285 | LIST_INIT(&spoe_appctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1286 | } |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1287 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1288 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1289 | /* Shutdown the server connection, if needed */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1290 | if (appctx->st0 != SPOE_APPCTX_ST_END) { |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1291 | if (appctx->st0 == SPOE_APPCTX_ST_IDLE) { |
| 1292 | eb32_delete(&spoe_appctx->node); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1293 | _HA_ATOMIC_DEC(&agent->counters.idles); |
Christopher Faulet | e99c439 | 2023-04-26 15:56:59 +0200 | [diff] [blame] | 1294 | agent->rt[tid].idles--; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1295 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1296 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1297 | appctx->st0 = SPOE_APPCTX_ST_END; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1298 | if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE) |
| 1299 | spoe_appctx->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1300 | } |
| 1301 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1302 | /* Destroy the task attached to this applet */ |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 1303 | task_destroy(spoe_appctx->task); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1304 | |
Christopher Faulet | 42a0662 | 2022-08-25 18:50:18 +0200 | [diff] [blame] | 1305 | /* Report an error to all streams in the appctx waiting queue */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1306 | list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1307 | LIST_DELETE(&ctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1308 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1309 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Willy Tarreau | aaebcae | 2023-04-27 11:54:11 +0200 | [diff] [blame] | 1310 | spoe_update_stat_time(&ctx->stats.wait_ts, &ctx->stats.t_waiting); |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1311 | ctx->spoe_appctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1312 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1313 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1314 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1315 | } |
| 1316 | |
Christopher Faulet | 42a0662 | 2022-08-25 18:50:18 +0200 | [diff] [blame] | 1317 | /* If the applet was processing a fragmented frame, report an error to |
| 1318 | * the corresponding stream. */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1319 | if (spoe_appctx->frag_ctx.ctx) { |
| 1320 | ctx = spoe_appctx->frag_ctx.ctx; |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1321 | ctx->spoe_appctx = NULL; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1322 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1323 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1324 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1325 | } |
| 1326 | |
Christopher Faulet | 42a0662 | 2022-08-25 18:50:18 +0200 | [diff] [blame] | 1327 | if (!LIST_ISEMPTY(&agent->rt[tid].applets)) { |
| 1328 | /* If there are still some running applets, remove reference on |
| 1329 | * the current one from streams in the async waiting queue. In |
| 1330 | * async mode, the ACK may be received from another appctx. |
| 1331 | */ |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1332 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) { |
| 1333 | if (ctx->spoe_appctx == spoe_appctx) |
| 1334 | ctx->spoe_appctx = NULL; |
| 1335 | } |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1336 | goto end; |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1337 | } |
Christopher Faulet | 6f1296b | 2021-08-02 17:53:56 +0200 | [diff] [blame] | 1338 | else { |
Christopher Faulet | 42a0662 | 2022-08-25 18:50:18 +0200 | [diff] [blame] | 1339 | /* It is the last running applet and the sending and async |
| 1340 | * waiting queues are not empty. So try to start a new applet if |
| 1341 | * HAproxy is not stopping. On success, we remove reference on |
| 1342 | * the current appctx from streams in the async waiting queue. |
| 1343 | * In async mode, the ACK may be received from another appctx. |
Christopher Faulet | 6f1296b | 2021-08-02 17:53:56 +0200 | [diff] [blame] | 1344 | */ |
| 1345 | if (!stopping && |
| 1346 | (!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] | 1347 | spoe_create_appctx(agent->spoe_conf)) { |
| 1348 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) { |
| 1349 | if (ctx->spoe_appctx == spoe_appctx) |
| 1350 | ctx->spoe_appctx = NULL; |
| 1351 | } |
Christopher Faulet | 6f1296b | 2021-08-02 17:53:56 +0200 | [diff] [blame] | 1352 | goto end; |
Christopher Faulet | 42a0662 | 2022-08-25 18:50:18 +0200 | [diff] [blame] | 1353 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1354 | |
Christopher Faulet | 42a0662 | 2022-08-25 18:50:18 +0200 | [diff] [blame] | 1355 | /* Otherwise, report an error to all streams in the sending and |
| 1356 | * async waiting queues. |
| 1357 | */ |
Christopher Faulet | 6f1296b | 2021-08-02 17:53:56 +0200 | [diff] [blame] | 1358 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) { |
| 1359 | LIST_DELETE(&ctx->list); |
| 1360 | LIST_INIT(&ctx->list); |
| 1361 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Willy Tarreau | aaebcae | 2023-04-27 11:54:11 +0200 | [diff] [blame] | 1362 | spoe_update_stat_time(&ctx->stats.queue_ts, &ctx->stats.t_queue); |
Christopher Faulet | 6f1296b | 2021-08-02 17:53:56 +0200 | [diff] [blame] | 1363 | ctx->spoe_appctx = NULL; |
| 1364 | ctx->state = SPOE_CTX_ST_ERROR; |
| 1365 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
| 1366 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1367 | } |
| 1368 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) { |
| 1369 | LIST_DELETE(&ctx->list); |
| 1370 | LIST_INIT(&ctx->list); |
| 1371 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Willy Tarreau | aaebcae | 2023-04-27 11:54:11 +0200 | [diff] [blame] | 1372 | spoe_update_stat_time(&ctx->stats.wait_ts, &ctx->stats.t_waiting); |
Christopher Faulet | 6f1296b | 2021-08-02 17:53:56 +0200 | [diff] [blame] | 1373 | ctx->spoe_appctx = NULL; |
| 1374 | ctx->state = SPOE_CTX_ST_ERROR; |
| 1375 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
| 1376 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1377 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1378 | } |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1379 | |
| 1380 | end: |
Christopher Faulet | 55ae8a6 | 2019-05-23 22:47:48 +0200 | [diff] [blame] | 1381 | /* Release allocated memory */ |
| 1382 | spoe_release_buffer(&spoe_appctx->buffer, |
| 1383 | &spoe_appctx->buffer_wait); |
| 1384 | pool_free(pool_head_spoe_appctx, spoe_appctx); |
| 1385 | |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1386 | /* Update runtinme agent info */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1387 | agent->rt[tid].frame_size = agent->max_frame_size; |
| 1388 | list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list) |
| 1389 | 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] | 1390 | } |
| 1391 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1392 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1393 | spoe_handle_connect_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1394 | { |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 1395 | struct stconn *sc = appctx_sc(appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1396 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1397 | char *frame, *buf; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1398 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1399 | |
Christopher Faulet | d550d26 | 2023-03-31 11:04:34 +0200 | [diff] [blame] | 1400 | /* if the connection is not established, inform the stream that we want |
| 1401 | * to be notified whenever the connection completes. |
| 1402 | */ |
| 1403 | if (sc_opposite(sc)->state < SC_ST_EST) { |
| 1404 | applet_need_more_data(appctx); |
| 1405 | se_need_remote_conn(appctx->sedesc); |
| 1406 | applet_have_more_data(appctx); |
| 1407 | goto stop; |
| 1408 | } |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1409 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1410 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1411 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1412 | " - Connection timed out\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 1413 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1414 | __FUNCTION__, appctx); |
| 1415 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1416 | goto exit; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1417 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1418 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1419 | if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1420 | SPOE_APPCTX(appctx)->task->expire = |
| 1421 | tick_add_ifset(now_ms, agent->timeout.hello); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1422 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1423 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1424 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1425 | buf = trash.area; frame = buf+4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1426 | ret = spoe_prepare_hahello_frame(appctx, frame, |
| 1427 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1428 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1429 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1430 | |
| 1431 | switch (ret) { |
| 1432 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1433 | case 0: /* ignore => an error, cannot be ignored */ |
| 1434 | goto exit; |
| 1435 | |
| 1436 | case 1: /* retry later */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1437 | goto stop; |
| 1438 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1439 | default: |
| 1440 | /* HELLO frame successfully sent, now wait for the |
| 1441 | * reply. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1442 | appctx->st0 = SPOE_APPCTX_ST_CONNECTING; |
| 1443 | goto next; |
| 1444 | } |
| 1445 | |
| 1446 | next: |
| 1447 | return 0; |
| 1448 | stop: |
| 1449 | return 1; |
| 1450 | exit: |
| 1451 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1452 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1453 | } |
| 1454 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1455 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1456 | spoe_handle_connecting_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1457 | { |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 1458 | struct stconn *sc = appctx_sc(appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1459 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1460 | char *frame; |
| 1461 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1462 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1463 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1464 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1465 | " - Connection timed out\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 1466 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1467 | __FUNCTION__, appctx); |
| 1468 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1469 | goto exit; |
| 1470 | } |
| 1471 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1472 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1473 | ret = spoe_recv_frame(appctx, frame, |
| 1474 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1475 | if (ret > 1) { |
| 1476 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1477 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1478 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1479 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1480 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1481 | ret = spoe_handle_agenthello_frame(appctx, frame, ret); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1482 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1483 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1484 | switch (ret) { |
| 1485 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1486 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1487 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1488 | goto next; |
| 1489 | |
| 1490 | case 1: /* retry later */ |
| 1491 | goto stop; |
| 1492 | |
| 1493 | default: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1494 | _HA_ATOMIC_INC(&agent->counters.idles); |
Christopher Faulet | e99c439 | 2023-04-26 15:56:59 +0200 | [diff] [blame] | 1495 | agent->rt[tid].idles++; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1496 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1497 | SPOE_APPCTX(appctx)->node.key = 0; |
| 1498 | eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node); |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1499 | |
| 1500 | /* Update runtinme agent info */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1501 | 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] | 1502 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1503 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1504 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1505 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1506 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1507 | if (trash.data) |
Willy Tarreau | 9002a2e | 2022-05-27 10:34:25 +0200 | [diff] [blame] | 1508 | co_skip(sc_oc(sc), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1509 | |
| 1510 | SPOE_APPCTX(appctx)->task->expire = |
| 1511 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1512 | return 0; |
| 1513 | stop: |
| 1514 | return 1; |
| 1515 | exit: |
| 1516 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1517 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1518 | } |
| 1519 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1520 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1521 | static int |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1522 | spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1523 | { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1524 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
| 1525 | struct spoe_context *ctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1526 | char *frame, *buf; |
| 1527 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1528 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1529 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1530 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1531 | buf = trash.area; frame = buf+4; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1532 | |
| 1533 | if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) { |
| 1534 | ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx; |
| 1535 | ret = spoe_prepare_hafrag_frame(appctx, ctx, frame, |
| 1536 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1537 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1538 | else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1539 | *skip = 1; |
| 1540 | ret = 1; |
| 1541 | goto end; |
| 1542 | } |
| 1543 | else { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1544 | ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1545 | ret = spoe_prepare_hanotify_frame(appctx, ctx, frame, |
| 1546 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1547 | |
| 1548 | } |
| 1549 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1550 | if (ret > 1) |
| 1551 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1552 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1553 | switch (ret) { |
| 1554 | case -1: /* error */ |
| 1555 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1556 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1557 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1558 | case 0: /* ignore */ |
| 1559 | if (ctx == NULL) |
| 1560 | goto abort_frag_frame; |
| 1561 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1562 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1563 | LIST_DELETE(&ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1564 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1565 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Willy Tarreau | aaebcae | 2023-04-27 11:54:11 +0200 | [diff] [blame] | 1566 | spoe_update_stat_time(&ctx->stats.queue_ts, &ctx->stats.t_queue); |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1567 | ctx->spoe_appctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1568 | ctx->state = SPOE_CTX_ST_ERROR; |
| 1569 | ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100); |
| 1570 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1571 | *skip = 1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1572 | break; |
| 1573 | |
| 1574 | case 1: /* retry */ |
| 1575 | *skip = 1; |
| 1576 | break; |
| 1577 | |
| 1578 | default: |
| 1579 | if (ctx == NULL) |
| 1580 | goto abort_frag_frame; |
| 1581 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1582 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1583 | LIST_DELETE(&ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1584 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1585 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Willy Tarreau | aaebcae | 2023-04-27 11:54:11 +0200 | [diff] [blame] | 1586 | spoe_update_stat_time(&ctx->stats.queue_ts, &ctx->stats.t_queue); |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1587 | ctx->spoe_appctx = SPOE_APPCTX(appctx); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1588 | if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) || |
| 1589 | (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN)) |
| 1590 | goto no_frag_frame_sent; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1591 | else |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1592 | goto frag_frame_sent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1593 | } |
| 1594 | goto end; |
| 1595 | |
| 1596 | frag_frame_sent: |
| 1597 | appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1598 | *skip = 1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1599 | SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx; |
| 1600 | SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id; |
| 1601 | SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1602 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
| 1603 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1604 | goto end; |
| 1605 | |
| 1606 | no_frag_frame_sent: |
| 1607 | if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) { |
| 1608 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1609 | LIST_APPEND(&agent->rt[tid].waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1610 | } |
| 1611 | else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) { |
| 1612 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1613 | LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1614 | } |
| 1615 | else { |
| 1616 | appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1617 | *skip = 1; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1618 | LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1619 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1620 | _HA_ATOMIC_INC(&agent->counters.nb_waiting); |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 1621 | ctx->stats.wait_ts = now_ns; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1622 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1623 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1624 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1625 | SPOE_APPCTX(appctx)->cur_fpa++; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1626 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1627 | ctx->state = SPOE_CTX_ST_WAITING_ACK; |
| 1628 | goto end; |
| 1629 | |
| 1630 | abort_frag_frame: |
| 1631 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1632 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1633 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1634 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1635 | goto end; |
| 1636 | |
| 1637 | end: |
| 1638 | return ret; |
| 1639 | } |
| 1640 | |
| 1641 | static int |
| 1642 | spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip) |
| 1643 | { |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 1644 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1645 | struct spoe_context *ctx = NULL; |
| 1646 | char *frame; |
| 1647 | int ret; |
| 1648 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1649 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1650 | ret = spoe_recv_frame(appctx, frame, |
| 1651 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1652 | if (ret > 1) { |
| 1653 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1654 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1655 | ret = -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1656 | goto end; |
| 1657 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1658 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1659 | ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret); |
| 1660 | } |
| 1661 | switch (ret) { |
| 1662 | case -1: /* error */ |
| 1663 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1664 | break; |
| 1665 | |
| 1666 | case 0: /* ignore */ |
| 1667 | break; |
| 1668 | |
| 1669 | case 1: /* retry */ |
| 1670 | *skip = 1; |
| 1671 | break; |
| 1672 | |
| 1673 | default: |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1674 | LIST_DELETE(&ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1675 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1676 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Willy Tarreau | aaebcae | 2023-04-27 11:54:11 +0200 | [diff] [blame] | 1677 | spoe_update_stat_time(&ctx->stats.wait_ts, &ctx->stats.t_waiting); |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 1678 | ctx->stats.response_ts = now_ns; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1679 | if (ctx->spoe_appctx) { |
| 1680 | ctx->spoe_appctx->cur_fpa--; |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1681 | ctx->spoe_appctx = NULL; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1682 | } |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1683 | if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY && |
| 1684 | ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) { |
| 1685 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1686 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1687 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1688 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1689 | } |
| 1690 | else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) |
| 1691 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1692 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1693 | break; |
| 1694 | } |
| 1695 | |
| 1696 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1697 | if (trash.data) |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 1698 | co_skip(sc_oc(appctx_sc(appctx)), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1699 | end: |
| 1700 | return ret; |
| 1701 | } |
| 1702 | |
| 1703 | static int |
| 1704 | spoe_handle_processing_appctx(struct appctx *appctx) |
| 1705 | { |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 1706 | struct stconn *sc = appctx_sc(appctx); |
Willy Tarreau | 9002a2e | 2022-05-27 10:34:25 +0200 | [diff] [blame] | 1707 | struct server *srv = objt_server(__sc_strm(sc)->target); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1708 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1709 | 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] | 1710 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1711 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
| 1712 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
| 1713 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1714 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1715 | goto next; |
| 1716 | } |
| 1717 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1718 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1719 | " - 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] | 1720 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1721 | __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa, |
| 1722 | agent->max_fpa, spoe_appctx_state_str[appctx->st0], |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1723 | SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1724 | |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1725 | |
| 1726 | /* Close the applet ASAP because some sessions are waiting for a free |
| 1727 | * connection slot. It is only an issue in multithreaded mode. |
| 1728 | */ |
| 1729 | close_asap = (global.nbthread > 1 && |
| 1730 | (agent->b.be->queue.length || |
| 1731 | (srv && (srv->queue.length || (srv->maxconn && srv->served >= srv_dynamic_maxconn(srv)))))); |
| 1732 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1733 | /* receiving_frame loop */ |
| 1734 | while (!skip_receiving) { |
| 1735 | ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving); |
| 1736 | switch (ret) { |
| 1737 | case -1: /* error */ |
| 1738 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1739 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1740 | case 0: /* ignore */ |
| 1741 | active_r = 1; |
| 1742 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1743 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1744 | case 1: /* retry */ |
| 1745 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1746 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1747 | default: |
| 1748 | active_r = 1; |
| 1749 | break; |
| 1750 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1751 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1752 | |
Christopher Faulet | d4349b8 | 2023-06-05 08:15:59 +0200 | [diff] [blame] | 1753 | /* Don"t try to send new frame we are waiting for at lease a ack, in |
| 1754 | * sync mode or if applet must be closed ASAP |
| 1755 | */ |
| 1756 | if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK || (close_asap && SPOE_APPCTX(appctx)->cur_fpa)) |
| 1757 | skip_sending = 1; |
| 1758 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1759 | /* send_frame loop */ |
| 1760 | while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) { |
| 1761 | ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending); |
| 1762 | switch (ret) { |
| 1763 | case -1: /* error */ |
| 1764 | goto next; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1765 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1766 | case 0: /* ignore */ |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1767 | if (SPOE_APPCTX(appctx)->node.key) |
| 1768 | SPOE_APPCTX(appctx)->node.key--; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1769 | active_s++; |
| 1770 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1771 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1772 | case 1: /* retry */ |
| 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 | default: |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1776 | if (SPOE_APPCTX(appctx)->node.key) |
| 1777 | SPOE_APPCTX(appctx)->node.key--; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1778 | active_s++; |
| 1779 | break; |
| 1780 | } |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1781 | |
| 1782 | /* if applet must be close ASAP, don't send more than a frame */ |
| 1783 | if (close_asap) |
| 1784 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1785 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1786 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1787 | if (active_s || active_r) { |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1788 | update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s); |
| 1789 | SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle); |
| 1790 | } |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1791 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1792 | 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] | 1793 | /* If applet must be closed, don't switch it in IDLE state and |
| 1794 | * close it when the last waiting frame is acknowledged. |
Christopher Faulet | 9e647e5 | 2021-03-01 15:01:14 +0100 | [diff] [blame] | 1795 | */ |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1796 | if (close_asap) { |
| 1797 | if (SPOE_APPCTX(appctx)->cur_fpa) |
| 1798 | goto out; |
Christopher Faulet | 9e647e5 | 2021-03-01 15:01:14 +0100 | [diff] [blame] | 1799 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
| 1800 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1801 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1802 | goto next; |
| 1803 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1804 | _HA_ATOMIC_INC(&agent->counters.idles); |
Christopher Faulet | e99c439 | 2023-04-26 15:56:59 +0200 | [diff] [blame] | 1805 | agent->rt[tid].idles++; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1806 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1807 | eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1808 | } |
Christopher Faulet | d7da3dd | 2021-08-02 18:13:27 +0200 | [diff] [blame] | 1809 | |
| 1810 | out: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1811 | return 1; |
| 1812 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1813 | next: |
| 1814 | SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle); |
| 1815 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1819 | spoe_handle_disconnect_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1820 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1821 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1822 | char *frame, *buf; |
| 1823 | int ret; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1824 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1825 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) |
| 1826 | goto exit; |
| 1827 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1828 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1829 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1830 | buf = trash.area; frame = buf+4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1831 | ret = spoe_prepare_hadiscon_frame(appctx, frame, |
| 1832 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1833 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1834 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1835 | |
| 1836 | switch (ret) { |
| 1837 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1838 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1839 | goto exit; |
| 1840 | |
| 1841 | case 1: /* retry */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1842 | goto stop; |
| 1843 | |
| 1844 | default: |
| 1845 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1846 | " - disconnected by HAProxy (%d): %s\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 1847 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1848 | __FUNCTION__, appctx, |
| 1849 | SPOE_APPCTX(appctx)->status_code, |
| 1850 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1851 | |
| 1852 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1853 | goto next; |
| 1854 | } |
| 1855 | |
| 1856 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1857 | SPOE_APPCTX(appctx)->task->expire = |
| 1858 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1859 | return 0; |
| 1860 | stop: |
| 1861 | return 1; |
| 1862 | exit: |
| 1863 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1864 | return 0; |
| 1865 | } |
| 1866 | |
| 1867 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1868 | spoe_handle_disconnecting_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1869 | { |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 1870 | struct stconn *sc = appctx_sc(appctx); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1871 | char *frame; |
| 1872 | int ret; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1873 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1874 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1875 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1876 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1877 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1878 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1879 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1880 | ret = spoe_recv_frame(appctx, frame, |
| 1881 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1882 | if (ret > 1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1883 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1884 | ret = spoe_handle_agentdiscon_frame(appctx, frame, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | switch (ret) { |
| 1888 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1889 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1890 | " - error on frame (%s)\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 1891 | (int)date.tv_sec, (int)date.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1892 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1893 | __FUNCTION__, appctx, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1894 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1895 | goto exit; |
| 1896 | |
| 1897 | case 0: /* ignore */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1898 | goto next; |
| 1899 | |
| 1900 | case 1: /* retry */ |
| 1901 | goto stop; |
| 1902 | |
| 1903 | default: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1904 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1905 | " - disconnected by peer (%d): %.*s\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 1906 | (int)date.tv_sec, (int)date.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1907 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1908 | __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code, |
| 1909 | SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1910 | goto exit; |
| 1911 | } |
| 1912 | |
| 1913 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1914 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1915 | if (trash.data) |
Willy Tarreau | 9002a2e | 2022-05-27 10:34:25 +0200 | [diff] [blame] | 1916 | co_skip(sc_oc(sc), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1917 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1918 | return 0; |
| 1919 | stop: |
| 1920 | return 1; |
| 1921 | exit: |
| 1922 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1923 | return 0; |
| 1924 | } |
| 1925 | |
| 1926 | /* I/O Handler processing messages exchanged with the agent */ |
| 1927 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1928 | spoe_handle_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1929 | { |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 1930 | struct stconn *sc = appctx_sc(appctx); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1931 | struct spoe_agent *agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1932 | |
| 1933 | if (SPOE_APPCTX(appctx) == NULL) |
| 1934 | return; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1935 | |
Christopher Faulet | d550d26 | 2023-03-31 11:04:34 +0200 | [diff] [blame] | 1936 | if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) { |
| 1937 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
| 1938 | goto out; |
| 1939 | } |
| 1940 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1941 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
| 1942 | agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1943 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1944 | switchstate: |
| 1945 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1946 | " - appctx-state=%s\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 1947 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1948 | __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]); |
| 1949 | |
| 1950 | switch (appctx->st0) { |
| 1951 | case SPOE_APPCTX_ST_CONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1952 | if (spoe_handle_connect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1953 | goto out; |
| 1954 | goto switchstate; |
| 1955 | |
| 1956 | case SPOE_APPCTX_ST_CONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1957 | if (spoe_handle_connecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1958 | goto out; |
| 1959 | goto switchstate; |
| 1960 | |
| 1961 | case SPOE_APPCTX_ST_IDLE: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1962 | _HA_ATOMIC_DEC(&agent->counters.idles); |
Christopher Faulet | e99c439 | 2023-04-26 15:56:59 +0200 | [diff] [blame] | 1963 | agent->rt[tid].idles--; |
Christopher Faulet | 7d9f1ba | 2018-02-28 13:33:26 +0100 | [diff] [blame] | 1964 | eb32_delete(&SPOE_APPCTX(appctx)->node); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1965 | if (stopping && |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1966 | LIST_ISEMPTY(&agent->rt[tid].sending_queue) && |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1967 | LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1968 | SPOE_APPCTX(appctx)->task->expire = |
| 1969 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1970 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1971 | goto switchstate; |
| 1972 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1973 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Willy Tarreau | 3064f52 | 2022-11-14 07:22:14 +0100 | [diff] [blame] | 1974 | __fallthrough; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1975 | |
| 1976 | case SPOE_APPCTX_ST_PROCESSING: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1977 | case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY: |
| 1978 | case SPOE_APPCTX_ST_WAITING_SYNC_ACK: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1979 | if (spoe_handle_processing_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1980 | goto out; |
| 1981 | goto switchstate; |
| 1982 | |
| 1983 | case SPOE_APPCTX_ST_DISCONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1984 | if (spoe_handle_disconnect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1985 | goto out; |
| 1986 | goto switchstate; |
| 1987 | |
| 1988 | case SPOE_APPCTX_ST_DISCONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1989 | if (spoe_handle_disconnecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1990 | goto out; |
| 1991 | goto switchstate; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1992 | |
| 1993 | case SPOE_APPCTX_ST_EXIT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1994 | appctx->st0 = SPOE_APPCTX_ST_END; |
| 1995 | SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY; |
Christopher Faulet | d550d26 | 2023-03-31 11:04:34 +0200 | [diff] [blame] | 1996 | se_fl_set(appctx->sedesc, SE_FL_EOS); |
| 1997 | if (SPOE_APPCTX(appctx)->status_code != SPOE_FRM_ERR_NONE) |
| 1998 | se_fl_set(appctx->sedesc, SE_FL_ERROR); |
| 1999 | else |
| 2000 | se_fl_set(appctx->sedesc, SE_FL_EOI); |
Willy Tarreau | 3064f52 | 2022-11-14 07:22:14 +0100 | [diff] [blame] | 2001 | __fallthrough; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2002 | |
| 2003 | case SPOE_APPCTX_ST_END: |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2004 | return; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2005 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2006 | out: |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2007 | if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY) |
| 2008 | task_queue(SPOE_APPCTX(appctx)->task); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | struct applet spoe_applet = { |
| 2012 | .obj_type = OBJ_TYPE_APPLET, |
| 2013 | .name = "<SPOE>", /* used for logging */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2014 | .fct = spoe_handle_appctx, |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 2015 | .init = spoe_init_appctx, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2016 | .release = spoe_release_appctx, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2017 | }; |
| 2018 | |
| 2019 | /* Create a SPOE applet. On success, the created applet is returned, else |
| 2020 | * NULL. */ |
| 2021 | static struct appctx * |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2022 | spoe_create_appctx(struct spoe_config *conf) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2023 | { |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 2024 | struct spoe_appctx *spoe_appctx; |
| 2025 | struct appctx *appctx; |
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 | spoe_appctx = pool_zalloc(pool_head_spoe_appctx); |
| 2028 | if (spoe_appctx == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2029 | goto out_error; |
| 2030 | |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 2031 | spoe_appctx->agent = conf->agent; |
| 2032 | spoe_appctx->version = 0; |
| 2033 | spoe_appctx->max_frame_size = conf->agent->max_frame_size; |
| 2034 | spoe_appctx->flags = 0; |
| 2035 | spoe_appctx->status_code = SPOE_FRM_ERR_NONE; |
| 2036 | spoe_appctx->buffer = BUF_NULL; |
| 2037 | spoe_appctx->cur_fpa = 0; |
| 2038 | LIST_INIT(&spoe_appctx->list); |
| 2039 | LIST_INIT(&spoe_appctx->waiting_queue); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2040 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2041 | |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 2042 | if ((appctx = appctx_new_here(&spoe_applet, NULL)) == NULL) |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 2043 | goto out_free_spoe_appctx; |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2044 | |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 2045 | appctx->svcctx = spoe_appctx; |
| 2046 | if (appctx_init(appctx) == -1) |
| 2047 | goto out_free_appctx; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2048 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2049 | appctx_wakeup(appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2050 | return appctx; |
| 2051 | |
| 2052 | /* Error unrolling */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2053 | out_free_appctx: |
Christopher Faulet | 69ebc30 | 2022-05-12 15:28:51 +0200 | [diff] [blame] | 2054 | appctx_free_on_early_error(appctx); |
| 2055 | out_free_spoe_appctx: |
| 2056 | pool_free(pool_head_spoe_appctx, spoe_appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2057 | out_error: |
| 2058 | return NULL; |
| 2059 | } |
| 2060 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2061 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2062 | spoe_queue_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2063 | { |
| 2064 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2065 | struct spoe_agent *agent = conf->agent; |
| 2066 | struct appctx *appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2067 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2068 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2069 | /* Check if we need to create a new SPOE applet or not. */ |
Christopher Faulet | e99c439 | 2023-04-26 15:56:59 +0200 | [diff] [blame] | 2070 | if (agent->rt[tid].processing < agent->rt[tid].idles || |
| 2071 | 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] | 2072 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2073 | |
| 2074 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2075 | " - try to create new SPOE appctx\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 2076 | (int)date.tv_sec, (int)date.tv_usec, agent->id, __FUNCTION__, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2077 | ctx->strm); |
| 2078 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2079 | /* Do not try to create a new applet if there is no server up for the |
| 2080 | * agent's backend. */ |
| 2081 | if (!agent->b.be->srv_act && !agent->b.be->srv_bck) { |
| 2082 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2083 | " - cannot create SPOE appctx: no server up\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 2084 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2085 | __FUNCTION__, ctx->strm); |
| 2086 | goto end; |
| 2087 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2088 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2089 | /* Do not try to create a new applet if we have reached the maximum of |
| 2090 | * connection per seconds */ |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 2091 | if (agent->cps_max > 0) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2092 | 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] | 2093 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2094 | " - cannot create SPOE appctx: max CPS reached\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 2095 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2096 | __FUNCTION__, ctx->strm); |
| 2097 | goto end; |
| 2098 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2099 | } |
| 2100 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2101 | appctx = spoe_create_appctx(conf); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2102 | if (appctx == NULL) { |
| 2103 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2104 | " - failed to create SPOE appctx\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 2105 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2106 | __FUNCTION__, ctx->strm); |
Christopher Faulet | 3b8e349 | 2018-03-26 17:20:58 +0200 | [diff] [blame] | 2107 | send_log(&conf->agent_fe, LOG_EMERG, |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2108 | "SPOE: [%s] failed to create SPOE applet\n", |
| 2109 | agent->id); |
| 2110 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2111 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2112 | } |
| 2113 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2114 | /* Increase the per-process number of cumulated connections */ |
| 2115 | if (agent->cps_max > 0) |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2116 | update_freq_ctr(&agent->rt[tid].conn_per_sec, 1); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2117 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2118 | end: |
| 2119 | /* 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] | 2120 | if (LIST_ISEMPTY(&agent->rt[tid].applets)) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2121 | ctx->status_code = SPOE_CTX_ERR_RES; |
| 2122 | return -1; |
| 2123 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2124 | |
Christopher Faulet | 3e86cec | 2019-04-10 14:02:12 +0200 | [diff] [blame] | 2125 | /* Add the SPOE context in the sending queue if the stream has no applet |
| 2126 | * already assigned and wakeup all idle applets. Otherwise, don't queue |
| 2127 | * it. */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2128 | _HA_ATOMIC_INC(&agent->counters.nb_sending); |
Willy Tarreau | aaebcae | 2023-04-27 11:54:11 +0200 | [diff] [blame] | 2129 | spoe_update_stat_time(&ctx->stats.request_ts, &ctx->stats.t_request); |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 2130 | ctx->stats.queue_ts = now_ns; |
Christopher Faulet | 3e86cec | 2019-04-10 14:02:12 +0200 | [diff] [blame] | 2131 | if (ctx->spoe_appctx) |
| 2132 | return 1; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2133 | LIST_APPEND(&agent->rt[tid].sending_queue, &ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2134 | |
| 2135 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2136 | " - Add stream in sending queue" |
Christopher Faulet | 68db023 | 2018-04-06 11:34:12 +0200 | [diff] [blame] | 2137 | " - applets=%u - idles=%u - processing=%u\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 2138 | (int)date.tv_sec, (int)date.tv_usec, agent->id, __FUNCTION__, |
Christopher Faulet | 68db023 | 2018-04-06 11:34:12 +0200 | [diff] [blame] | 2139 | ctx->strm, agent->counters.applets, agent->counters.idles, |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2140 | agent->rt[tid].processing); |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2141 | |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 2142 | /* Finally try to wakeup an IDLE applet. */ |
| 2143 | if (!eb_is_empty(&agent->rt[tid].idle_applets)) { |
| 2144 | struct eb32_node *node; |
| 2145 | |
| 2146 | node = eb32_first(&agent->rt[tid].idle_applets); |
| 2147 | spoe_appctx = eb32_entry(node, struct spoe_appctx, node); |
| 2148 | if (node && spoe_appctx) { |
| 2149 | eb32_delete(&spoe_appctx->node); |
| 2150 | spoe_appctx->node.key++; |
| 2151 | eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node); |
| 2152 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2153 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2154 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2155 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | /*************************************************************************** |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2159 | * Functions that encode SPOE messages |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2160 | **************************************************************************/ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2161 | /* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle |
| 2162 | * fragmented_content. If the next message can be processed, it returns 0. If |
| 2163 | * the message is too big, it returns -1.*/ |
| 2164 | static int |
| 2165 | spoe_encode_message(struct stream *s, struct spoe_context *ctx, |
| 2166 | struct spoe_message *msg, int dir, |
| 2167 | char **buf, char *end) |
| 2168 | { |
| 2169 | struct sample *smp; |
| 2170 | struct spoe_arg *arg; |
| 2171 | int ret; |
| 2172 | |
| 2173 | if (msg->cond) { |
| 2174 | ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2175 | ret = acl_pass(ret); |
| 2176 | if (msg->cond->pol == ACL_COND_UNLESS) |
| 2177 | ret = !ret; |
| 2178 | |
| 2179 | /* the rule does not match */ |
| 2180 | if (!ret) |
| 2181 | goto next; |
| 2182 | } |
| 2183 | |
| 2184 | /* Resume encoding of a SPOE argument */ |
| 2185 | if (ctx->frag_ctx.curarg != NULL) { |
| 2186 | arg = ctx->frag_ctx.curarg; |
| 2187 | goto encode_argument; |
| 2188 | } |
| 2189 | |
| 2190 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2191 | goto encode_msg_payload; |
| 2192 | |
| 2193 | /* Check if there is enough space for the message name and the |
| 2194 | * number of arguments. It implies <msg->id_len> is encoded on 2 |
| 2195 | * bytes, at most (< 2288). */ |
| 2196 | if (*buf + 2 + msg->id_len + 1 > end) |
| 2197 | goto too_big; |
| 2198 | |
| 2199 | /* Encode the message name */ |
| 2200 | if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1) |
| 2201 | goto too_big; |
| 2202 | |
| 2203 | /* Set the number of arguments for this message */ |
| 2204 | **buf = msg->nargs; |
| 2205 | (*buf)++; |
| 2206 | |
| 2207 | ctx->frag_ctx.curoff = 0; |
| 2208 | encode_msg_payload: |
| 2209 | |
| 2210 | /* Loop on arguments */ |
| 2211 | list_for_each_entry(arg, &msg->args, list) { |
| 2212 | ctx->frag_ctx.curarg = arg; |
| 2213 | ctx->frag_ctx.curoff = UINT_MAX; |
Kevin Zhu | f7f5428 | 2019-04-26 14:00:01 +0800 | [diff] [blame] | 2214 | ctx->frag_ctx.curlen = 0; |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2215 | |
| 2216 | encode_argument: |
| 2217 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2218 | goto encode_arg_value; |
| 2219 | |
Joseph Herlant | f7f6031 | 2018-11-15 13:46:49 -0800 | [diff] [blame] | 2220 | /* Encode the argument name as a string. It can by NULL */ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2221 | if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1) |
| 2222 | goto too_big; |
| 2223 | |
| 2224 | ctx->frag_ctx.curoff = 0; |
| 2225 | encode_arg_value: |
| 2226 | |
Joseph Herlant | f7f6031 | 2018-11-15 13:46:49 -0800 | [diff] [blame] | 2227 | /* Fetch the argument value */ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2228 | 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] | 2229 | if (smp) { |
| 2230 | smp->ctx.a[0] = &ctx->frag_ctx.curlen; |
| 2231 | smp->ctx.a[1] = &ctx->frag_ctx.curoff; |
| 2232 | } |
Christopher Faulet | 85db321 | 2019-04-26 14:30:15 +0200 | [diff] [blame] | 2233 | ret = spoe_encode_data(smp, buf, end); |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2234 | if (ret == -1 || ctx->frag_ctx.curoff) |
| 2235 | goto too_big; |
| 2236 | } |
| 2237 | |
| 2238 | next: |
| 2239 | return 0; |
| 2240 | |
| 2241 | too_big: |
| 2242 | return -1; |
| 2243 | } |
| 2244 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2245 | /* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to |
| 2246 | * handle fragmented content. On success it returns 1. If an error occurred, -1 |
| 2247 | * is returned. If nothing has been encoded, it returns 0 (this is only possible |
| 2248 | * for unfragmented payload). */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2249 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2250 | spoe_encode_messages(struct stream *s, struct spoe_context *ctx, |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2251 | struct list *messages, int dir, int type) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2252 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2253 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2254 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2255 | struct spoe_message *msg; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2256 | char *p, *end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2257 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2258 | p = b_head(&ctx->buffer); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2259 | end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2260 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2261 | if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */ |
| 2262 | /* Resume encoding of a SPOE message */ |
| 2263 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2264 | msg = ctx->frag_ctx.curmsg; |
| 2265 | goto encode_evt_message; |
| 2266 | } |
| 2267 | |
| 2268 | list_for_each_entry(msg, messages, by_evt) { |
| 2269 | ctx->frag_ctx.curmsg = msg; |
| 2270 | ctx->frag_ctx.curarg = NULL; |
| 2271 | ctx->frag_ctx.curoff = UINT_MAX; |
| 2272 | |
| 2273 | encode_evt_message: |
| 2274 | if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1) |
| 2275 | goto too_big; |
| 2276 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2277 | } |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2278 | else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */ |
| 2279 | /* Resume encoding of a SPOE message */ |
| 2280 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2281 | msg = ctx->frag_ctx.curmsg; |
| 2282 | goto encode_grp_message; |
| 2283 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2284 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2285 | list_for_each_entry(msg, messages, by_grp) { |
| 2286 | ctx->frag_ctx.curmsg = msg; |
| 2287 | ctx->frag_ctx.curarg = NULL; |
| 2288 | ctx->frag_ctx.curoff = UINT_MAX; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2289 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2290 | encode_grp_message: |
| 2291 | if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1) |
| 2292 | goto too_big; |
| 2293 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2294 | } |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2295 | else |
| 2296 | goto skip; |
| 2297 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2298 | |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2299 | /* nothing has been encoded for an unfragmented payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2300 | if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer)) |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2301 | goto skip; |
| 2302 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2303 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2304 | " - encode %s messages - spoe_appctx=%p" |
| 2305 | "- max_size=%u - encoded=%ld\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 2306 | (int)date.tv_sec, (int)date.tv_usec, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2307 | agent->id, __FUNCTION__, s, |
| 2308 | ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"), |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2309 | ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE), |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2310 | p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2311 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2312 | b_set_data(&ctx->buffer, p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2313 | ctx->frag_ctx.curmsg = NULL; |
| 2314 | ctx->frag_ctx.curarg = NULL; |
| 2315 | ctx->frag_ctx.curoff = 0; |
| 2316 | ctx->frag_ctx.flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2317 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2318 | return 1; |
| 2319 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2320 | too_big: |
Christopher Faulet | a715ea8 | 2019-04-10 14:21:51 +0200 | [diff] [blame] | 2321 | /* Return an error if fragmentation is unsupported or if nothing has |
| 2322 | * been encoded because its too big and not splittable. */ |
| 2323 | if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) { |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 2324 | ctx->status_code = SPOE_CTX_ERR_TOO_BIG; |
| 2325 | return -1; |
| 2326 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2327 | |
| 2328 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2329 | " - encode fragmented messages - spoe_appctx=%p" |
| 2330 | " - curmsg=%p - curarg=%p - curoff=%u" |
| 2331 | " - max_size=%u - encoded=%ld\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 2332 | (int)date.tv_sec, (int)date.tv_usec, |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2333 | agent->id, __FUNCTION__, s, ctx->spoe_appctx, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2334 | ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff, |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2335 | (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] | 2336 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2337 | b_set_data(&ctx->buffer, p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2338 | ctx->flags |= SPOE_CTX_FL_FRAGMENTED; |
| 2339 | ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN; |
| 2340 | return 1; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2341 | |
| 2342 | skip: |
| 2343 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2344 | " - skip the frame because nothing has been encoded\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 2345 | (int)date.tv_sec, (int)date.tv_usec, |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2346 | agent->id, __FUNCTION__, s); |
| 2347 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2348 | } |
| 2349 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2350 | |
| 2351 | /*************************************************************************** |
| 2352 | * Functions that handle SPOE actions |
| 2353 | **************************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2354 | /* Helper function to set a variable */ |
| 2355 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2356 | 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] | 2357 | struct sample *smp) |
| 2358 | { |
| 2359 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2360 | struct spoe_agent *agent = conf->agent; |
| 2361 | char varname[64]; |
| 2362 | |
| 2363 | memset(varname, 0, sizeof(varname)); |
| 2364 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2365 | scope, agent->var_pfx, len, name); |
Etienne Carriere | aec8989 | 2017-12-14 09:36:40 +0000 | [diff] [blame] | 2366 | if (agent->flags & SPOE_FL_FORCE_SET_VAR) |
| 2367 | vars_set_by_name(varname, len, smp); |
| 2368 | else |
| 2369 | vars_set_by_name_ifexist(varname, len, smp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | /* Helper function to unset a variable */ |
| 2373 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2374 | 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] | 2375 | struct sample *smp) |
| 2376 | { |
| 2377 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2378 | struct spoe_agent *agent = conf->agent; |
| 2379 | char varname[64]; |
| 2380 | |
| 2381 | memset(varname, 0, sizeof(varname)); |
| 2382 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2383 | scope, agent->var_pfx, len, name); |
| 2384 | vars_unset_by_name_ifexist(varname, len, smp); |
| 2385 | } |
| 2386 | |
| 2387 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2388 | static inline int |
| 2389 | spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx, |
| 2390 | char **buf, char *end, int dir) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2391 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2392 | char *str, *scope, *p = *buf; |
| 2393 | struct sample smp; |
| 2394 | uint64_t sz; |
| 2395 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2396 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2397 | if (p + 2 >= end) |
| 2398 | goto skip; |
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 | /* SET-VAR requires 3 arguments */ |
| 2401 | if (*p++ != 3) |
| 2402 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2403 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2404 | switch (*p++) { |
| 2405 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2406 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2407 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2408 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2409 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2410 | default: goto skip; |
| 2411 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2412 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2413 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2414 | goto skip; |
| 2415 | memset(&smp, 0, sizeof(smp)); |
| 2416 | 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] | 2417 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2418 | if (spoe_decode_data(&p, end, &smp) == -1) |
| 2419 | goto skip; |
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 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2422 | " - set-var '%s.%s.%.*s'\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 2423 | (int)date.tv_sec, (int)date.tv_usec, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2424 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2425 | __FUNCTION__, s, scope, |
| 2426 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2427 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2428 | |
Christopher Faulet | 2469eba | 2020-10-15 16:08:30 +0200 | [diff] [blame] | 2429 | if (smp.data.type == SMP_T_ANY) |
| 2430 | spoe_unset_var(ctx, scope, str, sz, &smp); |
| 2431 | else |
| 2432 | spoe_set_var(ctx, scope, str, sz, &smp); |
Christopher Faulet | b5cff60 | 2016-11-24 14:53:22 +0100 | [diff] [blame] | 2433 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2434 | ret = (p - *buf); |
| 2435 | *buf = p; |
| 2436 | return ret; |
| 2437 | skip: |
| 2438 | return 0; |
| 2439 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2440 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2441 | static inline int |
| 2442 | spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx, |
| 2443 | char **buf, char *end, int dir) |
| 2444 | { |
| 2445 | char *str, *scope, *p = *buf; |
| 2446 | struct sample smp; |
| 2447 | uint64_t sz; |
| 2448 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2449 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2450 | if (p + 2 >= end) |
| 2451 | goto skip; |
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 | /* UNSET-VAR requires 2 arguments */ |
| 2454 | if (*p++ != 2) |
| 2455 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2456 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2457 | switch (*p++) { |
| 2458 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2459 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2460 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2461 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2462 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2463 | default: goto skip; |
| 2464 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2465 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2466 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2467 | goto skip; |
| 2468 | memset(&smp, 0, sizeof(smp)); |
| 2469 | 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] | 2470 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2471 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2472 | " - unset-var '%s.%s.%.*s'\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 2473 | (int)date.tv_sec, (int)date.tv_usec, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2474 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2475 | __FUNCTION__, s, scope, |
| 2476 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2477 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2478 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2479 | spoe_unset_var(ctx, scope, str, sz, &smp); |
| 2480 | |
| 2481 | ret = (p - *buf); |
| 2482 | *buf = p; |
| 2483 | return ret; |
| 2484 | skip: |
| 2485 | return 0; |
| 2486 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2487 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2488 | /* Process SPOE actions for a specific event. It returns 1 on success. If an |
| 2489 | * error occurred, 0 is returned. */ |
| 2490 | static int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2491 | spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2492 | { |
| 2493 | char *p, *end; |
| 2494 | int ret; |
| 2495 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2496 | p = b_head(&ctx->buffer); |
| 2497 | end = p + b_data(&ctx->buffer); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2498 | |
| 2499 | while (p < end) { |
| 2500 | enum spoe_action_type type; |
| 2501 | |
| 2502 | type = *p++; |
| 2503 | switch (type) { |
| 2504 | case SPOE_ACT_T_SET_VAR: |
| 2505 | ret = spoe_decode_action_set_var(s, ctx, &p, end, dir); |
| 2506 | if (!ret) |
| 2507 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2508 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2509 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2510 | case SPOE_ACT_T_UNSET_VAR: |
| 2511 | ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir); |
| 2512 | if (!ret) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2513 | goto skip; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2514 | break; |
| 2515 | |
| 2516 | default: |
| 2517 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2518 | } |
| 2519 | } |
| 2520 | |
| 2521 | return 1; |
| 2522 | skip: |
| 2523 | return 0; |
| 2524 | } |
| 2525 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2526 | /*************************************************************************** |
| 2527 | * Functions that process SPOE events |
| 2528 | **************************************************************************/ |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2529 | static void |
| 2530 | spoe_update_stats(struct stream *s, struct spoe_agent *agent, |
| 2531 | struct spoe_context *ctx, int dir) |
| 2532 | { |
Willy Tarreau | aaebcae | 2023-04-27 11:54:11 +0200 | [diff] [blame] | 2533 | if (ctx->stats.start_ts != 0) { |
| 2534 | spoe_update_stat_time(&ctx->stats.start_ts, &ctx->stats.t_process); |
| 2535 | ctx->stats.t_total += ctx->stats.t_process; |
| 2536 | ctx->stats.request_ts = 0; |
| 2537 | ctx->stats.queue_ts = 0; |
| 2538 | ctx->stats.wait_ts = 0; |
| 2539 | ctx->stats.response_ts = 0; |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | if (agent->var_t_process) { |
| 2543 | struct sample smp; |
| 2544 | |
| 2545 | memset(&smp, 0, sizeof(smp)); |
| 2546 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2547 | smp.data.u.sint = ctx->stats.t_process; |
| 2548 | smp.data.type = SMP_T_SINT; |
| 2549 | |
| 2550 | spoe_set_var(ctx, "txn", agent->var_t_process, |
| 2551 | strlen(agent->var_t_process), &smp); |
| 2552 | } |
| 2553 | |
| 2554 | if (agent->var_t_total) { |
| 2555 | struct sample smp; |
| 2556 | |
| 2557 | memset(&smp, 0, sizeof(smp)); |
| 2558 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2559 | smp.data.u.sint = ctx->stats.t_total; |
| 2560 | smp.data.type = SMP_T_SINT; |
| 2561 | |
| 2562 | spoe_set_var(ctx, "txn", agent->var_t_total, |
| 2563 | strlen(agent->var_t_total), &smp); |
| 2564 | } |
| 2565 | } |
| 2566 | |
| 2567 | static void |
| 2568 | spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent, |
| 2569 | struct spoe_context *ctx, int dir) |
| 2570 | { |
| 2571 | if (agent->eps_max > 0) |
| 2572 | update_freq_ctr(&agent->rt[tid].err_per_sec, 1); |
| 2573 | |
| 2574 | if (agent->var_on_error) { |
| 2575 | struct sample smp; |
| 2576 | |
| 2577 | memset(&smp, 0, sizeof(smp)); |
| 2578 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2579 | smp.data.u.sint = ctx->status_code; |
| 2580 | smp.data.type = SMP_T_BOOL; |
| 2581 | |
| 2582 | spoe_set_var(ctx, "txn", agent->var_on_error, |
| 2583 | strlen(agent->var_on_error), &smp); |
| 2584 | } |
| 2585 | |
| 2586 | ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR) |
| 2587 | ? SPOE_CTX_ST_READY |
| 2588 | : SPOE_CTX_ST_NONE); |
| 2589 | } |
| 2590 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2591 | static inline int |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2592 | 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] | 2593 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2594 | /* If a process is already started for this SPOE context, retry |
| 2595 | * later. */ |
| 2596 | if (ctx->flags & SPOE_CTX_FL_PROCESS) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2597 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2598 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2599 | agent->rt[tid].processing++; |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 2600 | ctx->stats.start_ts = now_ns; |
| 2601 | ctx->stats.request_ts = now_ns; |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2602 | ctx->stats.t_request = -1; |
| 2603 | ctx->stats.t_queue = -1; |
| 2604 | ctx->stats.t_waiting = -1; |
| 2605 | ctx->stats.t_response = -1; |
| 2606 | ctx->stats.t_process = -1; |
| 2607 | |
| 2608 | ctx->status_code = 0; |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2609 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2610 | /* Set the right flag to prevent request and response processing |
| 2611 | * in same time. */ |
| 2612 | ctx->flags |= ((dir == SMP_OPT_DIR_REQ) |
| 2613 | ? SPOE_CTX_FL_REQ_PROCESS |
| 2614 | : SPOE_CTX_FL_RSP_PROCESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2615 | return 1; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2616 | } |
| 2617 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2618 | static inline void |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2619 | spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2620 | { |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2621 | struct spoe_appctx *sa = ctx->spoe_appctx; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2622 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2623 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) |
| 2624 | return; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2625 | _HA_ATOMIC_INC(&agent->counters.nb_processed); |
Christopher Faulet | 879dca9 | 2018-03-23 11:53:24 +0100 | [diff] [blame] | 2626 | if (sa) { |
| 2627 | if (sa->frag_ctx.ctx == ctx) { |
| 2628 | sa->frag_ctx.ctx = NULL; |
| 2629 | spoe_wakeup_appctx(sa->owner); |
| 2630 | } |
| 2631 | else |
| 2632 | sa->cur_fpa--; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2633 | } |
| 2634 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2635 | /* Reset the flag to allow next processing */ |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2636 | agent->rt[tid].processing--; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2637 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2638 | |
| 2639 | /* Reset processing timer */ |
| 2640 | ctx->process_exp = TICK_ETERNITY; |
| 2641 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2642 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2643 | |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2644 | ctx->spoe_appctx = NULL; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2645 | ctx->frag_ctx.curmsg = NULL; |
| 2646 | ctx->frag_ctx.curarg = NULL; |
| 2647 | ctx->frag_ctx.curoff = 0; |
| 2648 | ctx->frag_ctx.flags = 0; |
| 2649 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2650 | if (!LIST_ISEMPTY(&ctx->list)) { |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2651 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2652 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2653 | else |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2654 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2655 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2656 | LIST_DELETE(&ctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2657 | LIST_INIT(&ctx->list); |
| 2658 | } |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2659 | } |
| 2660 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2661 | /* Process a list of SPOE messages. First, this functions will process messages |
| 2662 | * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame |
| 2663 | * to process corresponding actions. During all the processing, it returns 0 |
| 2664 | * and it returns 1 when the processing is finished. If an error occurred, -1 |
| 2665 | * is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2666 | static int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2667 | spoe_process_messages(struct stream *s, struct spoe_context *ctx, |
| 2668 | struct list *messages, int dir, int type) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2669 | { |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2670 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2671 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2672 | int ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2673 | |
| 2674 | if (ctx->state == SPOE_CTX_ST_ERROR) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2675 | goto end; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2676 | |
| 2677 | if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) { |
| 2678 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2679 | " - failed to process messages: timeout\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 2680 | (int)date.tv_sec, (int)date.tv_usec, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2681 | agent->id, __FUNCTION__, s); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2682 | ctx->status_code = SPOE_CTX_ERR_TOUT; |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2683 | goto end; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2684 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2685 | |
| 2686 | if (ctx->state == SPOE_CTX_ST_READY) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2687 | if (agent->eps_max > 0) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2688 | 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] | 2689 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2690 | " - skip processing of messages: max EPS reached\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 2691 | (int)date.tv_sec, (int)date.tv_usec, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2692 | agent->id, __FUNCTION__, s); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2693 | goto skip; |
| 2694 | } |
| 2695 | } |
| 2696 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2697 | if (!tick_isset(ctx->process_exp)) { |
| 2698 | ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing); |
| 2699 | s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire), |
| 2700 | ctx->process_exp); |
| 2701 | } |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2702 | ret = spoe_start_processing(agent, ctx, dir); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2703 | if (!ret) |
| 2704 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2705 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2706 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2707 | /* fall through */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2708 | } |
| 2709 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2710 | if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) { |
Willy Tarreau | aaebcae | 2023-04-27 11:54:11 +0200 | [diff] [blame] | 2711 | if (ctx->stats.request_ts == 0) |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 2712 | ctx->stats.request_ts = now_ns; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2713 | if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait)) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2714 | goto out; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2715 | ret = spoe_encode_messages(s, ctx, messages, dir, type); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2716 | if (ret < 0) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2717 | goto end; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2718 | if (!ret) |
| 2719 | goto skip; |
Christopher Faulet | 333694d | 2018-01-12 10:45:47 +0100 | [diff] [blame] | 2720 | if (spoe_queue_context(ctx) < 0) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2721 | goto end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2722 | ctx->state = SPOE_CTX_ST_SENDING_MSGS; |
| 2723 | } |
| 2724 | |
| 2725 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) { |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2726 | if (ctx->spoe_appctx) |
| 2727 | spoe_wakeup_appctx(ctx->spoe_appctx->owner); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2728 | ret = 0; |
| 2729 | goto out; |
| 2730 | } |
| 2731 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2732 | if (ctx->state == SPOE_CTX_ST_WAITING_ACK) { |
| 2733 | ret = 0; |
| 2734 | goto out; |
| 2735 | } |
| 2736 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2737 | if (ctx->state == SPOE_CTX_ST_DONE) { |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2738 | spoe_process_actions(s, ctx, dir); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2739 | ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2740 | ctx->frame_id++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2741 | ctx->state = SPOE_CTX_ST_READY; |
Willy Tarreau | aaebcae | 2023-04-27 11:54:11 +0200 | [diff] [blame] | 2742 | spoe_update_stat_time(&ctx->stats.response_ts, &ctx->stats.t_response); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2743 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | out: |
| 2747 | return ret; |
| 2748 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2749 | skip: |
Willy Tarreau | aaebcae | 2023-04-27 11:54:11 +0200 | [diff] [blame] | 2750 | ctx->stats.start_ts = 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2751 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2752 | spoe_stop_processing(agent, ctx); |
| 2753 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2754 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2755 | end: |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2756 | spoe_update_stats(s, agent, ctx, dir); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2757 | spoe_stop_processing(agent, ctx); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2758 | if (ctx->status_code) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2759 | _HA_ATOMIC_INC(&agent->counters.nb_errors); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2760 | spoe_handle_processing_error(s, agent, ctx, dir); |
| 2761 | ret = 1; |
| 2762 | } |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2763 | return ret; |
| 2764 | } |
| 2765 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2766 | /* Process a SPOE group, ie the list of messages attached to the group <grp>. |
| 2767 | * See spoe_process_message for details. */ |
| 2768 | static int |
| 2769 | spoe_process_group(struct stream *s, struct spoe_context *ctx, |
| 2770 | struct spoe_group *group, int dir) |
| 2771 | { |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2772 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2773 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2774 | int ret; |
| 2775 | |
| 2776 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2777 | " - ctx-state=%s - Process messages for group=%s\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 2778 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2779 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2780 | group->id); |
| 2781 | |
| 2782 | if (LIST_ISEMPTY(&group->messages)) |
| 2783 | return 1; |
| 2784 | |
| 2785 | 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] | 2786 | if (ret && ctx->stats.t_process != -1) { |
| 2787 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2788 | " - <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] | 2789 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2790 | __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code, |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2791 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2792 | ctx->stats.t_response, ctx->stats.t_process, |
| 2793 | agent->counters.idles, agent->counters.applets, |
| 2794 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2795 | agent->counters.nb_errors, agent->counters.nb_processed, |
| 2796 | agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec)); |
| 2797 | if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM)) |
| 2798 | send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING), |
| 2799 | "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n", |
| 2800 | agent->id, group->id, s->uniq_id, ctx->status_code, |
| 2801 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
| 2802 | ctx->stats.t_response, ctx->stats.t_process, |
| 2803 | agent->counters.idles, agent->counters.applets, |
| 2804 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2805 | agent->counters.nb_errors, agent->counters.nb_processed); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2806 | } |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2807 | return ret; |
| 2808 | } |
| 2809 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2810 | /* Process a SPOE event, ie the list of messages attached to the event <ev>. |
| 2811 | * See spoe_process_message for details. */ |
| 2812 | static int |
| 2813 | spoe_process_event(struct stream *s, struct spoe_context *ctx, |
| 2814 | enum spoe_event ev) |
| 2815 | { |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2816 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2817 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2818 | int dir, ret; |
| 2819 | |
| 2820 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2821 | " - ctx-state=%s - Process messages for event=%s\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 2822 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2823 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2824 | spoe_event_str[ev]); |
| 2825 | |
| 2826 | dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 2827 | |
| 2828 | if (LIST_ISEMPTY(&(ctx->events[ev]))) |
| 2829 | return 1; |
| 2830 | |
| 2831 | 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] | 2832 | if (ret && ctx->stats.t_process != -1) { |
| 2833 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2834 | " - <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] | 2835 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2836 | __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code, |
| 2837 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2838 | ctx->stats.t_response, ctx->stats.t_process, |
| 2839 | agent->counters.idles, agent->counters.applets, |
| 2840 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2841 | agent->counters.nb_errors, agent->counters.nb_processed, |
| 2842 | agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec)); |
| 2843 | if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM)) |
| 2844 | send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING), |
| 2845 | "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n", |
| 2846 | agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code, |
| 2847 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
| 2848 | ctx->stats.t_response, ctx->stats.t_process, |
| 2849 | agent->counters.idles, agent->counters.applets, |
| 2850 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2851 | agent->counters.nb_errors, agent->counters.nb_processed); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2852 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2853 | return ret; |
| 2854 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2855 | |
| 2856 | /*************************************************************************** |
| 2857 | * Functions that create/destroy SPOE contexts |
| 2858 | **************************************************************************/ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2859 | static int |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2860 | spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2861 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2862 | if (buf->size) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2863 | return 1; |
| 2864 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2865 | if (LIST_INLIST(&buffer_wait->list)) |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2866 | LIST_DEL_INIT(&buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2867 | |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 2868 | if (b_alloc(buf)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2869 | return 1; |
| 2870 | |
Willy Tarreau | b4e3476 | 2021-09-30 19:02:18 +0200 | [diff] [blame] | 2871 | LIST_APPEND(&th_ctx->buffer_wq, &buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2872 | return 0; |
| 2873 | } |
| 2874 | |
| 2875 | static void |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2876 | spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2877 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2878 | if (LIST_INLIST(&buffer_wait->list)) |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2879 | LIST_DEL_INIT(&buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2880 | |
| 2881 | /* Release the buffer if needed */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2882 | if (buf->size) { |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2883 | b_free(buf); |
Willy Tarreau | 4d77bbf | 2021-02-20 12:02:46 +0100 | [diff] [blame] | 2884 | offer_buffers(buffer_wait->target, 1); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2885 | } |
| 2886 | } |
| 2887 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2888 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2889 | spoe_wakeup_context(struct spoe_context *ctx) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2890 | { |
| 2891 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 2892 | return 1; |
| 2893 | } |
| 2894 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2895 | static struct spoe_context * |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2896 | spoe_create_context(struct stream *s, struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2897 | { |
| 2898 | struct spoe_config *conf = FLT_CONF(filter); |
| 2899 | struct spoe_context *ctx; |
| 2900 | |
Willy Tarreau | c9ef9bc | 2021-03-22 21:04:50 +0100 | [diff] [blame] | 2901 | ctx = pool_zalloc(pool_head_spoe_ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2902 | if (ctx == NULL) { |
| 2903 | return NULL; |
| 2904 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2905 | ctx->filter = filter; |
| 2906 | ctx->state = SPOE_CTX_ST_NONE; |
| 2907 | ctx->status_code = SPOE_CTX_ERR_NONE; |
| 2908 | ctx->flags = 0; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2909 | ctx->events = conf->agent->events; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 2910 | ctx->groups = &conf->agent->groups; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2911 | ctx->buffer = BUF_NULL; |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2912 | LIST_INIT(&ctx->buffer_wait.list); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2913 | ctx->buffer_wait.target = ctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2914 | ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2915 | LIST_INIT(&ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2916 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2917 | ctx->stream_id = 0; |
| 2918 | ctx->frame_id = 1; |
| 2919 | ctx->process_exp = TICK_ETERNITY; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2920 | |
Willy Tarreau | aaebcae | 2023-04-27 11:54:11 +0200 | [diff] [blame] | 2921 | ctx->stats.start_ts = 0; |
| 2922 | ctx->stats.request_ts = 0; |
| 2923 | ctx->stats.queue_ts = 0; |
| 2924 | ctx->stats.wait_ts = 0; |
| 2925 | ctx->stats.response_ts= 0; |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2926 | ctx->stats.t_request = -1; |
| 2927 | ctx->stats.t_queue = -1; |
| 2928 | ctx->stats.t_waiting = -1; |
| 2929 | ctx->stats.t_response = -1; |
| 2930 | ctx->stats.t_process = -1; |
| 2931 | ctx->stats.t_total = 0; |
| 2932 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2933 | ctx->strm = s; |
| 2934 | ctx->state = SPOE_CTX_ST_READY; |
| 2935 | filter->ctx = ctx; |
| 2936 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2937 | return ctx; |
| 2938 | } |
| 2939 | |
| 2940 | static void |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2941 | spoe_destroy_context(struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2942 | { |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2943 | struct spoe_config *conf = FLT_CONF(filter); |
| 2944 | struct spoe_context *ctx = filter->ctx; |
| 2945 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2946 | if (!ctx) |
| 2947 | return; |
| 2948 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2949 | spoe_stop_processing(conf->agent, ctx); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2950 | pool_free(pool_head_spoe_ctx, ctx); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2951 | filter->ctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2955 | spoe_reset_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2956 | { |
| 2957 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 2958 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2959 | |
Willy Tarreau | aaebcae | 2023-04-27 11:54:11 +0200 | [diff] [blame] | 2960 | ctx->stats.start_ts = 0; |
| 2961 | ctx->stats.request_ts = 0; |
| 2962 | ctx->stats.queue_ts = 0; |
| 2963 | ctx->stats.wait_ts = 0; |
| 2964 | ctx->stats.response_ts= 0; |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2965 | ctx->stats.t_request = -1; |
| 2966 | ctx->stats.t_queue = -1; |
| 2967 | ctx->stats.t_waiting = -1; |
| 2968 | ctx->stats.t_response = -1; |
| 2969 | ctx->stats.t_process = -1; |
| 2970 | ctx->stats.t_total = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | |
| 2974 | /*************************************************************************** |
| 2975 | * Hooks that manage the filter lifecycle (init/check/deinit) |
| 2976 | **************************************************************************/ |
| 2977 | /* Signal handler: Do a soft stop, wakeup SPOE applet */ |
| 2978 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2979 | spoe_sig_stop(struct sig_handler *sh) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2980 | { |
| 2981 | struct proxy *p; |
| 2982 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 2983 | p = proxies_list; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2984 | while (p) { |
| 2985 | struct flt_conf *fconf; |
| 2986 | |
Christopher Faulet | 7f4ffad | 2023-05-11 09:08:28 +0200 | [diff] [blame] | 2987 | /* SPOE filter are not initialized for disabled proxoes. Move to |
| 2988 | * the next one |
| 2989 | */ |
| 2990 | if (p->flags & PR_FL_DISABLED) { |
| 2991 | p = p->next; |
| 2992 | continue; |
| 2993 | } |
| 2994 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2995 | list_for_each_entry(fconf, &p->filter_configs, list) { |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 2996 | struct spoe_config *conf; |
| 2997 | struct spoe_agent *agent; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2998 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2999 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3000 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 3001 | if (fconf->id != spoe_filter_id) |
| 3002 | continue; |
| 3003 | |
| 3004 | conf = fconf->conf; |
| 3005 | agent = conf->agent; |
| 3006 | |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3007 | for (i = 0; i < global.nbthread; ++i) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3008 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 3009 | list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list) |
| 3010 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 3011 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3012 | } |
| 3013 | } |
| 3014 | p = p->next; |
| 3015 | } |
| 3016 | } |
| 3017 | |
| 3018 | |
| 3019 | /* Initialize the SPOE filter. Returns -1 on error, else 0. */ |
| 3020 | static int |
| 3021 | spoe_init(struct proxy *px, struct flt_conf *fconf) |
| 3022 | { |
| 3023 | struct spoe_config *conf = fconf->conf; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3024 | |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3025 | /* conf->agent_fe was already initialized during the config |
| 3026 | * parsing. Finish initialization. */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 3027 | conf->agent_fe.last_change = ns_to_sec(now_ns); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3028 | conf->agent_fe.cap = PR_CAP_FE; |
| 3029 | conf->agent_fe.mode = PR_MODE_TCP; |
| 3030 | conf->agent_fe.maxconn = 0; |
| 3031 | conf->agent_fe.options2 |= PR_O2_INDEPSTR; |
| 3032 | conf->agent_fe.conn_retries = CONN_RETRIES; |
| 3033 | conf->agent_fe.accept = frontend_accept; |
| 3034 | conf->agent_fe.srv = NULL; |
| 3035 | conf->agent_fe.timeout.client = TICK_ETERNITY; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3036 | conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES; |
| 3037 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3038 | if (!sighandler_registered) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3039 | signal_register_fct(0, spoe_sig_stop, 0); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3040 | sighandler_registered = 1; |
| 3041 | } |
| 3042 | |
Christopher Faulet | 0029235 | 2019-01-09 15:05:10 +0100 | [diff] [blame] | 3043 | fconf->flags |= FLT_CFG_FL_HTX; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3044 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3045 | } |
| 3046 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3047 | /* Free resources allocated by the SPOE filter. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3048 | static void |
| 3049 | spoe_deinit(struct proxy *px, struct flt_conf *fconf) |
| 3050 | { |
| 3051 | struct spoe_config *conf = fconf->conf; |
| 3052 | |
| 3053 | if (conf) { |
| 3054 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3055 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3056 | spoe_release_agent(agent); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3057 | free(conf->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3058 | free(conf); |
| 3059 | } |
| 3060 | fconf->conf = NULL; |
| 3061 | } |
| 3062 | |
| 3063 | /* Check configuration of a SPOE filter for a specified proxy. |
| 3064 | * Return 1 on error, else 0. */ |
| 3065 | static int |
| 3066 | spoe_check(struct proxy *px, struct flt_conf *fconf) |
| 3067 | { |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3068 | struct flt_conf *f; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3069 | struct spoe_config *conf = fconf->conf; |
| 3070 | struct proxy *target; |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3071 | struct logsrv *logsrv; |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3072 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3073 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3074 | /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names |
| 3075 | * are uniq */ |
| 3076 | list_for_each_entry(f, &px->filter_configs, list) { |
| 3077 | struct spoe_config *c = f->conf; |
| 3078 | |
| 3079 | /* This is not an SPOE filter */ |
| 3080 | if (f->id != spoe_filter_id) |
| 3081 | continue; |
| 3082 | /* This is the current SPOE filter */ |
| 3083 | if (f == fconf) |
| 3084 | continue; |
| 3085 | |
| 3086 | /* Check engine Id. It should be uniq */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3087 | if (strcmp(conf->id, c->id) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3088 | ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n", |
| 3089 | px->id, conf->id); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3090 | return 1; |
| 3091 | } |
| 3092 | } |
| 3093 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3094 | target = proxy_be_by_name(conf->agent->b.name); |
| 3095 | if (target == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3096 | ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'" |
| 3097 | " declared at %s:%d.\n", |
| 3098 | px->id, conf->agent->b.name, conf->agent->id, |
| 3099 | conf->agent->conf.file, conf->agent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3100 | return 1; |
| 3101 | } |
| 3102 | if (target->mode != PR_MODE_TCP) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3103 | ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared" |
| 3104 | " at %s:%d does not support HTTP mode.\n", |
| 3105 | px->id, target->id, conf->agent->id, |
| 3106 | conf->agent->conf.file, conf->agent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3107 | return 1; |
| 3108 | } |
| 3109 | |
Christopher Faulet | fe26155 | 2019-03-18 13:57:42 +0100 | [diff] [blame] | 3110 | if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) { |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3111 | ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n", |
| 3112 | px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line); |
| 3113 | return 1; |
| 3114 | } |
| 3115 | for (i = 0; i < global.nbthread; ++i) { |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3116 | conf->agent->rt[i].engine_id = NULL; |
Christopher Faulet | fe26155 | 2019-03-18 13:57:42 +0100 | [diff] [blame] | 3117 | conf->agent->rt[i].frame_size = conf->agent->max_frame_size; |
| 3118 | conf->agent->rt[i].processing = 0; |
Christopher Faulet | e99c439 | 2023-04-26 15:56:59 +0200 | [diff] [blame] | 3119 | conf->agent->rt[i].idles = 0; |
Christopher Faulet | fe26155 | 2019-03-18 13:57:42 +0100 | [diff] [blame] | 3120 | LIST_INIT(&conf->agent->rt[i].applets); |
| 3121 | LIST_INIT(&conf->agent->rt[i].sending_queue); |
| 3122 | LIST_INIT(&conf->agent->rt[i].waiting_queue); |
| 3123 | HA_SPIN_INIT(&conf->agent->rt[i].lock); |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3124 | } |
| 3125 | |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3126 | list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) { |
| 3127 | if (logsrv->type == LOG_TARGET_BUFFER) { |
| 3128 | struct sink *sink = sink_find(logsrv->ring_name); |
| 3129 | |
| 3130 | if (!sink || sink->type != SINK_TYPE_BUFFER) { |
| 3131 | ha_alert("Proxy %s : log server used by SPOE agent '%s' declared" |
Ilya Shipitsin | d7a988c | 2021-03-04 23:26:15 +0500 | [diff] [blame] | 3132 | " at %s:%d uses unknown ring named '%s'.\n", |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3133 | px->id, conf->agent->id, conf->agent->conf.file, |
| 3134 | conf->agent->conf.line, logsrv->ring_name); |
| 3135 | return 1; |
| 3136 | } |
| 3137 | logsrv->sink = sink; |
| 3138 | } |
| 3139 | } |
| 3140 | |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3141 | ha_free(&conf->agent->b.name); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3142 | conf->agent->b.be = target; |
| 3143 | return 0; |
| 3144 | } |
| 3145 | |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3146 | /* Initializes the SPOE filter for a proxy for a specific thread. |
| 3147 | * Returns a negative value if an error occurs. */ |
| 3148 | static int |
| 3149 | spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf) |
| 3150 | { |
| 3151 | struct spoe_config *conf = fconf->conf; |
| 3152 | struct spoe_agent *agent = conf->agent; |
| 3153 | |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3154 | agent->rt[tid].engine_id = generate_pseudo_uuid(); |
| 3155 | if (agent->rt[tid].engine_id == NULL) |
| 3156 | return -1; |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3157 | return 0; |
| 3158 | } |
| 3159 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3160 | /************************************************************************** |
| 3161 | * Hooks attached to a stream |
| 3162 | *************************************************************************/ |
| 3163 | /* Called when a filter instance is created and attach to a stream. It creates |
| 3164 | * the context that will be used to process this stream. */ |
| 3165 | static int |
| 3166 | spoe_start(struct stream *s, struct filter *filter) |
| 3167 | { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3168 | struct spoe_config *conf = FLT_CONF(filter); |
| 3169 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3170 | struct spoe_context *ctx; |
| 3171 | |
| 3172 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 3173 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3174 | __FUNCTION__, s); |
| 3175 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 3176 | if ((ctx = spoe_create_context(s, filter)) == NULL) { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3177 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 3178 | " - failed to create SPOE context\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 3179 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | ccbc3fd | 2017-09-15 11:51:18 +0200 | [diff] [blame] | 3180 | __FUNCTION__, s); |
Christopher Faulet | 3b8e349 | 2018-03-26 17:20:58 +0200 | [diff] [blame] | 3181 | send_log(&conf->agent_fe, LOG_EMERG, |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3182 | "SPOE: [%s] failed to create SPOE context\n", |
| 3183 | agent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3184 | return 0; |
| 3185 | } |
| 3186 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3187 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3188 | filter->pre_analyzers |= AN_REQ_INSPECT_FE; |
| 3189 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3190 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3191 | filter->pre_analyzers |= AN_REQ_INSPECT_BE; |
| 3192 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3193 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3194 | filter->pre_analyzers |= AN_RES_INSPECT; |
| 3195 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3196 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3197 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE; |
| 3198 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3199 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3200 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE; |
| 3201 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3202 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3203 | filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE; |
| 3204 | |
| 3205 | return 1; |
| 3206 | } |
| 3207 | |
| 3208 | /* Called when a filter instance is detached from a stream. It release the |
| 3209 | * attached SPOE context. */ |
| 3210 | static void |
| 3211 | spoe_stop(struct stream *s, struct filter *filter) |
| 3212 | { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3213 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 3214 | (int)date.tv_sec, (int)date.tv_usec, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3215 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3216 | __FUNCTION__, s); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 3217 | spoe_destroy_context(filter); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3218 | } |
| 3219 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3220 | |
| 3221 | /* |
| 3222 | * Called when the stream is woken up because of expired timer. |
| 3223 | */ |
| 3224 | static void |
| 3225 | spoe_check_timeouts(struct stream *s, struct filter *filter) |
| 3226 | { |
| 3227 | struct spoe_context *ctx = filter->ctx; |
| 3228 | |
Christopher Faulet | ac58060 | 2018-03-20 16:09:20 +0100 | [diff] [blame] | 3229 | if (tick_is_expired(ctx->process_exp, now_ms)) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3230 | s->pending_events |= TASK_WOKEN_MSG; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3231 | } |
| 3232 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3233 | /* Called when we are ready to filter data on a channel */ |
| 3234 | static int |
| 3235 | spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 3236 | { |
| 3237 | struct spoe_context *ctx = filter->ctx; |
| 3238 | int ret = 1; |
| 3239 | |
| 3240 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3241 | " - ctx-flags=0x%08x\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 3242 | (int)date.tv_sec, (int)date.tv_usec, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3243 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3244 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 3245 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3246 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 3247 | goto out; |
| 3248 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3249 | if (!(chn->flags & CF_ISRESP)) { |
| 3250 | if (filter->pre_analyzers & AN_REQ_INSPECT_FE) |
| 3251 | chn->analysers |= AN_REQ_INSPECT_FE; |
| 3252 | if (filter->pre_analyzers & AN_REQ_INSPECT_BE) |
| 3253 | chn->analysers |= AN_REQ_INSPECT_BE; |
| 3254 | |
| 3255 | if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED) |
| 3256 | goto out; |
| 3257 | |
| 3258 | ctx->stream_id = s->uniq_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3259 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3260 | if (!ret) |
| 3261 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3262 | ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED; |
| 3263 | } |
| 3264 | else { |
Miroslav Zagorac | 8840326 | 2020-06-19 22:17:17 +0200 | [diff] [blame] | 3265 | if (filter->pre_analyzers & AN_RES_INSPECT) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3266 | chn->analysers |= AN_RES_INSPECT; |
| 3267 | |
| 3268 | if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED) |
| 3269 | goto out; |
| 3270 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3271 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3272 | if (!ret) { |
| 3273 | channel_dont_read(chn); |
| 3274 | channel_dont_close(chn); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3275 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3276 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3277 | ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | out: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3281 | return ret; |
| 3282 | } |
| 3283 | |
| 3284 | /* Called before a processing happens on a given channel */ |
| 3285 | static int |
| 3286 | spoe_chn_pre_analyze(struct stream *s, struct filter *filter, |
| 3287 | struct channel *chn, unsigned an_bit) |
| 3288 | { |
| 3289 | struct spoe_context *ctx = filter->ctx; |
| 3290 | int ret = 1; |
| 3291 | |
| 3292 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3293 | " - ctx-flags=0x%08x - ana=0x%08x\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 3294 | (int)date.tv_sec, (int)date.tv_usec, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3295 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3296 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 3297 | ctx->flags, an_bit); |
| 3298 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3299 | if (ctx->state == SPOE_CTX_ST_NONE) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3300 | goto out; |
| 3301 | |
| 3302 | switch (an_bit) { |
| 3303 | case AN_REQ_INSPECT_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3304 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3305 | break; |
| 3306 | case AN_REQ_INSPECT_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3307 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3308 | break; |
| 3309 | case AN_RES_INSPECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3310 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3311 | break; |
| 3312 | case AN_REQ_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3313 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3314 | break; |
| 3315 | case AN_REQ_HTTP_PROCESS_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3316 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3317 | break; |
| 3318 | case AN_RES_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3319 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3320 | break; |
| 3321 | } |
| 3322 | |
| 3323 | out: |
Christopher Faulet | 9cdca97 | 2018-02-01 08:45:45 +0100 | [diff] [blame] | 3324 | if (!ret && (chn->flags & CF_ISRESP)) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3325 | channel_dont_read(chn); |
| 3326 | channel_dont_close(chn); |
| 3327 | } |
| 3328 | return ret; |
| 3329 | } |
| 3330 | |
| 3331 | /* Called when the filtering on the channel ends. */ |
| 3332 | static int |
| 3333 | spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 3334 | { |
| 3335 | struct spoe_context *ctx = filter->ctx; |
| 3336 | |
| 3337 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3338 | " - ctx-flags=0x%08x\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 3339 | (int)date.tv_sec, (int)date.tv_usec, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3340 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3341 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 3342 | |
| 3343 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3344 | spoe_reset_context(ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | return 1; |
| 3348 | } |
| 3349 | |
| 3350 | /******************************************************************** |
| 3351 | * Functions that manage the filter initialization |
| 3352 | ********************************************************************/ |
| 3353 | struct flt_ops spoe_ops = { |
| 3354 | /* Manage SPOE filter, called for each filter declaration */ |
| 3355 | .init = spoe_init, |
| 3356 | .deinit = spoe_deinit, |
| 3357 | .check = spoe_check, |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3358 | .init_per_thread = spoe_init_per_thread, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3359 | |
| 3360 | /* Handle start/stop of SPOE */ |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3361 | .attach = spoe_start, |
| 3362 | .detach = spoe_stop, |
| 3363 | .check_timeouts = spoe_check_timeouts, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3364 | |
| 3365 | /* Handle channels activity */ |
| 3366 | .channel_start_analyze = spoe_start_analyze, |
| 3367 | .channel_pre_analyze = spoe_chn_pre_analyze, |
| 3368 | .channel_end_analyze = spoe_end_analyze, |
| 3369 | }; |
| 3370 | |
| 3371 | |
| 3372 | static int |
| 3373 | cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm) |
| 3374 | { |
| 3375 | const char *err; |
| 3376 | int i, err_code = 0; |
| 3377 | |
| 3378 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3379 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3380 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3381 | goto out; |
| 3382 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3383 | if (strcmp(args[0], "spoe-agent") == 0) { /* new spoe-agent section */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3384 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3385 | ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n", |
| 3386 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3387 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3388 | goto out; |
| 3389 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3390 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3391 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3392 | goto out; |
| 3393 | } |
| 3394 | |
| 3395 | err = invalid_char(args[1]); |
| 3396 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3397 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3398 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3399 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3400 | goto out; |
| 3401 | } |
| 3402 | |
| 3403 | if (curagent != NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3404 | ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n", |
| 3405 | 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 | if ((curagent = calloc(1, sizeof(*curagent))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3410 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3411 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3412 | goto out; |
| 3413 | } |
| 3414 | |
| 3415 | curagent->id = strdup(args[1]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3416 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3417 | curagent->conf.file = strdup(file); |
| 3418 | curagent->conf.line = linenum; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3419 | |
| 3420 | curagent->timeout.hello = TICK_ETERNITY; |
| 3421 | curagent->timeout.idle = TICK_ETERNITY; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3422 | curagent->timeout.processing = TICK_ETERNITY; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3423 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3424 | curagent->var_pfx = NULL; |
| 3425 | curagent->var_on_error = NULL; |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3426 | curagent->var_t_process = NULL; |
| 3427 | curagent->var_t_total = NULL; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3428 | curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3429 | curagent->cps_max = 0; |
| 3430 | curagent->eps_max = 0; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 3431 | curagent->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 3432 | curagent->max_fpa = 20; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3433 | |
| 3434 | for (i = 0; i < SPOE_EV_EVENTS; ++i) |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3435 | LIST_INIT(&curagent->events[i]); |
| 3436 | LIST_INIT(&curagent->groups); |
| 3437 | LIST_INIT(&curagent->messages); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3438 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3439 | else if (strcmp(args[0], "use-backend") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3440 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3441 | ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n", |
| 3442 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3443 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3444 | goto out; |
| 3445 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3446 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3447 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3448 | free(curagent->b.name); |
| 3449 | curagent->b.name = strdup(args[1]); |
| 3450 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3451 | else if (strcmp(args[0], "messages") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3452 | int cur_arg = 1; |
| 3453 | while (*args[cur_arg]) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3454 | struct spoe_placeholder *ph = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3455 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3456 | list_for_each_entry(ph, &curmphs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3457 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3458 | ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3459 | file, linenum, args[cur_arg]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3460 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3461 | goto out; |
| 3462 | } |
| 3463 | } |
| 3464 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3465 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3466 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3467 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3468 | goto out; |
| 3469 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3470 | ph->id = strdup(args[cur_arg]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3471 | LIST_APPEND(&curmphs, &ph->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3472 | cur_arg++; |
| 3473 | } |
| 3474 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3475 | else if (strcmp(args[0], "groups") == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3476 | int cur_arg = 1; |
| 3477 | while (*args[cur_arg]) { |
| 3478 | struct spoe_placeholder *ph = NULL; |
| 3479 | |
| 3480 | list_for_each_entry(ph, &curgphs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3481 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3482 | ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n", |
| 3483 | file, linenum, args[cur_arg]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3484 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3485 | goto out; |
| 3486 | } |
| 3487 | } |
| 3488 | |
| 3489 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3490 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3491 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3492 | goto out; |
| 3493 | } |
| 3494 | ph->id = strdup(args[cur_arg]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3495 | LIST_APPEND(&curgphs, &ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3496 | cur_arg++; |
| 3497 | } |
| 3498 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3499 | else if (strcmp(args[0], "timeout") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3500 | unsigned int *tv = NULL; |
| 3501 | const char *res; |
| 3502 | unsigned timeout; |
| 3503 | |
| 3504 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3505 | ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n", |
| 3506 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3507 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3508 | goto out; |
| 3509 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3510 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3511 | goto out; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3512 | if (strcmp(args[1], "hello") == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3513 | tv = &curagent->timeout.hello; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3514 | else if (strcmp(args[1], "idle") == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3515 | tv = &curagent->timeout.idle; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3516 | else if (strcmp(args[1], "processing") == 0) |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3517 | tv = &curagent->timeout.processing; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3518 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3519 | ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n", |
| 3520 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3521 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3522 | goto out; |
| 3523 | } |
| 3524 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3525 | ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n", |
| 3526 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3527 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3528 | goto out; |
| 3529 | } |
| 3530 | res = parse_time_err(args[2], &timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 3531 | if (res == PARSE_TIME_OVER) { |
| 3532 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n", |
| 3533 | file, linenum, args[2], args[0], args[1]); |
| 3534 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3535 | goto out; |
| 3536 | } |
| 3537 | else if (res == PARSE_TIME_UNDER) { |
| 3538 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n", |
| 3539 | file, linenum, args[2], args[0], args[1]); |
| 3540 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3541 | goto out; |
| 3542 | } |
| 3543 | else if (res) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3544 | ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n", |
| 3545 | file, linenum, *res, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3546 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3547 | goto out; |
| 3548 | } |
| 3549 | *tv = MS_TO_TICKS(timeout); |
| 3550 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3551 | else if (strcmp(args[0], "option") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3552 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3553 | ha_alert("parsing [%s:%d]: '%s' expects an option name.\n", |
| 3554 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3555 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3556 | goto out; |
| 3557 | } |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3558 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3559 | if (strcmp(args[1], "pipelining") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3560 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3561 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3562 | if (kwm == 1) |
| 3563 | curagent->flags &= ~SPOE_FL_PIPELINING; |
| 3564 | else |
| 3565 | curagent->flags |= SPOE_FL_PIPELINING; |
| 3566 | goto out; |
| 3567 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3568 | else if (strcmp(args[1], "async") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3569 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3570 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3571 | if (kwm == 1) |
| 3572 | curagent->flags &= ~SPOE_FL_ASYNC; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3573 | else |
| 3574 | curagent->flags |= SPOE_FL_ASYNC; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3575 | goto out; |
| 3576 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3577 | else if (strcmp(args[1], "send-frag-payload") == 0) { |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 3578 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3579 | goto out; |
| 3580 | if (kwm == 1) |
| 3581 | curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION; |
| 3582 | else |
| 3583 | curagent->flags |= SPOE_FL_SND_FRAGMENTATION; |
| 3584 | goto out; |
| 3585 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3586 | else if (strcmp(args[1], "dontlog-normal") == 0) { |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 3587 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3588 | goto out; |
| 3589 | if (kwm == 1) |
| 3590 | curpxopts2 &= ~PR_O2_NOLOGNORM; |
| 3591 | else |
| 3592 | curpxopts2 |= PR_O2_NOLOGNORM; |
Christopher Faulet | 799f518 | 2018-04-26 11:36:34 +0200 | [diff] [blame] | 3593 | goto out; |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 3594 | } |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3595 | |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3596 | /* Following options does not support negation */ |
| 3597 | if (kwm == 1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3598 | ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n", |
| 3599 | file, linenum, args[1]); |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3600 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3601 | goto out; |
| 3602 | } |
| 3603 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3604 | if (strcmp(args[1], "var-prefix") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3605 | char *tmp; |
| 3606 | |
| 3607 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3608 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3609 | file, linenum, args[0], |
| 3610 | args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3611 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3612 | goto out; |
| 3613 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3614 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3615 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3616 | tmp = args[2]; |
| 3617 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3618 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3619 | 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] | 3620 | file, linenum, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3621 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3622 | goto out; |
| 3623 | } |
| 3624 | tmp++; |
| 3625 | } |
| 3626 | curagent->var_pfx = strdup(args[2]); |
| 3627 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3628 | else if (strcmp(args[1], "force-set-var") == 0) { |
Etienne Carriere | aec8989 | 2017-12-14 09:36:40 +0000 | [diff] [blame] | 3629 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3630 | goto out; |
| 3631 | curagent->flags |= SPOE_FL_FORCE_SET_VAR; |
| 3632 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3633 | else if (strcmp(args[1], "continue-on-error") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3634 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3635 | goto out; |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3636 | curagent->flags |= SPOE_FL_CONT_ON_ERR; |
| 3637 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3638 | else if (strcmp(args[1], "set-on-error") == 0) { |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3639 | char *tmp; |
| 3640 | |
| 3641 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3642 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3643 | file, linenum, args[0], |
| 3644 | args[1]); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3645 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3646 | goto out; |
| 3647 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3648 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3649 | goto out; |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3650 | tmp = args[2]; |
| 3651 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3652 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3653 | 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] | 3654 | file, linenum, args[0], args[1]); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3655 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3656 | goto out; |
| 3657 | } |
| 3658 | tmp++; |
| 3659 | } |
| 3660 | curagent->var_on_error = strdup(args[2]); |
| 3661 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3662 | else if (strcmp(args[1], "set-process-time") == 0) { |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3663 | char *tmp; |
| 3664 | |
| 3665 | if (!*args[2]) { |
| 3666 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3667 | file, linenum, args[0], |
| 3668 | args[1]); |
| 3669 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3670 | goto out; |
| 3671 | } |
| 3672 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3673 | goto out; |
| 3674 | tmp = args[2]; |
| 3675 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3676 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3677 | 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] | 3678 | file, linenum, args[0], args[1]); |
| 3679 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3680 | goto out; |
| 3681 | } |
| 3682 | tmp++; |
| 3683 | } |
| 3684 | curagent->var_t_process = strdup(args[2]); |
| 3685 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3686 | else if (strcmp(args[1], "set-total-time") == 0) { |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3687 | char *tmp; |
| 3688 | |
| 3689 | if (!*args[2]) { |
| 3690 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3691 | file, linenum, args[0], |
| 3692 | args[1]); |
| 3693 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3694 | goto out; |
| 3695 | } |
| 3696 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3697 | goto out; |
| 3698 | tmp = args[2]; |
| 3699 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3700 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3701 | 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] | 3702 | file, linenum, args[0], args[1]); |
| 3703 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3704 | goto out; |
| 3705 | } |
| 3706 | tmp++; |
| 3707 | } |
| 3708 | curagent->var_t_total = strdup(args[2]); |
| 3709 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3710 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3711 | ha_alert("parsing [%s:%d]: option '%s' is not supported.\n", |
| 3712 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3713 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3714 | goto out; |
| 3715 | } |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3716 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3717 | else if (strcmp(args[0], "maxconnrate") == 0) { |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3718 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3719 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3720 | file, linenum, args[0]); |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3721 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3722 | goto out; |
| 3723 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3724 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3725 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3726 | curagent->cps_max = atol(args[1]); |
| 3727 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3728 | else if (strcmp(args[0], "maxerrrate") == 0) { |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3729 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3730 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3731 | file, linenum, args[0]); |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3732 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3733 | goto out; |
| 3734 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3735 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3736 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3737 | curagent->eps_max = atol(args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3738 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3739 | else if (strcmp(args[0], "max-frame-size") == 0) { |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3740 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3741 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3742 | file, linenum, args[0]); |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3743 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3744 | goto out; |
| 3745 | } |
| 3746 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3747 | goto out; |
| 3748 | curagent->max_frame_size = atol(args[1]); |
| 3749 | if (curagent->max_frame_size < MIN_FRAME_SIZE || |
| 3750 | curagent->max_frame_size > MAX_FRAME_SIZE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3751 | ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n", |
| 3752 | file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE); |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3753 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3754 | goto out; |
| 3755 | } |
| 3756 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3757 | else if (strcmp(args[0], "max-waiting-frames") == 0) { |
Christopher Faulet | e8ade38 | 2018-01-25 15:32:22 +0100 | [diff] [blame] | 3758 | if (!*args[1]) { |
| 3759 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3760 | file, linenum, args[0]); |
| 3761 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3762 | goto out; |
| 3763 | } |
| 3764 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3765 | goto out; |
| 3766 | curagent->max_fpa = atol(args[1]); |
| 3767 | if (curagent->max_fpa < 1) { |
| 3768 | ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n", |
| 3769 | file, linenum, args[0]); |
| 3770 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3771 | goto out; |
| 3772 | } |
| 3773 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3774 | else if (strcmp(args[0], "register-var-names") == 0) { |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3775 | int cur_arg; |
| 3776 | |
| 3777 | if (!*args[1]) { |
| 3778 | ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n", |
| 3779 | file, linenum, args[0]); |
| 3780 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3781 | goto out; |
| 3782 | } |
| 3783 | cur_arg = 1; |
| 3784 | while (*args[cur_arg]) { |
| 3785 | struct spoe_var_placeholder *vph; |
| 3786 | |
| 3787 | if ((vph = calloc(1, sizeof(*vph))) == NULL) { |
| 3788 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3789 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3790 | goto out; |
| 3791 | } |
| 3792 | if ((vph->name = strdup(args[cur_arg])) == NULL) { |
Tim Duesterhus | b298613 | 2019-06-23 22:10:13 +0200 | [diff] [blame] | 3793 | free(vph); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3794 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3795 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3796 | goto out; |
| 3797 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3798 | LIST_APPEND(&curvars, &vph->list); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3799 | cur_arg++; |
| 3800 | } |
| 3801 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3802 | else if (strcmp(args[0], "log") == 0) { |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3803 | char *errmsg = NULL; |
| 3804 | |
Emeric Brun | 9533a70 | 2021-04-02 10:13:43 +0200 | [diff] [blame] | 3805 | if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), file, linenum, &errmsg)) { |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3806 | ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); |
| 3807 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3808 | goto out; |
| 3809 | } |
| 3810 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3811 | else if (*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3812 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n", |
| 3813 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3814 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3815 | goto out; |
| 3816 | } |
| 3817 | out: |
| 3818 | return err_code; |
| 3819 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3820 | static int |
| 3821 | cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm) |
| 3822 | { |
| 3823 | struct spoe_group *grp; |
| 3824 | const char *err; |
| 3825 | int err_code = 0; |
| 3826 | |
| 3827 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3828 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3829 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3830 | goto out; |
| 3831 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3832 | if (strcmp(args[0], "spoe-group") == 0) { /* new spoe-group section */ |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3833 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3834 | ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n", |
| 3835 | file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3836 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3837 | goto out; |
| 3838 | } |
| 3839 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3840 | err_code |= ERR_ABORT; |
| 3841 | goto out; |
| 3842 | } |
| 3843 | |
| 3844 | err = invalid_char(args[1]); |
| 3845 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3846 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3847 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3848 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3849 | goto out; |
| 3850 | } |
| 3851 | |
| 3852 | list_for_each_entry(grp, &curgrps, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3853 | if (strcmp(grp->id, args[1]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3854 | ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same" |
| 3855 | " name as another one declared at %s:%d.\n", |
| 3856 | file, linenum, args[1], grp->conf.file, grp->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3857 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3858 | goto out; |
| 3859 | } |
| 3860 | } |
| 3861 | |
| 3862 | if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3863 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3864 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3865 | goto out; |
| 3866 | } |
| 3867 | |
| 3868 | curgrp->id = strdup(args[1]); |
| 3869 | curgrp->conf.file = strdup(file); |
| 3870 | curgrp->conf.line = linenum; |
| 3871 | LIST_INIT(&curgrp->phs); |
| 3872 | LIST_INIT(&curgrp->messages); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3873 | LIST_APPEND(&curgrps, &curgrp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3874 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3875 | else if (strcmp(args[0], "messages") == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3876 | int cur_arg = 1; |
| 3877 | while (*args[cur_arg]) { |
| 3878 | struct spoe_placeholder *ph = NULL; |
| 3879 | |
| 3880 | list_for_each_entry(ph, &curgrp->phs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3881 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3882 | ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3883 | file, linenum, args[cur_arg]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3884 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3885 | goto out; |
| 3886 | } |
| 3887 | } |
| 3888 | |
| 3889 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3890 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3891 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3892 | goto out; |
| 3893 | } |
| 3894 | ph->id = strdup(args[cur_arg]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3895 | LIST_APPEND(&curgrp->phs, &ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3896 | cur_arg++; |
| 3897 | } |
| 3898 | } |
| 3899 | else if (*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3900 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n", |
| 3901 | file, linenum, args[0]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3902 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3903 | goto out; |
| 3904 | } |
| 3905 | out: |
| 3906 | return err_code; |
| 3907 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3908 | |
| 3909 | static int |
| 3910 | cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm) |
| 3911 | { |
| 3912 | struct spoe_message *msg; |
| 3913 | struct spoe_arg *arg; |
| 3914 | const char *err; |
| 3915 | char *errmsg = NULL; |
| 3916 | int err_code = 0; |
| 3917 | |
| 3918 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3919 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3920 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3921 | goto out; |
| 3922 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3923 | if (strcmp(args[0], "spoe-message") == 0) { /* new spoe-message section */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3924 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3925 | ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n", |
| 3926 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3927 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3928 | goto out; |
| 3929 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3930 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3931 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3932 | goto out; |
| 3933 | } |
| 3934 | |
| 3935 | err = invalid_char(args[1]); |
| 3936 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3937 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3938 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3939 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3940 | goto out; |
| 3941 | } |
| 3942 | |
| 3943 | list_for_each_entry(msg, &curmsgs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3944 | if (strcmp(msg->id, args[1]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3945 | ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same" |
| 3946 | " name as another one declared at %s:%d.\n", |
| 3947 | file, linenum, args[1], msg->conf.file, msg->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3948 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3949 | goto out; |
| 3950 | } |
| 3951 | } |
| 3952 | |
| 3953 | if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3954 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3955 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3956 | goto out; |
| 3957 | } |
| 3958 | |
| 3959 | curmsg->id = strdup(args[1]); |
| 3960 | curmsg->id_len = strlen(curmsg->id); |
| 3961 | curmsg->event = SPOE_EV_NONE; |
| 3962 | curmsg->conf.file = strdup(file); |
| 3963 | curmsg->conf.line = linenum; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 3964 | curmsg->nargs = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3965 | LIST_INIT(&curmsg->args); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3966 | LIST_INIT(&curmsg->acls); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3967 | LIST_INIT(&curmsg->by_evt); |
| 3968 | LIST_INIT(&curmsg->by_grp); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3969 | LIST_APPEND(&curmsgs, &curmsg->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3970 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3971 | else if (strcmp(args[0], "args") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3972 | int cur_arg = 1; |
| 3973 | |
| 3974 | curproxy->conf.args.ctx = ARGC_SPOE; |
| 3975 | curproxy->conf.args.file = file; |
| 3976 | curproxy->conf.args.line = linenum; |
| 3977 | while (*args[cur_arg]) { |
| 3978 | char *delim = strchr(args[cur_arg], '='); |
| 3979 | int idx = 0; |
| 3980 | |
| 3981 | if ((arg = calloc(1, sizeof(*arg))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3982 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3983 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3984 | goto out; |
| 3985 | } |
| 3986 | |
| 3987 | if (!delim) { |
| 3988 | arg->name = NULL; |
| 3989 | arg->name_len = 0; |
| 3990 | delim = args[cur_arg]; |
| 3991 | } |
| 3992 | else { |
| 3993 | arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]); |
| 3994 | arg->name_len = delim - args[cur_arg]; |
| 3995 | delim++; |
| 3996 | } |
Christopher Faulet | b0b4238 | 2017-02-23 22:41:09 +0100 | [diff] [blame] | 3997 | arg->expr = sample_parse_expr((char*[]){delim, NULL}, |
| 3998 | &idx, file, linenum, &errmsg, |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 3999 | &curproxy->conf.args, NULL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4000 | if (arg->expr == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4001 | 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] | 4002 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4003 | free(arg->name); |
| 4004 | free(arg); |
| 4005 | goto out; |
| 4006 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 4007 | curmsg->nargs++; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4008 | LIST_APPEND(&curmsg->args, &arg->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4009 | cur_arg++; |
| 4010 | } |
| 4011 | curproxy->conf.args.file = NULL; |
| 4012 | curproxy->conf.args.line = 0; |
| 4013 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4014 | else if (strcmp(args[0], "acl") == 0) { |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4015 | err = invalid_char(args[1]); |
| 4016 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4017 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n", |
| 4018 | file, linenum, *err, args[1]); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4019 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4020 | goto out; |
| 4021 | } |
Tim Duesterhus | 0cf811a | 2020-02-05 21:00:50 +0100 | [diff] [blame] | 4022 | if (strcasecmp(args[1], "or") == 0) { |
Tim Duesterhus | f1bc24c | 2020-02-06 22:04:03 +0100 | [diff] [blame] | 4023 | 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] | 4024 | "logical disjunction within a condition.\n", |
| 4025 | file, linenum, args[1]); |
| 4026 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4027 | goto out; |
| 4028 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4029 | 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] | 4030 | ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n", |
| 4031 | file, linenum, args[1], errmsg); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4032 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4033 | goto out; |
| 4034 | } |
| 4035 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4036 | else if (strcmp(args[0], "event") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4037 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4038 | ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4039 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4040 | goto out; |
| 4041 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4042 | /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */ |
| 4043 | /* goto out; */ |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4044 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4045 | 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] | 4046 | curmsg->event = SPOE_EV_ON_CLIENT_SESS; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4047 | 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] | 4048 | curmsg->event = SPOE_EV_ON_SERVER_SESS; |
| 4049 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4050 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4051 | curmsg->event = SPOE_EV_ON_TCP_REQ_FE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4052 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4053 | curmsg->event = SPOE_EV_ON_TCP_REQ_BE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4054 | 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] | 4055 | curmsg->event = SPOE_EV_ON_TCP_RSP; |
| 4056 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4057 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4058 | curmsg->event = SPOE_EV_ON_HTTP_REQ_FE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4059 | else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]) == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4060 | curmsg->event = SPOE_EV_ON_HTTP_REQ_BE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4061 | 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] | 4062 | curmsg->event = SPOE_EV_ON_HTTP_RSP; |
| 4063 | else { |
Joseph Herlant | f1da69d | 2018-11-15 13:49:02 -0800 | [diff] [blame] | 4064 | ha_alert("parsing [%s:%d] : unknown event '%s'.\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4065 | file, linenum, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4066 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4067 | goto out; |
| 4068 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4069 | |
| 4070 | if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) { |
| 4071 | struct acl_cond *cond; |
| 4072 | |
| 4073 | cond = build_acl_cond(file, linenum, &curmsg->acls, |
| 4074 | curproxy, (const char **)args+2, |
| 4075 | &errmsg); |
| 4076 | if (cond == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4077 | ha_alert("parsing [%s:%d] : error detected while " |
| 4078 | "parsing an 'event %s' condition : %s.\n", |
| 4079 | file, linenum, args[1], errmsg); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4080 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4081 | goto out; |
| 4082 | } |
| 4083 | curmsg->cond = cond; |
| 4084 | } |
| 4085 | else if (*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4086 | ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' " |
| 4087 | "or 'unless' followed by a condition but found '%s'.\n", |
| 4088 | file, linenum, args[1], args[2]); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4089 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4090 | goto out; |
| 4091 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4092 | } |
| 4093 | else if (!*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4094 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n", |
| 4095 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4096 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4097 | goto out; |
| 4098 | } |
| 4099 | out: |
| 4100 | free(errmsg); |
| 4101 | return err_code; |
| 4102 | } |
| 4103 | |
| 4104 | /* Return -1 on error, else 0 */ |
| 4105 | static int |
| 4106 | parse_spoe_flt(char **args, int *cur_arg, struct proxy *px, |
| 4107 | struct flt_conf *fconf, char **err, void *private) |
| 4108 | { |
| 4109 | struct list backup_sections; |
| 4110 | struct spoe_config *conf; |
| 4111 | struct spoe_message *msg, *msgback; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4112 | struct spoe_group *grp, *grpback; |
| 4113 | struct spoe_placeholder *ph, *phback; |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4114 | struct spoe_var_placeholder *vph, *vphback; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4115 | struct logsrv *logsrv, *logsrvback; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4116 | char *file = NULL, *engine = NULL; |
| 4117 | int ret, pos = *cur_arg + 1; |
| 4118 | |
Christopher Faulet | 84c844e | 2018-03-23 14:37:14 +0100 | [diff] [blame] | 4119 | LIST_INIT(&curmsgs); |
| 4120 | LIST_INIT(&curgrps); |
| 4121 | LIST_INIT(&curmphs); |
| 4122 | LIST_INIT(&curgphs); |
| 4123 | LIST_INIT(&curvars); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4124 | LIST_INIT(&curlogsrvs); |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 4125 | curpxopts = 0; |
| 4126 | curpxopts2 = 0; |
Christopher Faulet | 84c844e | 2018-03-23 14:37:14 +0100 | [diff] [blame] | 4127 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4128 | conf = calloc(1, sizeof(*conf)); |
| 4129 | if (conf == NULL) { |
| 4130 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 4131 | goto error; |
| 4132 | } |
| 4133 | conf->proxy = px; |
| 4134 | |
| 4135 | while (*args[pos]) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4136 | if (strcmp(args[pos], "config") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4137 | if (!*args[pos+1]) { |
| 4138 | memprintf(err, "'%s' : '%s' option without value", |
| 4139 | args[*cur_arg], args[pos]); |
| 4140 | goto error; |
| 4141 | } |
| 4142 | file = args[pos+1]; |
| 4143 | pos += 2; |
| 4144 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4145 | else if (strcmp(args[pos], "engine") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4146 | if (!*args[pos+1]) { |
| 4147 | memprintf(err, "'%s' : '%s' option without value", |
| 4148 | args[*cur_arg], args[pos]); |
| 4149 | goto error; |
| 4150 | } |
| 4151 | engine = args[pos+1]; |
| 4152 | pos += 2; |
| 4153 | } |
| 4154 | else { |
| 4155 | memprintf(err, "unknown keyword '%s'", args[pos]); |
| 4156 | goto error; |
| 4157 | } |
| 4158 | } |
| 4159 | if (file == NULL) { |
| 4160 | memprintf(err, "'%s' : missing config file", args[*cur_arg]); |
| 4161 | goto error; |
| 4162 | } |
| 4163 | |
| 4164 | /* backup sections and register SPOE sections */ |
| 4165 | LIST_INIT(&backup_sections); |
| 4166 | cfg_backup_sections(&backup_sections); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4167 | cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL); |
| 4168 | cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL); |
William Lallemand | d2ff56d | 2017-10-16 11:06:50 +0200 | [diff] [blame] | 4169 | cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4170 | |
| 4171 | /* Parse SPOE filter configuration file */ |
| 4172 | curengine = engine; |
| 4173 | curproxy = px; |
| 4174 | curagent = NULL; |
| 4175 | curmsg = NULL; |
| 4176 | ret = readcfgfile(file); |
| 4177 | curproxy = NULL; |
| 4178 | |
| 4179 | /* unregister SPOE sections and restore previous sections */ |
| 4180 | cfg_unregister_sections(); |
| 4181 | cfg_restore_sections(&backup_sections); |
| 4182 | |
| 4183 | if (ret == -1) { |
| 4184 | memprintf(err, "Could not open configuration file %s : %s", |
| 4185 | file, strerror(errno)); |
| 4186 | goto error; |
| 4187 | } |
| 4188 | if (ret & (ERR_ABORT|ERR_FATAL)) { |
| 4189 | memprintf(err, "Error(s) found in configuration file %s", file); |
| 4190 | goto error; |
| 4191 | } |
| 4192 | |
| 4193 | /* Check SPOE agent */ |
| 4194 | if (curagent == NULL) { |
| 4195 | memprintf(err, "No SPOE agent found in file %s", file); |
| 4196 | goto error; |
| 4197 | } |
| 4198 | if (curagent->b.name == NULL) { |
| 4199 | memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d", |
| 4200 | curagent->id, curagent->conf.file, curagent->conf.line); |
| 4201 | goto error; |
| 4202 | } |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 4203 | if (curagent->timeout.hello == TICK_ETERNITY || |
| 4204 | curagent->timeout.idle == TICK_ETERNITY || |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 4205 | curagent->timeout.processing == TICK_ETERNITY) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4206 | ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n" |
| 4207 | " | While not properly invalid, you will certainly encounter various problems\n" |
| 4208 | " | with such a configuration. To fix this, please ensure that all following\n" |
| 4209 | " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n", |
| 4210 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4211 | } |
| 4212 | if (curagent->var_pfx == NULL) { |
| 4213 | char *tmp = curagent->id; |
| 4214 | |
| 4215 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4216 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4217 | memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. " |
| 4218 | "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n", |
| 4219 | curagent->id, curagent->id, curagent->conf.file, curagent->conf.line); |
| 4220 | goto error; |
| 4221 | } |
| 4222 | tmp++; |
| 4223 | } |
| 4224 | curagent->var_pfx = strdup(curagent->id); |
| 4225 | } |
| 4226 | |
Christopher Faulet | b7426d1 | 2018-03-21 14:12:17 +0100 | [diff] [blame] | 4227 | if (curagent->var_on_error) { |
| 4228 | struct arg arg; |
| 4229 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4230 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | b7426d1 | 2018-03-21 14:12:17 +0100 | [diff] [blame] | 4231 | curagent->var_pfx, curagent->var_on_error); |
| 4232 | |
| 4233 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4234 | arg.data.str.area = trash.area; |
| 4235 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4236 | 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] | 4237 | if (!vars_check_arg(&arg, err)) { |
| 4238 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4239 | curagent->id, curagent->var_pfx, curagent->var_on_error, *err); |
| 4240 | goto error; |
| 4241 | } |
| 4242 | } |
| 4243 | |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4244 | if (curagent->var_t_process) { |
| 4245 | struct arg arg; |
| 4246 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4247 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4248 | curagent->var_pfx, curagent->var_t_process); |
| 4249 | |
| 4250 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4251 | arg.data.str.area = trash.area; |
| 4252 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4253 | 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] | 4254 | if (!vars_check_arg(&arg, err)) { |
| 4255 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4256 | curagent->id, curagent->var_pfx, curagent->var_t_process, *err); |
| 4257 | goto error; |
| 4258 | } |
| 4259 | } |
| 4260 | |
| 4261 | if (curagent->var_t_total) { |
| 4262 | struct arg arg; |
| 4263 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4264 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4265 | curagent->var_pfx, curagent->var_t_total); |
| 4266 | |
| 4267 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4268 | arg.data.str.area = trash.area; |
| 4269 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4270 | 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] | 4271 | if (!vars_check_arg(&arg, err)) { |
| 4272 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4273 | curagent->id, curagent->var_pfx, curagent->var_t_process, *err); |
| 4274 | goto error; |
| 4275 | } |
| 4276 | } |
| 4277 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4278 | if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4279 | ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n", |
| 4280 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4281 | goto finish; |
| 4282 | } |
| 4283 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4284 | /* Replace placeholders by the corresponding messages for the SPOE |
| 4285 | * agent */ |
| 4286 | list_for_each_entry(ph, &curmphs, list) { |
| 4287 | list_for_each_entry(msg, &curmsgs, list) { |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4288 | struct spoe_arg *arg; |
| 4289 | unsigned int where; |
| 4290 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4291 | if (strcmp(msg->id, ph->id) == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4292 | if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) { |
| 4293 | if (msg->event == SPOE_EV_ON_TCP_REQ_BE) |
| 4294 | msg->event = SPOE_EV_ON_TCP_REQ_FE; |
| 4295 | if (msg->event == SPOE_EV_ON_HTTP_REQ_BE) |
| 4296 | msg->event = SPOE_EV_ON_HTTP_REQ_FE; |
| 4297 | } |
| 4298 | if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS || |
| 4299 | msg->event == SPOE_EV_ON_TCP_REQ_FE || |
| 4300 | msg->event == SPOE_EV_ON_HTTP_REQ_FE)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4301 | ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n", |
| 4302 | px->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 | } |
| 4305 | if (msg->event == SPOE_EV_NONE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4306 | ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n", |
| 4307 | px->id, msg->id, msg->conf.file, msg->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4308 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4309 | } |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4310 | |
| 4311 | where = 0; |
| 4312 | switch (msg->event) { |
| 4313 | case SPOE_EV_ON_CLIENT_SESS: |
| 4314 | where |= SMP_VAL_FE_CON_ACC; |
| 4315 | break; |
| 4316 | |
| 4317 | case SPOE_EV_ON_TCP_REQ_FE: |
| 4318 | where |= SMP_VAL_FE_REQ_CNT; |
| 4319 | break; |
| 4320 | |
| 4321 | case SPOE_EV_ON_HTTP_REQ_FE: |
| 4322 | where |= SMP_VAL_FE_HRQ_HDR; |
| 4323 | break; |
| 4324 | |
| 4325 | case SPOE_EV_ON_TCP_REQ_BE: |
| 4326 | if (px->cap & PR_CAP_FE) |
| 4327 | where |= SMP_VAL_FE_REQ_CNT; |
| 4328 | if (px->cap & PR_CAP_BE) |
| 4329 | where |= SMP_VAL_BE_REQ_CNT; |
| 4330 | break; |
| 4331 | |
| 4332 | case SPOE_EV_ON_HTTP_REQ_BE: |
| 4333 | if (px->cap & PR_CAP_FE) |
| 4334 | where |= SMP_VAL_FE_HRQ_HDR; |
| 4335 | if (px->cap & PR_CAP_BE) |
| 4336 | where |= SMP_VAL_BE_HRQ_HDR; |
| 4337 | break; |
| 4338 | |
| 4339 | case SPOE_EV_ON_SERVER_SESS: |
| 4340 | where |= SMP_VAL_BE_SRV_CON; |
| 4341 | break; |
| 4342 | |
| 4343 | case SPOE_EV_ON_TCP_RSP: |
| 4344 | if (px->cap & PR_CAP_FE) |
| 4345 | where |= SMP_VAL_FE_RES_CNT; |
| 4346 | if (px->cap & PR_CAP_BE) |
| 4347 | where |= SMP_VAL_BE_RES_CNT; |
| 4348 | break; |
| 4349 | |
| 4350 | case SPOE_EV_ON_HTTP_RSP: |
| 4351 | if (px->cap & PR_CAP_FE) |
| 4352 | where |= SMP_VAL_FE_HRS_HDR; |
| 4353 | if (px->cap & PR_CAP_BE) |
| 4354 | where |= SMP_VAL_BE_HRS_HDR; |
| 4355 | break; |
| 4356 | |
| 4357 | default: |
| 4358 | break; |
| 4359 | } |
| 4360 | |
| 4361 | list_for_each_entry(arg, &msg->args, list) { |
| 4362 | if (!(arg->expr->fetch->val & where)) { |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4363 | memprintf(err, "Ignore SPOE message '%s' at %s:%d: " |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4364 | "some args extract information from '%s', " |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4365 | "none of which is available here ('%s')", |
| 4366 | msg->id, msg->conf.file, msg->conf.line, |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4367 | sample_ckp_names(arg->expr->fetch->use), |
| 4368 | sample_ckp_names(where)); |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4369 | goto error; |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4370 | } |
| 4371 | } |
| 4372 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4373 | msg->agent = curagent; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4374 | LIST_APPEND(&curagent->events[msg->event], &msg->by_evt); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4375 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4376 | } |
| 4377 | } |
| 4378 | 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] | 4379 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4380 | goto error; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4381 | next_mph: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4382 | continue; |
| 4383 | } |
| 4384 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4385 | /* Replace placeholders by the corresponding groups for the SPOE |
| 4386 | * agent */ |
| 4387 | list_for_each_entry(ph, &curgphs, list) { |
| 4388 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4389 | if (strcmp(grp->id, ph->id) == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4390 | grp->agent = curagent; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4391 | LIST_DELETE(&grp->list); |
| 4392 | LIST_APPEND(&curagent->groups, &grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4393 | goto next_aph; |
| 4394 | } |
| 4395 | } |
| 4396 | memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d", |
| 4397 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 4398 | goto error; |
| 4399 | next_aph: |
| 4400 | continue; |
| 4401 | } |
| 4402 | |
| 4403 | /* Replace placeholders by the corresponding message for each SPOE |
| 4404 | * group of the SPOE agent */ |
| 4405 | list_for_each_entry(grp, &curagent->groups, list) { |
| 4406 | list_for_each_entry_safe(ph, phback, &grp->phs, list) { |
| 4407 | list_for_each_entry(msg, &curmsgs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4408 | if (strcmp(msg->id, ph->id) == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4409 | if (msg->group != NULL) { |
| 4410 | memprintf(err, "SPOE message '%s' already belongs to " |
| 4411 | "the SPOE group '%s' declare at %s:%d", |
| 4412 | msg->id, msg->group->id, |
| 4413 | msg->group->conf.file, |
| 4414 | msg->group->conf.line); |
| 4415 | goto error; |
| 4416 | } |
| 4417 | |
| 4418 | /* Scope for arguments are not checked for now. We will check |
| 4419 | * them only if a rule use the corresponding SPOE group. */ |
| 4420 | msg->agent = curagent; |
| 4421 | msg->group = grp; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4422 | LIST_DELETE(&ph->list); |
| 4423 | LIST_APPEND(&grp->messages, &msg->by_grp); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4424 | goto next_mph_grp; |
| 4425 | } |
| 4426 | } |
| 4427 | memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d", |
| 4428 | grp->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 4429 | goto error; |
| 4430 | next_mph_grp: |
| 4431 | continue; |
| 4432 | } |
| 4433 | } |
| 4434 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4435 | finish: |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4436 | /* move curmsgs to the agent message list */ |
| 4437 | curmsgs.n->p = &curagent->messages; |
| 4438 | curmsgs.p->n = &curagent->messages; |
| 4439 | curagent->messages = curmsgs; |
| 4440 | LIST_INIT(&curmsgs); |
| 4441 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 4442 | conf->id = strdup(engine ? engine : curagent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4443 | conf->agent = curagent; |
Christopher Faulet | 434b852 | 2021-08-02 17:51:01 +0200 | [diff] [blame] | 4444 | curagent->spoe_conf = conf; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4445 | |
| 4446 | /* Start agent's proxy initialization here. It will be finished during |
| 4447 | * the filter init. */ |
| 4448 | memset(&conf->agent_fe, 0, sizeof(conf->agent_fe)); |
| 4449 | init_new_proxy(&conf->agent_fe); |
| 4450 | conf->agent_fe.id = conf->agent->id; |
| 4451 | conf->agent_fe.parent = conf->agent; |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 4452 | conf->agent_fe.options |= curpxopts; |
| 4453 | conf->agent_fe.options2 |= curpxopts2; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4454 | |
| 4455 | list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4456 | LIST_DELETE(&logsrv->list); |
| 4457 | LIST_APPEND(&conf->agent_fe.logsrvs, &logsrv->list); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4458 | } |
| 4459 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4460 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4461 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4462 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4463 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4464 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4465 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4466 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4467 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4468 | list_for_each_entry_safe(vph, vphback, &curvars, list) { |
| 4469 | struct arg arg; |
| 4470 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4471 | trash.data = snprintf(trash.area, trash.size, "proc.%s.%s", |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4472 | curagent->var_pfx, vph->name); |
| 4473 | |
| 4474 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4475 | arg.data.str.area = trash.area; |
| 4476 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4477 | 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] | 4478 | if (!vars_check_arg(&arg, err)) { |
| 4479 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4480 | curagent->id, curagent->var_pfx, vph->name, *err); |
| 4481 | goto error; |
| 4482 | } |
| 4483 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4484 | LIST_DELETE(&vph->list); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4485 | free(vph->name); |
| 4486 | free(vph); |
| 4487 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4488 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4489 | LIST_DELETE(&grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4490 | spoe_release_group(grp); |
| 4491 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4492 | *cur_arg = pos; |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 4493 | fconf->id = spoe_filter_id; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4494 | fconf->ops = &spoe_ops; |
| 4495 | fconf->conf = conf; |
| 4496 | return 0; |
| 4497 | |
| 4498 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4499 | spoe_release_agent(curagent); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4500 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4501 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4502 | spoe_release_placeholder(ph); |
| 4503 | } |
| 4504 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4505 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4506 | spoe_release_placeholder(ph); |
| 4507 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4508 | list_for_each_entry_safe(vph, vphback, &curvars, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4509 | LIST_DELETE(&vph->list); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4510 | free(vph->name); |
| 4511 | free(vph); |
| 4512 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4513 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4514 | LIST_DELETE(&grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4515 | spoe_release_group(grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4516 | } |
| 4517 | list_for_each_entry_safe(msg, msgback, &curmsgs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4518 | LIST_DELETE(&msg->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4519 | spoe_release_message(msg); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4520 | } |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4521 | list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4522 | LIST_DELETE(&logsrv->list); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4523 | free(logsrv); |
| 4524 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4525 | free(conf); |
| 4526 | return -1; |
| 4527 | } |
| 4528 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4529 | /* Send message of a SPOE group. This is the action_ptr callback of a rule |
| 4530 | * associated to a "send-spoe-group" action. |
| 4531 | * |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4532 | * It returns ACT_RET_CONT if processing is finished (with error or not), it returns |
| 4533 | * ACT_RET_YIELD if the action is in progress. */ |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4534 | static enum act_return |
| 4535 | spoe_send_group(struct act_rule *rule, struct proxy *px, |
| 4536 | struct session *sess, struct stream *s, int flags) |
| 4537 | { |
| 4538 | struct filter *filter; |
| 4539 | struct spoe_agent *agent = NULL; |
| 4540 | struct spoe_group *group = NULL; |
| 4541 | struct spoe_context *ctx = NULL; |
| 4542 | int ret, dir; |
| 4543 | |
| 4544 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 4545 | if (filter->config == rule->arg.act.p[0]) { |
| 4546 | agent = rule->arg.act.p[2]; |
| 4547 | group = rule->arg.act.p[3]; |
| 4548 | ctx = filter->ctx; |
| 4549 | break; |
| 4550 | } |
| 4551 | } |
| 4552 | if (agent == NULL || group == NULL || ctx == NULL) |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4553 | return ACT_RET_CONT; |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4554 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 4555 | return ACT_RET_CONT; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4556 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4557 | switch (rule->from) { |
| 4558 | case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break; |
| 4559 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 4560 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 4561 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 4562 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
| 4563 | default: |
| 4564 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 4565 | " - internal error while execute spoe-send-group\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 4566 | (int)date.tv_sec, (int)date.tv_usec, agent->id, |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4567 | __FUNCTION__, s); |
| 4568 | send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n", |
| 4569 | agent->id); |
| 4570 | return ACT_RET_CONT; |
| 4571 | } |
| 4572 | |
| 4573 | ret = spoe_process_group(s, ctx, group, dir); |
| 4574 | if (ret == 1) |
| 4575 | return ACT_RET_CONT; |
| 4576 | else if (ret == 0) { |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 4577 | if (flags & ACT_OPT_FINAL) { |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4578 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 4579 | " - failed to process group '%s': interrupted by caller\n", |
Willy Tarreau | a5f0e6c | 2023-04-27 11:56:03 +0200 | [diff] [blame] | 4580 | (int)date.tv_sec, (int)date.tv_usec, |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4581 | agent->id, __FUNCTION__, s, group->id); |
| 4582 | ctx->status_code = SPOE_CTX_ERR_INTERRUPT; |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 4583 | spoe_stop_processing(agent, ctx); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 4584 | spoe_handle_processing_error(s, agent, ctx, dir); |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4585 | return ACT_RET_CONT; |
| 4586 | } |
| 4587 | return ACT_RET_YIELD; |
| 4588 | } |
| 4589 | else |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4590 | return ACT_RET_CONT; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4591 | } |
| 4592 | |
| 4593 | /* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE |
| 4594 | * group associated to <rule>. The format of an rule using 'send-spoe-group' |
| 4595 | * action should be: |
| 4596 | * |
| 4597 | * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id> |
| 4598 | * |
| 4599 | * So, we'll loop on each configured SPOE filter for the proxy <px> to find the |
| 4600 | * SPOE engine matching <engine-id>. And then, we'll try to find the good group |
| 4601 | * matching <group-id>. Finally, we'll check all messages referenced by the SPOE |
| 4602 | * group. |
| 4603 | * |
| 4604 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 4605 | * filled. |
| 4606 | */ |
| 4607 | static int |
| 4608 | check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err) |
| 4609 | { |
| 4610 | struct flt_conf *fconf; |
| 4611 | struct spoe_config *conf; |
| 4612 | struct spoe_agent *agent = NULL; |
| 4613 | struct spoe_group *group; |
| 4614 | struct spoe_message *msg; |
| 4615 | char *engine_id = rule->arg.act.p[0]; |
| 4616 | char *group_id = rule->arg.act.p[1]; |
| 4617 | unsigned int where = 0; |
| 4618 | |
| 4619 | switch (rule->from) { |
| 4620 | case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break; |
| 4621 | case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break; |
| 4622 | case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break; |
| 4623 | case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break; |
| 4624 | case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break; |
| 4625 | default: |
| 4626 | memprintf(err, |
| 4627 | "internal error, unexpected rule->from=%d, please report this bug!", |
| 4628 | rule->from); |
| 4629 | goto error; |
| 4630 | } |
| 4631 | |
| 4632 | /* Try to find the SPOE engine by checking all SPOE filters for proxy |
| 4633 | * <px> */ |
| 4634 | list_for_each_entry(fconf, &px->filter_configs, list) { |
| 4635 | conf = fconf->conf; |
| 4636 | |
| 4637 | /* This is not an SPOE filter */ |
| 4638 | if (fconf->id != spoe_filter_id) |
| 4639 | continue; |
| 4640 | |
| 4641 | /* This is the good engine */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4642 | if (strcmp(conf->id, engine_id) == 0) { |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4643 | agent = conf->agent; |
| 4644 | break; |
| 4645 | } |
| 4646 | } |
| 4647 | if (agent == NULL) { |
| 4648 | memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'", |
| 4649 | engine_id, group_id); |
| 4650 | goto error; |
| 4651 | } |
| 4652 | |
| 4653 | /* Try to find the right group */ |
| 4654 | list_for_each_entry(group, &agent->groups, list) { |
| 4655 | /* This is the good group */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4656 | if (strcmp(group->id, group_id) == 0) |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4657 | break; |
| 4658 | } |
| 4659 | if (&group->list == &agent->groups) { |
| 4660 | memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration", |
| 4661 | group_id, engine_id); |
| 4662 | goto error; |
| 4663 | } |
| 4664 | |
| 4665 | /* Ok, we found the group, we need to check messages and their |
| 4666 | * arguments */ |
| 4667 | list_for_each_entry(msg, &group->messages, by_grp) { |
| 4668 | struct spoe_arg *arg; |
| 4669 | |
| 4670 | list_for_each_entry(arg, &msg->args, list) { |
| 4671 | if (!(arg->expr->fetch->val & where)) { |
| 4672 | memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: " |
| 4673 | "some args extract information from '%s'," |
| 4674 | "none of which is available here ('%s')", |
| 4675 | msg->id, group->id, msg->conf.file, msg->conf.line, |
| 4676 | sample_ckp_names(arg->expr->fetch->use), |
| 4677 | sample_ckp_names(where)); |
| 4678 | goto error; |
| 4679 | } |
| 4680 | } |
| 4681 | } |
| 4682 | |
| 4683 | free(engine_id); |
| 4684 | free(group_id); |
| 4685 | rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */ |
| 4686 | rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */ |
| 4687 | rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */ |
| 4688 | rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */ |
| 4689 | return 1; |
| 4690 | |
| 4691 | error: |
| 4692 | free(engine_id); |
| 4693 | free(group_id); |
| 4694 | return 0; |
| 4695 | } |
| 4696 | |
| 4697 | /* Parse 'send-spoe-group' action following the format: |
| 4698 | * |
| 4699 | * ... send-spoe-group <engine-id> <group-id> |
| 4700 | * |
| 4701 | * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error |
| 4702 | * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group |
| 4703 | * ids are saved and used later, when the rule will be checked. |
| 4704 | */ |
| 4705 | static enum act_parse_ret |
| 4706 | parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px, |
| 4707 | struct act_rule *rule, char **err) |
| 4708 | { |
| 4709 | if (!*args[*orig_arg] || !*args[*orig_arg+1] || |
| 4710 | (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) { |
| 4711 | memprintf(err, "expects 2 arguments: <engine-id> <group-id>"); |
| 4712 | return ACT_RET_PRS_ERR; |
| 4713 | } |
| 4714 | rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */ |
| 4715 | rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */ |
| 4716 | |
| 4717 | (*orig_arg) += 2; |
| 4718 | |
| 4719 | rule->action = ACT_CUSTOM; |
| 4720 | rule->action_ptr = spoe_send_group; |
| 4721 | rule->check_ptr = check_send_spoe_group; |
| 4722 | return ACT_RET_PRS_OK; |
| 4723 | } |
| 4724 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4725 | |
| 4726 | /* Declare the filter parser for "spoe" keyword */ |
| 4727 | static struct flt_kw_list flt_kws = { "SPOE", { }, { |
| 4728 | { "spoe", parse_spoe_flt, NULL }, |
| 4729 | { NULL, NULL, NULL }, |
| 4730 | } |
| 4731 | }; |
| 4732 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4733 | INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws); |
| 4734 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4735 | /* Delcate the action parser for "spoe-action" keyword */ |
| 4736 | static struct action_kw_list tcp_req_action_kws = { { }, { |
| 4737 | { "send-spoe-group", parse_send_spoe_group }, |
| 4738 | { /* END */ }, |
| 4739 | } |
| 4740 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4741 | |
| 4742 | INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws); |
| 4743 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4744 | static struct action_kw_list tcp_res_action_kws = { { }, { |
| 4745 | { "send-spoe-group", parse_send_spoe_group }, |
| 4746 | { /* END */ }, |
| 4747 | } |
| 4748 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4749 | |
| 4750 | INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws); |
| 4751 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4752 | static struct action_kw_list http_req_action_kws = { { }, { |
| 4753 | { "send-spoe-group", parse_send_spoe_group }, |
| 4754 | { /* END */ }, |
| 4755 | } |
| 4756 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4757 | |
| 4758 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws); |
| 4759 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4760 | static struct action_kw_list http_res_action_kws = { { }, { |
| 4761 | { "send-spoe-group", parse_send_spoe_group }, |
| 4762 | { /* END */ }, |
| 4763 | } |
| 4764 | }; |
| 4765 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4766 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws); |