Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Stream processing offload engine management. |
| 3 | * |
| 4 | * Copyright 2016 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | #include <ctype.h> |
| 13 | #include <errno.h> |
| 14 | |
Willy Tarreau | dcc048a | 2020-06-04 19:11:43 +0200 | [diff] [blame] | 15 | #include <haproxy/acl.h> |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 16 | #include <haproxy/action-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 17 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 18 | #include <haproxy/arg.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 19 | #include <haproxy/cfgparse.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 20 | #include <haproxy/filters.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 21 | #include <haproxy/freq_ctr.h> |
Willy Tarreau | 762d7a5 | 2020-06-04 11:23:07 +0200 | [diff] [blame] | 22 | #include <haproxy/frontend.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 23 | #include <haproxy/global.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 24 | #include <haproxy/http_rules.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 25 | #include <haproxy/log.h> |
Willy Tarreau | d0ef439 | 2020-06-02 09:38:52 +0200 | [diff] [blame] | 26 | #include <haproxy/pool.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 27 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/sample.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 29 | #include <haproxy/session.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 30 | #include <haproxy/signal.h> |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 31 | #include <haproxy/sink.h> |
Willy Tarreau | 6c58ab0 | 2020-06-04 22:35:49 +0200 | [diff] [blame] | 32 | #include <haproxy/spoe.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 33 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 34 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 35 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 36 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 37 | #include <haproxy/thread.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 38 | #include <haproxy/time.h> |
Willy Tarreau | e16ada1 | 2021-05-08 12:57:17 +0200 | [diff] [blame] | 39 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 40 | #include <haproxy/vars.h> |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 41 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 42 | |
| 43 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 44 | #define SPOE_PRINTF(x...) fprintf(x) |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 45 | #define SPOE_DEBUG_STMT(statement) statement |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 46 | #else |
| 47 | #define SPOE_PRINTF(x...) |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 48 | #define SPOE_DEBUG_STMT(statement) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 49 | #endif |
| 50 | |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 51 | /* Reserved 4 bytes to the frame size. So a frame and its size can be written |
| 52 | * together in a buffer */ |
| 53 | #define MAX_FRAME_SIZE global.tune.bufsize - 4 |
| 54 | |
| 55 | /* The minimum size for a frame */ |
| 56 | #define MIN_FRAME_SIZE 256 |
| 57 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 58 | /* Reserved for the metadata and the frame type. |
| 59 | * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */ |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 60 | #define FRAME_HDR_SIZE 32 |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 61 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 62 | /* Helper to get SPOE ctx inside an appctx */ |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 63 | #define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr)) |
| 64 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 65 | /* SPOE filter id. Used to identify SPOE filters */ |
| 66 | const char *spoe_filter_id = "SPOE filter"; |
| 67 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 68 | /* Set if the handle on SIGUSR1 is registered */ |
| 69 | static int sighandler_registered = 0; |
| 70 | |
| 71 | /* proxy used during the parsing */ |
| 72 | struct proxy *curproxy = NULL; |
| 73 | |
| 74 | /* The name of the SPOE engine, used during the parsing */ |
| 75 | char *curengine = NULL; |
| 76 | |
| 77 | /* SPOE agent used during the parsing */ |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 78 | /* SPOE agent/group/message used during the parsing */ |
| 79 | struct spoe_agent *curagent = NULL; |
| 80 | struct spoe_group *curgrp = NULL; |
| 81 | struct spoe_message *curmsg = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 82 | |
| 83 | /* list of SPOE messages and placeholders used during the parsing */ |
| 84 | struct list curmsgs; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 85 | struct list curgrps; |
| 86 | struct list curmphs; |
| 87 | struct list curgphs; |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 88 | struct list curvars; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 89 | |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 90 | /* list of log servers used during the parsing */ |
| 91 | struct list curlogsrvs; |
| 92 | |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 93 | /* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */ |
| 94 | int curpxopts; |
| 95 | int curpxopts2; |
| 96 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 97 | /* Pools used to allocate SPOE structs */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 98 | DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context)); |
| 99 | DECLARE_STATIC_POOL(pool_head_spoe_appctx, "spoe_appctx", sizeof(struct spoe_appctx)); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 100 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 101 | struct flt_ops spoe_ops; |
| 102 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 103 | static int spoe_queue_context(struct spoe_context *ctx); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 104 | static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait); |
| 105 | static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 106 | |
| 107 | /******************************************************************** |
| 108 | * helper functions/globals |
| 109 | ********************************************************************/ |
| 110 | static void |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 111 | spoe_release_placeholder(struct spoe_placeholder *ph) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 112 | { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 113 | if (!ph) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 114 | return; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 115 | free(ph->id); |
| 116 | free(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 119 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 120 | spoe_release_message(struct spoe_message *msg) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 121 | { |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 122 | struct spoe_arg *arg, *argback; |
| 123 | struct acl *acl, *aclback; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 124 | |
| 125 | if (!msg) |
| 126 | return; |
| 127 | free(msg->id); |
| 128 | free(msg->conf.file); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 129 | list_for_each_entry_safe(arg, argback, &msg->args, list) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 130 | release_sample_expr(arg->expr); |
| 131 | free(arg->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 132 | LIST_DELETE(&arg->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 133 | free(arg); |
| 134 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 135 | list_for_each_entry_safe(acl, aclback, &msg->acls, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 136 | LIST_DELETE(&acl->list); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 137 | prune_acl(acl); |
| 138 | free(acl); |
| 139 | } |
| 140 | if (msg->cond) { |
| 141 | prune_acl_cond(msg->cond); |
| 142 | free(msg->cond); |
| 143 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 144 | free(msg); |
| 145 | } |
| 146 | |
| 147 | static void |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 148 | spoe_release_group(struct spoe_group *grp) |
| 149 | { |
| 150 | if (!grp) |
| 151 | return; |
| 152 | free(grp->id); |
| 153 | free(grp->conf.file); |
| 154 | free(grp); |
| 155 | } |
| 156 | |
| 157 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 158 | spoe_release_agent(struct spoe_agent *agent) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 159 | { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 160 | struct spoe_message *msg, *msgback; |
| 161 | struct spoe_group *grp, *grpback; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 162 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 163 | |
| 164 | if (!agent) |
| 165 | return; |
| 166 | free(agent->id); |
| 167 | free(agent->conf.file); |
| 168 | free(agent->var_pfx); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 169 | free(agent->var_on_error); |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 170 | free(agent->var_t_process); |
| 171 | free(agent->var_t_total); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 172 | list_for_each_entry_safe(msg, msgback, &agent->messages, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 173 | LIST_DELETE(&msg->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 174 | spoe_release_message(msg); |
| 175 | } |
| 176 | list_for_each_entry_safe(grp, grpback, &agent->groups, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 177 | LIST_DELETE(&grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 178 | spoe_release_group(grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 179 | } |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 180 | if (agent->rt) { |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 181 | for (i = 0; i < global.nbthread; ++i) { |
| 182 | free(agent->rt[i].engine_id); |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 183 | HA_SPIN_DESTROY(&agent->rt[i].lock); |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 184 | } |
Willy Tarreau | 3ddcf76 | 2019-02-07 14:22:52 +0100 | [diff] [blame] | 185 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 186 | free(agent->rt); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 187 | free(agent); |
| 188 | } |
| 189 | |
| 190 | static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 191 | [SPOE_FRM_ERR_NONE] = "normal", |
| 192 | [SPOE_FRM_ERR_IO] = "I/O error", |
| 193 | [SPOE_FRM_ERR_TOUT] = "a timeout occurred", |
| 194 | [SPOE_FRM_ERR_TOO_BIG] = "frame is too big", |
| 195 | [SPOE_FRM_ERR_INVALID] = "invalid frame received", |
| 196 | [SPOE_FRM_ERR_NO_VSN] = "version value not found", |
| 197 | [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found", |
| 198 | [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found", |
| 199 | [SPOE_FRM_ERR_BAD_VSN] = "unsupported version", |
| 200 | [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small", |
| 201 | [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported", |
| 202 | [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames", |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 203 | [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 204 | [SPOE_FRM_ERR_RES] = "resource allocation error", |
| 205 | [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | static const char *spoe_event_str[SPOE_EV_EVENTS] = { |
| 209 | [SPOE_EV_ON_CLIENT_SESS] = "on-client-session", |
| 210 | [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request", |
| 211 | [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request", |
| 212 | [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request", |
| 213 | [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request", |
| 214 | |
| 215 | [SPOE_EV_ON_SERVER_SESS] = "on-server-session", |
| 216 | [SPOE_EV_ON_TCP_RSP] = "on-tcp-response", |
| 217 | [SPOE_EV_ON_HTTP_RSP] = "on-http-response", |
| 218 | }; |
| 219 | |
| 220 | |
| 221 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 222 | |
| 223 | static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 224 | [SPOE_CTX_ST_NONE] = "NONE", |
| 225 | [SPOE_CTX_ST_READY] = "READY", |
| 226 | [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS", |
| 227 | [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS", |
| 228 | [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK", |
| 229 | [SPOE_CTX_ST_DONE] = "DONE", |
| 230 | [SPOE_CTX_ST_ERROR] = "ERROR", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 234 | [SPOE_APPCTX_ST_CONNECT] = "CONNECT", |
| 235 | [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING", |
| 236 | [SPOE_APPCTX_ST_IDLE] = "IDLE", |
| 237 | [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING", |
| 238 | [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY", |
| 239 | [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK", |
| 240 | [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT", |
| 241 | [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING", |
| 242 | [SPOE_APPCTX_ST_EXIT] = "EXIT", |
| 243 | [SPOE_APPCTX_ST_END] = "END", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | #endif |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 247 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 248 | /* 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] | 249 | * 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] | 250 | * allocation failed, it returns NULL. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 251 | static char * |
| 252 | generate_pseudo_uuid() |
| 253 | { |
Willy Tarreau | ee3bcdd | 2020-03-08 17:48:17 +0100 | [diff] [blame] | 254 | ha_generate_uuid(&trash); |
Kevin Zhu | 079f808 | 2020-03-13 10:39:51 +0800 | [diff] [blame] | 255 | return my_strndup(trash.area, trash.data); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 258 | |
| 259 | static inline void |
| 260 | spoe_update_stat_time(struct timeval *tv, long *t) |
| 261 | { |
| 262 | if (*t == -1) |
| 263 | *t = tv_ms_elapsed(tv, &now); |
| 264 | else |
| 265 | *t += tv_ms_elapsed(tv, &now); |
| 266 | tv_zero(tv); |
| 267 | } |
| 268 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 269 | /******************************************************************** |
| 270 | * Functions that encode/decode SPOE frames |
| 271 | ********************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 272 | /* Helper to get static string length, excluding the terminating null byte */ |
| 273 | #define SLEN(str) (sizeof(str)-1) |
| 274 | |
| 275 | /* Predefined key used in HELLO/DISCONNECT frames */ |
| 276 | #define SUPPORTED_VERSIONS_KEY "supported-versions" |
| 277 | #define VERSION_KEY "version" |
| 278 | #define MAX_FRAME_SIZE_KEY "max-frame-size" |
| 279 | #define CAPABILITIES_KEY "capabilities" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 280 | #define ENGINE_ID_KEY "engine-id" |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 281 | #define HEALTHCHECK_KEY "healthcheck" |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 282 | #define STATUS_CODE_KEY "status-code" |
| 283 | #define MSG_KEY "message" |
| 284 | |
| 285 | struct spoe_version { |
| 286 | char *str; |
| 287 | int min; |
| 288 | int max; |
| 289 | }; |
| 290 | |
| 291 | /* All supported versions */ |
| 292 | static struct spoe_version supported_versions[] = { |
Christopher Faulet | 6381650 | 2018-05-31 14:56:42 +0200 | [diff] [blame] | 293 | /* 1.0 is now unsupported because of a bug about frame's flags*/ |
| 294 | {"2.0", 2000, 2000}, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 295 | {NULL, 0, 0} |
| 296 | }; |
| 297 | |
| 298 | /* Comma-separated list of supported versions */ |
Christopher Faulet | 6381650 | 2018-05-31 14:56:42 +0200 | [diff] [blame] | 299 | #define SUPPORTED_VERSIONS_VAL "2.0" |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 300 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 301 | /* Convert a string to a SPOE version value. The string must follow the format |
| 302 | * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR). |
| 303 | * If an error occurred, -1 is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 304 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 305 | spoe_str_to_vsn(const char *str, size_t len) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 306 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 307 | const char *p, *end; |
| 308 | int maj, min, vsn; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 309 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 310 | p = str; |
| 311 | end = str+len; |
| 312 | maj = min = 0; |
| 313 | vsn = -1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 314 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 315 | /* skip leading spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 316 | while (p < end && isspace((unsigned char)*p)) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 317 | p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 318 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 319 | /* parse Major number, until the '.' */ |
| 320 | while (*p != '.') { |
| 321 | if (p >= end || *p < '0' || *p > '9') |
| 322 | goto out; |
| 323 | maj *= 10; |
| 324 | maj += (*p - '0'); |
| 325 | p++; |
| 326 | } |
| 327 | |
| 328 | /* check Major version */ |
| 329 | if (!maj) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 330 | goto out; |
| 331 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 332 | p++; /* skip the '.' */ |
| 333 | if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */ |
| 334 | goto out; |
| 335 | |
| 336 | /* Parse Minor number */ |
| 337 | while (p < end) { |
| 338 | if (*p < '0' || *p > '9') |
| 339 | break; |
| 340 | min *= 10; |
| 341 | min += (*p - '0'); |
| 342 | p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 343 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 344 | |
| 345 | /* check Minor number */ |
| 346 | if (min > 999) |
| 347 | goto out; |
| 348 | |
| 349 | /* skip trailing spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 350 | while (p < end && isspace((unsigned char)*p)) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 351 | p++; |
| 352 | if (p != end) |
| 353 | goto out; |
| 354 | |
| 355 | vsn = maj * 1000 + min; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 356 | out: |
| 357 | return vsn; |
| 358 | } |
| 359 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 360 | /* 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] | 361 | * 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] | 362 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 363 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 364 | spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 365 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 366 | struct buffer *chk; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 367 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 368 | char *p, *end; |
| 369 | unsigned int flags = SPOE_FRM_FL_FIN; |
| 370 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 371 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 372 | p = frame; |
| 373 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 374 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 375 | /* Set Frame type */ |
| 376 | *p++ = SPOE_FRM_T_HAPROXY_HELLO; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 377 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 378 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 379 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 380 | memcpy(p, (char *)&flags, 4); |
| 381 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 382 | |
| 383 | /* No stream-id and frame-id for HELLO frames */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 384 | *p++ = 0; *p++ = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 385 | |
| 386 | /* There are 3 mandatory items: "supported-versions", "max-frame-size" |
| 387 | * and "capabilities" */ |
| 388 | |
| 389 | /* "supported-versions" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 390 | sz = SLEN(SUPPORTED_VERSIONS_KEY); |
| 391 | if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1) |
| 392 | goto too_big; |
| 393 | |
| 394 | *p++ = SPOE_DATA_T_STR; |
| 395 | sz = SLEN(SUPPORTED_VERSIONS_VAL); |
| 396 | if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1) |
| 397 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 398 | |
| 399 | /* "max-fram-size" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 400 | sz = SLEN(MAX_FRAME_SIZE_KEY); |
| 401 | if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1) |
| 402 | goto too_big; |
| 403 | |
| 404 | *p++ = SPOE_DATA_T_UINT32; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 405 | if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 406 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 407 | |
| 408 | /* "capabilities" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 409 | sz = SLEN(CAPABILITIES_KEY); |
| 410 | if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1) |
| 411 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 412 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 413 | *p++ = SPOE_DATA_T_STR; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 414 | chk = get_trash_chunk(); |
| 415 | if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 416 | memcpy(chk->area, "pipelining", 10); |
| 417 | chk->data += 10; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 418 | } |
| 419 | if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 420 | if (chk->data) chk->area[chk->data++] = ','; |
| 421 | memcpy(chk->area+chk->data, "async", 5); |
| 422 | chk->data += 5; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 423 | } |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 424 | if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 425 | if (chk->data) chk->area[chk->data++] = ','; |
| 426 | memcpy(chk->area+chk->data, "fragmentation", 13); |
Miroslav Zagorac | 6b3690b | 2019-01-13 16:55:01 +0100 | [diff] [blame] | 427 | chk->data += 13; |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 428 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 429 | if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 430 | goto too_big; |
| 431 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 432 | /* (optional) "engine-id" K/V item, if present */ |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 433 | if (agent != NULL && agent->rt[tid].engine_id != NULL) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 434 | sz = SLEN(ENGINE_ID_KEY); |
| 435 | if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1) |
| 436 | goto too_big; |
| 437 | |
| 438 | *p++ = SPOE_DATA_T_STR; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 439 | sz = strlen(agent->rt[tid].engine_id); |
| 440 | 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] | 441 | goto too_big; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 442 | } |
| 443 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 444 | return (p - frame); |
| 445 | |
| 446 | too_big: |
| 447 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 448 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 449 | } |
| 450 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 451 | /* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of |
| 452 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
| 453 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 454 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 455 | spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 456 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 457 | const char *reason; |
| 458 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 459 | unsigned int flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 460 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 461 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 462 | p = frame; |
| 463 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 464 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 465 | /* Set Frame type */ |
| 466 | *p++ = SPOE_FRM_T_HAPROXY_DISCON; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 467 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 468 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 469 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 470 | memcpy(p, (char *)&flags, 4); |
| 471 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 472 | |
| 473 | /* No stream-id and frame-id for DISCONNECT frames */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 474 | *p++ = 0; *p++ = 0; |
| 475 | |
| 476 | if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS) |
| 477 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 478 | |
| 479 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 480 | |
| 481 | /* "status-code" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 482 | sz = SLEN(STATUS_CODE_KEY); |
| 483 | if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1) |
| 484 | goto too_big; |
| 485 | |
| 486 | *p++ = SPOE_DATA_T_UINT32; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 487 | if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 488 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 489 | |
| 490 | /* "message" K/V item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 491 | sz = SLEN(MSG_KEY); |
| 492 | if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1) |
| 493 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 494 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 495 | /*Get the message corresponding to the status code */ |
| 496 | reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]; |
| 497 | |
| 498 | *p++ = SPOE_DATA_T_STR; |
| 499 | sz = strlen(reason); |
| 500 | if (spoe_encode_buffer(reason, sz, &p, end) == -1) |
| 501 | goto too_big; |
| 502 | |
| 503 | return (p - frame); |
| 504 | |
| 505 | too_big: |
| 506 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 507 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 508 | } |
| 509 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 510 | /* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of |
| 511 | * encoded bytes in the frame on success, 0 if an encoding error occurred and -1 |
| 512 | * if a fatal error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 513 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 514 | spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 515 | char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 516 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 517 | char *p, *end; |
| 518 | unsigned int stream_id, frame_id; |
| 519 | unsigned int flags = SPOE_FRM_FL_FIN; |
| 520 | size_t sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 521 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 522 | p = frame; |
| 523 | end = frame+size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 524 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 525 | stream_id = ctx->stream_id; |
| 526 | frame_id = ctx->frame_id; |
| 527 | |
| 528 | if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) { |
| 529 | /* The fragmentation is not supported by the applet */ |
| 530 | if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) { |
| 531 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
| 532 | return -1; |
| 533 | } |
| 534 | flags = ctx->frag_ctx.flags; |
| 535 | } |
| 536 | |
| 537 | /* Set Frame type */ |
| 538 | *p++ = SPOE_FRM_T_HAPROXY_NOTIFY; |
| 539 | |
| 540 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 541 | flags = htonl(flags); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 542 | memcpy(p, (char *)&flags, 4); |
| 543 | p += 4; |
| 544 | |
| 545 | /* Set stream-id and frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 546 | if (encode_varint(stream_id, &p, end) == -1) |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 547 | goto too_big; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 548 | if (encode_varint(frame_id, &p, end) == -1) |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 549 | goto too_big; |
| 550 | |
| 551 | /* Copy encoded messages, if possible */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 552 | sz = b_data(&ctx->buffer); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 553 | if (p + sz >= end) |
| 554 | goto too_big; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 555 | memcpy(p, b_head(&ctx->buffer), sz); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 556 | p += sz; |
| 557 | |
| 558 | return (p - frame); |
| 559 | |
| 560 | too_big: |
| 561 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | /* Encode next part of a fragmented frame sent by HAProxy to an agent. It |
| 566 | * returns the number of encoded bytes in the frame on success, 0 if an encoding |
| 567 | * error occurred and -1 if a fatal error occurred. */ |
| 568 | static int |
| 569 | spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx, |
| 570 | char *frame, size_t size) |
| 571 | { |
| 572 | char *p, *end; |
| 573 | unsigned int stream_id, frame_id; |
| 574 | unsigned int flags; |
| 575 | size_t sz; |
| 576 | |
| 577 | p = frame; |
| 578 | end = frame+size; |
| 579 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 580 | /* <ctx> is null when the stream has aborted the processing of a |
| 581 | * fragmented frame. In this case, we must notify the corresponding |
| 582 | * agent using ids stored in <frag_ctx>. */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 583 | if (ctx == NULL) { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 584 | flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 585 | stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid; |
| 586 | frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid; |
| 587 | } |
| 588 | else { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 589 | flags = ctx->frag_ctx.flags; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 590 | stream_id = ctx->stream_id; |
| 591 | frame_id = ctx->frame_id; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 592 | } |
| 593 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 594 | /* Set Frame type */ |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 595 | *p++ = SPOE_FRM_T_UNSET; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 596 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 597 | /* Set flags */ |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 598 | flags = htonl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 599 | memcpy(p, (char *)&flags, 4); |
| 600 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 601 | |
| 602 | /* Set stream-id and frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 603 | if (encode_varint(stream_id, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 604 | goto too_big; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 605 | if (encode_varint(frame_id, &p, end) == -1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 606 | goto too_big; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 607 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 608 | if (ctx == NULL) |
| 609 | goto end; |
| 610 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 611 | /* Copy encoded messages, if possible */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 612 | sz = b_data(&ctx->buffer); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 613 | if (p + sz >= end) |
| 614 | goto too_big; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 615 | memcpy(p, b_head(&ctx->buffer), sz); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 616 | p += sz; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 617 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 618 | end: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 619 | return (p - frame); |
| 620 | |
| 621 | too_big: |
| 622 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
| 623 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 624 | } |
| 625 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 626 | /* Decode and process the HELLO frame sent by an agent. It returns the number of |
| 627 | * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal |
| 628 | * error occurred. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 629 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 630 | spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 631 | { |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 632 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
| 633 | char *p, *end; |
| 634 | int vsn, max_frame_size; |
| 635 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 636 | |
| 637 | p = frame; |
| 638 | end = frame + size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 639 | |
| 640 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 641 | if (*p++ != SPOE_FRM_T_AGENT_HELLO) { |
| 642 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 643 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 644 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 645 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 646 | if (size < 7 /* TYPE + METADATA */) { |
| 647 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 648 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 649 | } |
| 650 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 651 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 652 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 653 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 654 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 655 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 656 | /* Fragmentation is not supported for HELLO frame */ |
| 657 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 658 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 659 | return -1; |
| 660 | } |
| 661 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 662 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 663 | if (*p != 0 || *(p+1) != 0) { |
| 664 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 665 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 666 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 667 | p += 2; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 668 | |
| 669 | /* There are 3 mandatory items: "version", "max-frame-size" and |
| 670 | * "capabilities" */ |
| 671 | |
| 672 | /* Loop on K/V items */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 673 | vsn = max_frame_size = flags = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 674 | while (p < end) { |
| 675 | char *str; |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 676 | uint64_t sz; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 677 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 678 | |
| 679 | /* Decode the item key */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 680 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 681 | if (ret == -1 || !sz) { |
| 682 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 683 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 684 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 685 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 686 | /* Check "version" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 687 | if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 688 | int i, type = *p++; |
| 689 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 690 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 691 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 692 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 693 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 694 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 695 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) { |
| 696 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 697 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 698 | } |
| 699 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 700 | vsn = spoe_str_to_vsn(str, sz); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 701 | if (vsn == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 702 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 703 | return -1; |
| 704 | } |
| 705 | for (i = 0; supported_versions[i].str != NULL; ++i) { |
| 706 | if (vsn >= supported_versions[i].min && |
| 707 | vsn <= supported_versions[i].max) |
| 708 | break; |
| 709 | } |
| 710 | if (supported_versions[i].str == NULL) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 711 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 712 | return -1; |
| 713 | } |
| 714 | } |
| 715 | /* Check "max-frame-size" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 716 | 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] | 717 | int type = *p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 718 | |
| 719 | /* The value must be integer */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 720 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 721 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 722 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 723 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 724 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 725 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 726 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 727 | if (decode_varint(&p, end, &sz) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 728 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 729 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 730 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 731 | if (sz < MIN_FRAME_SIZE || |
| 732 | sz > SPOE_APPCTX(appctx)->max_frame_size) { |
| 733 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 734 | return -1; |
| 735 | } |
| 736 | max_frame_size = sz; |
| 737 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 738 | /* Check "capabilities" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 739 | 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] | 740 | int type = *p++; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 741 | |
| 742 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 743 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 744 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 745 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 746 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 747 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) { |
| 748 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 749 | return 0; |
| 750 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 751 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 752 | while (sz) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 753 | char *delim; |
| 754 | |
| 755 | /* Skip leading spaces */ |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 756 | for (; isspace((unsigned char)*str) && sz; str++, sz--); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 757 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 758 | if (sz >= 10 && !strncmp(str, "pipelining", 10)) { |
| 759 | str += 10; sz -= 10; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 760 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 761 | flags |= SPOE_APPCTX_FL_PIPELINING; |
| 762 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 763 | else if (sz >= 5 && !strncmp(str, "async", 5)) { |
| 764 | str += 5; sz -= 5; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 765 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 766 | flags |= SPOE_APPCTX_FL_ASYNC; |
| 767 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 768 | else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) { |
| 769 | str += 13; sz -= 13; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 770 | if (!sz || isspace((unsigned char)*str) || *str == ',') |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 771 | flags |= SPOE_APPCTX_FL_FRAGMENTATION; |
| 772 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 773 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 774 | /* Get the next comma or break */ |
| 775 | if (!sz || (delim = memchr(str, ',', sz)) == NULL) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 776 | break; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 777 | delim++; |
| 778 | sz -= (delim - str); |
| 779 | str = delim; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 780 | } |
| 781 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 782 | else { |
| 783 | /* Silently ignore unknown item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 784 | if (spoe_skip_data(&p, end) == -1) { |
| 785 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 786 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 787 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | |
| 791 | /* Final checks */ |
| 792 | if (!vsn) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 793 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 794 | return -1; |
| 795 | } |
| 796 | if (!max_frame_size) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 797 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 798 | return -1; |
| 799 | } |
Christopher Faulet | 1138901 | 2019-02-07 16:13:26 +0100 | [diff] [blame] | 800 | if (!agent) |
| 801 | flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC); |
| 802 | else { |
| 803 | if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING)) |
| 804 | flags &= ~SPOE_APPCTX_FL_PIPELINING; |
| 805 | if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC)) |
| 806 | flags &= ~SPOE_APPCTX_FL_ASYNC; |
| 807 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 808 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 809 | SPOE_APPCTX(appctx)->version = (unsigned int)vsn; |
| 810 | SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size; |
| 811 | SPOE_APPCTX(appctx)->flags |= flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 812 | |
| 813 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | /* Decode DISCONNECT frame sent by an agent. It returns the number of by read |
| 817 | * bytes on success, 0 if the frame can be ignored and -1 if an error |
| 818 | * occurred. */ |
| 819 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 820 | spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 821 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 822 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 823 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 824 | |
| 825 | p = frame; |
| 826 | end = frame + size; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 827 | |
| 828 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 829 | if (*p++ != SPOE_FRM_T_AGENT_DISCON) { |
| 830 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 831 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 832 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 833 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 834 | if (size < 7 /* TYPE + METADATA */) { |
| 835 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 836 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 837 | } |
| 838 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 839 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 840 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 841 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 842 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 843 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 844 | /* Fragmentation is not supported for DISCONNECT frame */ |
| 845 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 846 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 847 | return -1; |
| 848 | } |
| 849 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 850 | /* stream-id and frame-id must be cleared */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 851 | if (*p != 0 || *(p+1) != 0) { |
| 852 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 853 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 854 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 855 | p += 2; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 856 | |
| 857 | /* There are 2 mandatory items: "status-code" and "message" */ |
| 858 | |
| 859 | /* Loop on K/V items */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 860 | while (p < end) { |
| 861 | char *str; |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 862 | uint64_t sz; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 863 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 864 | |
| 865 | /* Decode the item key */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 866 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 867 | if (ret == -1 || !sz) { |
| 868 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 869 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | /* Check "status-code" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 873 | 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] | 874 | int type = *p++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 875 | |
| 876 | /* The value must be an integer */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 877 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 && |
| 878 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 && |
| 879 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 && |
| 880 | (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 881 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 882 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 883 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 884 | if (decode_varint(&p, end, &sz) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 885 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 886 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 887 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 888 | SPOE_APPCTX(appctx)->status_code = sz; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | /* Check "message" K/V item */ |
Willy Tarreau | da21ed1 | 2020-06-16 17:58:14 +0200 | [diff] [blame] | 892 | 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] | 893 | int type = *p++; |
| 894 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 895 | /* The value must be a string */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 896 | if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) { |
| 897 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 898 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 899 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 900 | ret = spoe_decode_buffer(&p, end, &str, &sz); |
| 901 | if (ret == -1 || sz > 255) { |
| 902 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 903 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 904 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 905 | #if defined(DEBUG_SPOE) || defined(DEBUG_FULL) |
| 906 | SPOE_APPCTX(appctx)->reason = str; |
| 907 | SPOE_APPCTX(appctx)->rlen = sz; |
| 908 | #endif |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 909 | } |
| 910 | else { |
| 911 | /* Silently ignore unknown item */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 912 | if (spoe_skip_data(&p, end) == -1) { |
| 913 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 914 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 915 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 919 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 923 | /* 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] | 924 | * success, 0 if the frame can be ignored and -1 if an error occurred. */ |
| 925 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 926 | spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 927 | char *frame, size_t size) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 928 | { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 929 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 930 | char *p, *end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 931 | uint64_t stream_id, frame_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 932 | int len; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 933 | unsigned int flags; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 934 | |
| 935 | p = frame; |
| 936 | end = frame + size; |
| 937 | *ctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 938 | |
| 939 | /* Check frame type */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 940 | if (*p++ != SPOE_FRM_T_AGENT_ACK) { |
| 941 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 942 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 943 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 944 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 945 | if (size < 7 /* TYPE + METADATA */) { |
| 946 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 947 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 948 | } |
| 949 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 950 | /* Retrieve flags */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 951 | memcpy((char *)&flags, p, 4); |
Thierry FOURNIER | c4dcaff | 2018-05-18 12:25:39 +0200 | [diff] [blame] | 952 | flags = ntohl(flags); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 953 | p += 4; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 954 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 955 | /* Fragmentation is not supported for now */ |
| 956 | if (!(flags & SPOE_FRM_FL_FIN)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 957 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 958 | return -1; |
| 959 | } |
| 960 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 961 | /* Get the stream-id and the frame-id */ |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 962 | if (decode_varint(&p, end, &stream_id) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 963 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 964 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 965 | } |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 966 | if (decode_varint(&p, end, &frame_id) == -1) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 967 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 968 | return 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 969 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 970 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 971 | /* Try to find the corresponding SPOE context */ |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 972 | if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 973 | list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 974 | if ((*ctx)->stream_id == (unsigned int)stream_id && |
| 975 | (*ctx)->frame_id == (unsigned int)frame_id) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 976 | goto found; |
| 977 | } |
| 978 | } |
| 979 | else { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 980 | list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) { |
| 981 | if ((*ctx)->stream_id == (unsigned int)stream_id && |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 982 | (*ctx)->frame_id == (unsigned int)frame_id) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 983 | goto found; |
| 984 | } |
| 985 | } |
| 986 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 987 | if (SPOE_APPCTX(appctx)->frag_ctx.ctx && |
| 988 | SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id && |
| 989 | SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) { |
| 990 | |
| 991 | /* ABRT bit is set for an unfinished fragmented frame */ |
| 992 | if (flags & SPOE_FRM_FL_ABRT) { |
| 993 | *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx; |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 994 | (*ctx)->state = SPOE_CTX_ST_ERROR; |
| 995 | (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT; |
| 996 | /* Ignore the payload */ |
| 997 | goto end; |
| 998 | } |
| 999 | /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */ |
| 1000 | /* For now, we ignore the ack */ |
| 1001 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID; |
| 1002 | return 0; |
| 1003 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1004 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1005 | /* No Stream found, ignore the frame */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1006 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1007 | " - Ignore ACK frame" |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1008 | " - stream-id=%u - frame-id=%u\n", |
| 1009 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1010 | __FUNCTION__, appctx, |
| 1011 | (unsigned int)stream_id, (unsigned int)frame_id); |
| 1012 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1013 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND; |
Christopher Faulet | c7ba910 | 2020-11-10 14:31:39 +0100 | [diff] [blame] | 1014 | if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) { |
| 1015 | /* Report an error if we are waiting the ack for another frame, |
| 1016 | * but not if there is no longer frame waiting for a ack |
| 1017 | * (timeout) |
| 1018 | */ |
| 1019 | if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) || |
| 1020 | SPOE_APPCTX(appctx)->frag_ctx.ctx) |
| 1021 | return -1; |
| 1022 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1023 | SPOE_APPCTX(appctx)->cur_fpa = 0; |
| 1024 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1025 | return 0; |
| 1026 | |
| 1027 | found: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1028 | if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer, |
| 1029 | &SPOE_APPCTX(appctx)->buffer_wait)) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1030 | *ctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1031 | return 1; /* Retry later */ |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1032 | } |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1033 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1034 | /* Copy encoded actions */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1035 | len = (end - p); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1036 | memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len); |
| 1037 | b_set_data(&SPOE_APPCTX(appctx)->buffer, len); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1038 | p += len; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1039 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1040 | /* Transfer the buffer ownership to the SPOE context */ |
| 1041 | (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1042 | SPOE_APPCTX(appctx)->buffer = BUF_NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1043 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1044 | (*ctx)->state = SPOE_CTX_ST_DONE; |
| 1045 | |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1046 | end: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1047 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1048 | " - ACK frame received" |
| 1049 | " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1050 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1051 | __FUNCTION__, appctx, *ctx, (*ctx)->stream_id, |
| 1052 | (*ctx)->frame_id, flags); |
| 1053 | return (p - frame); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1054 | } |
| 1055 | |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1056 | /* This function is used in cfgparse.c and declared in proto/checks.h. It |
| 1057 | * prepare the request to send to agents during a healthcheck. It returns 0 on |
| 1058 | * success and -1 if an error occurred. */ |
| 1059 | int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1060 | spoe_prepare_healthcheck_request(char **req, int *len) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1061 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1062 | struct appctx appctx; |
| 1063 | struct spoe_appctx spoe_appctx; |
| 1064 | char *frame, *end, buf[MAX_FRAME_SIZE+4]; |
| 1065 | size_t sz; |
| 1066 | int ret; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1067 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1068 | memset(&appctx, 0, sizeof(appctx)); |
| 1069 | memset(&spoe_appctx, 0, sizeof(spoe_appctx)); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1070 | memset(buf, 0, sizeof(buf)); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1071 | |
| 1072 | appctx.ctx.spoe.ptr = &spoe_appctx; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1073 | SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1074 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1075 | frame = buf+4; /* Reserved the 4 first bytes for the frame size */ |
| 1076 | end = frame + MAX_FRAME_SIZE; |
| 1077 | |
| 1078 | ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE); |
| 1079 | if (ret <= 0) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1080 | return -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1081 | frame += ret; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1082 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1083 | /* Add "healthcheck" K/V item */ |
| 1084 | sz = SLEN(HEALTHCHECK_KEY); |
| 1085 | if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1) |
| 1086 | return -1; |
| 1087 | *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1088 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1089 | *len = frame - buf; |
| 1090 | sz = htonl(*len - 4); |
| 1091 | memcpy(buf, (char *)&sz, 4); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1092 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1093 | if ((*req = malloc(*len)) == NULL) |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1094 | return -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1095 | memcpy(*req, buf, *len); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | /* This function is used in checks.c and declared in proto/checks.h. It decode |
| 1100 | * the response received from an agent during a healthcheck. It returns 0 on |
| 1101 | * success and -1 if an error occurred. */ |
| 1102 | int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1103 | 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] | 1104 | { |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1105 | struct appctx appctx; |
| 1106 | struct spoe_appctx spoe_appctx; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1107 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1108 | memset(&appctx, 0, sizeof(appctx)); |
| 1109 | memset(&spoe_appctx, 0, sizeof(spoe_appctx)); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1110 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1111 | appctx.ctx.spoe.ptr = &spoe_appctx; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1112 | SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1113 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1114 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1115 | spoe_handle_agentdiscon_frame(&appctx, frame, size); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1116 | goto error; |
| 1117 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1118 | if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0) |
| 1119 | goto error; |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1120 | |
| 1121 | return 0; |
| 1122 | |
| 1123 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1124 | if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS) |
| 1125 | SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN; |
| 1126 | strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen); |
Christopher Faulet | ba7bc16 | 2016-11-07 21:07:38 +0100 | [diff] [blame] | 1127 | return -1; |
| 1128 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1129 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1130 | /* 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] | 1131 | * 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] | 1132 | * success. */ |
| 1133 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1134 | spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1135 | { |
| 1136 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1137 | int ret; |
| 1138 | uint32_t netint; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1139 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1140 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1141 | * length. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1142 | netint = htonl(framesz); |
| 1143 | memcpy(buf, (char *)&netint, 4); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1144 | ret = ci_putblk(si_ic(si), buf, framesz+4); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1145 | if (ret <= 0) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1146 | if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 1147 | si_rx_room_blk(si); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1148 | return 1; /* retry */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1149 | } |
| 1150 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1151 | return -1; /* error */ |
| 1152 | } |
| 1153 | return framesz; |
| 1154 | } |
| 1155 | |
| 1156 | /* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0 |
| 1157 | * when the frame can be ignored, 1 to retry later and the frame length on |
| 1158 | * success. */ |
| 1159 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1160 | spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1161 | { |
| 1162 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1163 | int ret; |
| 1164 | uint32_t netint; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1165 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1166 | ret = co_getblk(si_oc(si), (char *)&netint, 4, 0); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1167 | if (ret > 0) { |
| 1168 | framesz = ntohl(netint); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1169 | if (framesz > SPOE_APPCTX(appctx)->max_frame_size) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1170 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1171 | return -1; |
| 1172 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1173 | ret = co_getblk(si_oc(si), buf, framesz, 4); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1174 | } |
| 1175 | if (ret <= 0) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1176 | if (ret == 0) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1177 | return 1; /* retry */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1178 | } |
| 1179 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1180 | return -1; /* error */ |
| 1181 | } |
| 1182 | return framesz; |
| 1183 | } |
| 1184 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1185 | /******************************************************************** |
| 1186 | * Functions that manage the SPOE applet |
| 1187 | ********************************************************************/ |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1188 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1189 | spoe_wakeup_appctx(struct appctx *appctx) |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1190 | { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1191 | si_want_get(appctx->owner); |
Willy Tarreau | 8bb2ffb | 2018-11-14 17:54:13 +0100 | [diff] [blame] | 1192 | si_rx_endp_more(appctx->owner); |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1193 | appctx_wakeup(appctx); |
| 1194 | return 1; |
| 1195 | } |
| 1196 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1197 | /* Callback function that catches applet timeouts. If a timeout occurred, we set |
| 1198 | * <appctx->st1> flag and the SPOE applet is woken up. */ |
| 1199 | static struct task * |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1200 | spoe_process_appctx(struct task * task, void *context, unsigned int state) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1201 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 1202 | struct appctx *appctx = context; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1203 | |
| 1204 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1205 | if (tick_is_expired(task->expire, now_ms)) { |
| 1206 | task->expire = TICK_ETERNITY; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1207 | appctx->st1 = SPOE_APPCTX_ERR_TOUT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1208 | } |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1209 | spoe_wakeup_appctx(appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1210 | return task; |
| 1211 | } |
| 1212 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1213 | /* Callback function that releases a SPOE applet. This happens when the |
| 1214 | * connection with the agent is closed. */ |
| 1215 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1216 | spoe_release_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1217 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1218 | struct stream_interface *si = appctx->owner; |
| 1219 | struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx); |
| 1220 | struct spoe_agent *agent; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1221 | struct spoe_context *ctx, *back; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1222 | |
| 1223 | if (spoe_appctx == NULL) |
| 1224 | return; |
| 1225 | |
| 1226 | appctx->ctx.spoe.ptr = NULL; |
| 1227 | agent = spoe_appctx->agent; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1228 | |
| 1229 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n", |
| 1230 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1231 | __FUNCTION__, appctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1232 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1233 | /* Remove applet from the list of running applets */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1234 | _HA_ATOMIC_DEC(&agent->counters.applets); |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1235 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1236 | if (!LIST_ISEMPTY(&spoe_appctx->list)) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1237 | LIST_DELETE(&spoe_appctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1238 | LIST_INIT(&spoe_appctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1239 | } |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1240 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1241 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1242 | /* Shutdown the server connection, if needed */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1243 | if (appctx->st0 != SPOE_APPCTX_ST_END) { |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1244 | if (appctx->st0 == SPOE_APPCTX_ST_IDLE) { |
| 1245 | eb32_delete(&spoe_appctx->node); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1246 | _HA_ATOMIC_DEC(&agent->counters.idles); |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1247 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1248 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1249 | appctx->st0 = SPOE_APPCTX_ST_END; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1250 | if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE) |
| 1251 | spoe_appctx->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1252 | |
| 1253 | si_shutw(si); |
| 1254 | si_shutr(si); |
| 1255 | si_ic(si)->flags |= CF_READ_NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1256 | } |
| 1257 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1258 | /* Destroy the task attached to this applet */ |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 1259 | task_destroy(spoe_appctx->task); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1260 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1261 | /* Notify all waiting streams */ |
| 1262 | list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1263 | LIST_DELETE(&ctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1264 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1265 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1266 | spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting); |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1267 | ctx->spoe_appctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1268 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1269 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1270 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1271 | } |
| 1272 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1273 | /* If the applet was processing a fragmented frame, notify the |
| 1274 | * corresponding stream. */ |
| 1275 | if (spoe_appctx->frag_ctx.ctx) { |
| 1276 | ctx = spoe_appctx->frag_ctx.ctx; |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1277 | ctx->spoe_appctx = NULL; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1278 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1279 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1280 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1281 | } |
| 1282 | |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1283 | if (!LIST_ISEMPTY(&agent->rt[tid].applets)) { |
| 1284 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) { |
| 1285 | if (ctx->spoe_appctx == spoe_appctx) |
| 1286 | ctx->spoe_appctx = NULL; |
| 1287 | } |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1288 | goto end; |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1289 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1290 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1291 | /* If this was the last running applet, notify all waiting streams */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1292 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1293 | LIST_DELETE(&ctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1294 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1295 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1296 | spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue); |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1297 | ctx->spoe_appctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1298 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1299 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1300 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1301 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1302 | list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1303 | LIST_DELETE(&ctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1304 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1305 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1306 | spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting); |
Christopher Faulet | cf181c7 | 2020-11-10 18:45:34 +0100 | [diff] [blame] | 1307 | ctx->spoe_appctx = NULL; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1308 | ctx->state = SPOE_CTX_ST_ERROR; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1309 | ctx->status_code = (spoe_appctx->status_code + 0x100); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1310 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1311 | } |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1312 | |
| 1313 | end: |
Christopher Faulet | 55ae8a6 | 2019-05-23 22:47:48 +0200 | [diff] [blame] | 1314 | /* Release allocated memory */ |
| 1315 | spoe_release_buffer(&spoe_appctx->buffer, |
| 1316 | &spoe_appctx->buffer_wait); |
| 1317 | pool_free(pool_head_spoe_appctx, spoe_appctx); |
| 1318 | |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1319 | /* Update runtinme agent info */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1320 | agent->rt[tid].frame_size = agent->max_frame_size; |
| 1321 | list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list) |
| 1322 | 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] | 1323 | } |
| 1324 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1325 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1326 | spoe_handle_connect_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1327 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1328 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1329 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1330 | char *frame, *buf; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1331 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1332 | |
Willy Tarreau | 7ab22adb | 2019-06-05 14:53:22 +0200 | [diff] [blame] | 1333 | if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) { |
| 1334 | /* closed */ |
| 1335 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
| 1336 | goto exit; |
| 1337 | } |
| 1338 | |
Willy Tarreau | 4f283fa | 2019-06-05 14:34:03 +0200 | [diff] [blame] | 1339 | if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) { |
Willy Tarreau | 7ab22adb | 2019-06-05 14:53:22 +0200 | [diff] [blame] | 1340 | /* not connected yet */ |
Willy Tarreau | 8bb2ffb | 2018-11-14 17:54:13 +0100 | [diff] [blame] | 1341 | si_rx_endp_more(si); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1342 | task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG); |
| 1343 | goto stop; |
| 1344 | } |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1345 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1346 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1347 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1348 | " - Connection timed out\n", |
| 1349 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1350 | __FUNCTION__, appctx); |
| 1351 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1352 | goto exit; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1353 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1354 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1355 | if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1356 | SPOE_APPCTX(appctx)->task->expire = |
| 1357 | tick_add_ifset(now_ms, agent->timeout.hello); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1358 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1359 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1360 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1361 | buf = trash.area; frame = buf+4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1362 | ret = spoe_prepare_hahello_frame(appctx, frame, |
| 1363 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1364 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1365 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1366 | |
| 1367 | switch (ret) { |
| 1368 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1369 | case 0: /* ignore => an error, cannot be ignored */ |
| 1370 | goto exit; |
| 1371 | |
| 1372 | case 1: /* retry later */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1373 | goto stop; |
| 1374 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1375 | default: |
| 1376 | /* HELLO frame successfully sent, now wait for the |
| 1377 | * reply. */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1378 | appctx->st0 = SPOE_APPCTX_ST_CONNECTING; |
| 1379 | goto next; |
| 1380 | } |
| 1381 | |
| 1382 | next: |
| 1383 | return 0; |
| 1384 | stop: |
| 1385 | return 1; |
| 1386 | exit: |
| 1387 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1388 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1389 | } |
| 1390 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1391 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1392 | spoe_handle_connecting_appctx(struct appctx *appctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1393 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1394 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1395 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1396 | char *frame; |
| 1397 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1398 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1399 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1400 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1401 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1402 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1403 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1404 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1405 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1406 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1407 | " - Connection timed out\n", |
| 1408 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1409 | __FUNCTION__, appctx); |
| 1410 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1411 | goto exit; |
| 1412 | } |
| 1413 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1414 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1415 | ret = spoe_recv_frame(appctx, frame, |
| 1416 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1417 | if (ret > 1) { |
| 1418 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1419 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1420 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1421 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1422 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1423 | ret = spoe_handle_agenthello_frame(appctx, frame, ret); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1424 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1425 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1426 | switch (ret) { |
| 1427 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1428 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1429 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1430 | goto next; |
| 1431 | |
| 1432 | case 1: /* retry later */ |
| 1433 | goto stop; |
| 1434 | |
| 1435 | default: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1436 | _HA_ATOMIC_INC(&agent->counters.idles); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1437 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1438 | SPOE_APPCTX(appctx)->node.key = 0; |
| 1439 | eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node); |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 1440 | |
| 1441 | /* Update runtinme agent info */ |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1442 | 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] | 1443 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1444 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1445 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1446 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1447 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1448 | if (trash.data) |
| 1449 | co_skip(si_oc(si), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1450 | |
| 1451 | SPOE_APPCTX(appctx)->task->expire = |
| 1452 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1453 | return 0; |
| 1454 | stop: |
| 1455 | return 1; |
| 1456 | exit: |
| 1457 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1458 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1459 | } |
| 1460 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1461 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1462 | static int |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1463 | spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1464 | { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1465 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
| 1466 | struct spoe_context *ctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1467 | char *frame, *buf; |
| 1468 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1469 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1470 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1471 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1472 | buf = trash.area; frame = buf+4; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1473 | |
| 1474 | if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) { |
| 1475 | ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx; |
| 1476 | ret = spoe_prepare_hafrag_frame(appctx, ctx, frame, |
| 1477 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1478 | } |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1479 | else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) { |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1480 | *skip = 1; |
| 1481 | ret = 1; |
| 1482 | goto end; |
| 1483 | } |
| 1484 | else { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1485 | ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list); |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1486 | ret = spoe_prepare_hanotify_frame(appctx, ctx, frame, |
| 1487 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1488 | |
| 1489 | } |
| 1490 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1491 | if (ret > 1) |
| 1492 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1493 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1494 | switch (ret) { |
| 1495 | case -1: /* error */ |
| 1496 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1497 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1498 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1499 | case 0: /* ignore */ |
| 1500 | if (ctx == NULL) |
| 1501 | goto abort_frag_frame; |
| 1502 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1503 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1504 | LIST_DELETE(&ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1505 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1506 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1507 | spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue); |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1508 | ctx->spoe_appctx = NULL; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1509 | ctx->state = SPOE_CTX_ST_ERROR; |
| 1510 | ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100); |
| 1511 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1512 | *skip = 1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1513 | break; |
| 1514 | |
| 1515 | case 1: /* retry */ |
| 1516 | *skip = 1; |
| 1517 | break; |
| 1518 | |
| 1519 | default: |
| 1520 | if (ctx == NULL) |
| 1521 | goto abort_frag_frame; |
| 1522 | |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 1523 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1524 | LIST_DELETE(&ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1525 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1526 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1527 | spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue); |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1528 | ctx->spoe_appctx = SPOE_APPCTX(appctx); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1529 | if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) || |
| 1530 | (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN)) |
| 1531 | goto no_frag_frame_sent; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1532 | else |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1533 | goto frag_frame_sent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1534 | } |
| 1535 | goto end; |
| 1536 | |
| 1537 | frag_frame_sent: |
| 1538 | appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1539 | *skip = 1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1540 | SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx; |
| 1541 | SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id; |
| 1542 | SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1543 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
| 1544 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1545 | goto end; |
| 1546 | |
| 1547 | no_frag_frame_sent: |
| 1548 | if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) { |
| 1549 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1550 | LIST_APPEND(&agent->rt[tid].waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1551 | } |
| 1552 | else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) { |
| 1553 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1554 | LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1555 | } |
| 1556 | else { |
| 1557 | appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1558 | *skip = 1; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1559 | LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1560 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1561 | _HA_ATOMIC_INC(&agent->counters.nb_waiting); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1562 | ctx->stats.tv_wait = now; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1563 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1564 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1565 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1566 | SPOE_APPCTX(appctx)->cur_fpa++; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1567 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1568 | ctx->state = SPOE_CTX_ST_WAITING_ACK; |
| 1569 | goto end; |
| 1570 | |
| 1571 | abort_frag_frame: |
| 1572 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1573 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1574 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1575 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1576 | goto end; |
| 1577 | |
| 1578 | end: |
| 1579 | return ret; |
| 1580 | } |
| 1581 | |
| 1582 | static int |
| 1583 | spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip) |
| 1584 | { |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 1585 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1586 | struct spoe_context *ctx = NULL; |
| 1587 | char *frame; |
| 1588 | int ret; |
| 1589 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1590 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1591 | ret = spoe_recv_frame(appctx, frame, |
| 1592 | SPOE_APPCTX(appctx)->max_frame_size); |
| 1593 | if (ret > 1) { |
| 1594 | if (*frame == SPOE_FRM_T_AGENT_DISCON) { |
| 1595 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1596 | ret = -1; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1597 | goto end; |
| 1598 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1599 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1600 | ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret); |
| 1601 | } |
| 1602 | switch (ret) { |
| 1603 | case -1: /* error */ |
| 1604 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1605 | break; |
| 1606 | |
| 1607 | case 0: /* ignore */ |
| 1608 | break; |
| 1609 | |
| 1610 | case 1: /* retry */ |
| 1611 | *skip = 1; |
| 1612 | break; |
| 1613 | |
| 1614 | default: |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1615 | LIST_DELETE(&ctx->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1616 | LIST_INIT(&ctx->list); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1617 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 1618 | spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting); |
| 1619 | ctx->stats.tv_response = now; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1620 | if (ctx->spoe_appctx) { |
| 1621 | ctx->spoe_appctx->cur_fpa--; |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 1622 | ctx->spoe_appctx = NULL; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1623 | } |
Christopher Faulet | 8eda93f | 2017-02-09 09:44:33 +0100 | [diff] [blame] | 1624 | if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY && |
| 1625 | ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) { |
| 1626 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1627 | SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL; |
| 1628 | SPOE_APPCTX(appctx)->frag_ctx.cursid = 0; |
| 1629 | SPOE_APPCTX(appctx)->frag_ctx.curfid = 0; |
| 1630 | } |
| 1631 | else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) |
| 1632 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1633 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 1634 | break; |
| 1635 | } |
| 1636 | |
| 1637 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1638 | if (trash.data) |
| 1639 | co_skip(si_oc(appctx->owner), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1640 | end: |
| 1641 | return ret; |
| 1642 | } |
| 1643 | |
| 1644 | static int |
| 1645 | spoe_handle_processing_appctx(struct appctx *appctx) |
| 1646 | { |
| 1647 | struct stream_interface *si = appctx->owner; |
| 1648 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1649 | int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1650 | |
| 1651 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) { |
| 1652 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
| 1653 | goto exit; |
| 1654 | } |
| 1655 | |
| 1656 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
| 1657 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
| 1658 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1659 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1660 | goto next; |
| 1661 | } |
| 1662 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1663 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1664 | " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1665 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1666 | __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa, |
| 1667 | agent->max_fpa, spoe_appctx_state_str[appctx->st0], |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1668 | SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1669 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1670 | if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) |
| 1671 | skip_sending = 1; |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1672 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1673 | /* receiving_frame loop */ |
| 1674 | while (!skip_receiving) { |
| 1675 | ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving); |
| 1676 | switch (ret) { |
| 1677 | case -1: /* error */ |
| 1678 | goto next; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1679 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1680 | case 0: /* ignore */ |
| 1681 | active_r = 1; |
| 1682 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1683 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1684 | case 1: /* retry */ |
| 1685 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1686 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1687 | default: |
| 1688 | active_r = 1; |
| 1689 | break; |
| 1690 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1691 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1692 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1693 | /* send_frame loop */ |
| 1694 | while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) { |
| 1695 | ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending); |
| 1696 | switch (ret) { |
| 1697 | case -1: /* error */ |
| 1698 | goto next; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1699 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1700 | case 0: /* ignore */ |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1701 | if (SPOE_APPCTX(appctx)->node.key) |
| 1702 | SPOE_APPCTX(appctx)->node.key--; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1703 | active_s++; |
| 1704 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1705 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1706 | case 1: /* retry */ |
| 1707 | break; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1708 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1709 | default: |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1710 | if (SPOE_APPCTX(appctx)->node.key) |
| 1711 | SPOE_APPCTX(appctx)->node.key--; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1712 | active_s++; |
| 1713 | break; |
| 1714 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1715 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1716 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1717 | if (active_s || active_r) { |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1718 | update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s); |
| 1719 | SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle); |
| 1720 | } |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1721 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1722 | if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) { |
Christopher Faulet | 9e647e5 | 2021-03-01 15:01:14 +0100 | [diff] [blame] | 1723 | struct server *srv = objt_server(si_strm(si)->target); |
| 1724 | |
| 1725 | /* With several threads, close the applet if there are pending |
| 1726 | * connections or if the server is full. Otherwise, add the |
| 1727 | * applet in the idle list. |
| 1728 | */ |
| 1729 | if (global.nbthread > 1 && |
| 1730 | (agent->b.be->nbpend || |
| 1731 | (srv && (srv->nbpend || (srv->maxconn && srv->served >=srv_dynamic_maxconn(srv)))))) { |
| 1732 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
| 1733 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
| 1734 | appctx->st1 = SPOE_APPCTX_ERR_NONE; |
| 1735 | goto next; |
| 1736 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1737 | _HA_ATOMIC_INC(&agent->counters.idles); |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1738 | appctx->st0 = SPOE_APPCTX_ST_IDLE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 1739 | eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1740 | } |
| 1741 | return 1; |
| 1742 | |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1743 | next: |
| 1744 | SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle); |
| 1745 | return 0; |
| 1746 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1747 | exit: |
| 1748 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
| 1752 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1753 | spoe_handle_disconnect_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1754 | { |
| 1755 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1756 | struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1757 | char *frame, *buf; |
| 1758 | int ret; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1759 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1760 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) |
| 1761 | goto exit; |
| 1762 | |
| 1763 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) |
| 1764 | goto exit; |
| 1765 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1766 | /* 4 bytes are reserved at the beginning of <buf> to store the frame |
| 1767 | * length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1768 | buf = trash.area; frame = buf+4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1769 | ret = spoe_prepare_hadiscon_frame(appctx, frame, |
| 1770 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1771 | if (ret > 1) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1772 | ret = spoe_send_frame(appctx, buf, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1773 | |
| 1774 | switch (ret) { |
| 1775 | case -1: /* error */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1776 | case 0: /* ignore => an error, cannot be ignored */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1777 | goto exit; |
| 1778 | |
| 1779 | case 1: /* retry */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1780 | goto stop; |
| 1781 | |
| 1782 | default: |
| 1783 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1784 | " - disconnected by HAProxy (%d): %s\n", |
| 1785 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1786 | __FUNCTION__, appctx, |
| 1787 | SPOE_APPCTX(appctx)->status_code, |
| 1788 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1789 | |
| 1790 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING; |
| 1791 | goto next; |
| 1792 | } |
| 1793 | |
| 1794 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1795 | SPOE_APPCTX(appctx)->task->expire = |
| 1796 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1797 | return 0; |
| 1798 | stop: |
| 1799 | return 1; |
| 1800 | exit: |
| 1801 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1802 | return 0; |
| 1803 | } |
| 1804 | |
| 1805 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1806 | spoe_handle_disconnecting_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1807 | { |
| 1808 | struct stream_interface *si = appctx->owner; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1809 | char *frame; |
| 1810 | int ret; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1811 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1812 | if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1813 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1814 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1815 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1816 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1817 | if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1818 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1819 | goto exit; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1820 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1821 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1822 | frame = trash.area; trash.data = 0; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1823 | ret = spoe_recv_frame(appctx, frame, |
| 1824 | SPOE_APPCTX(appctx)->max_frame_size); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1825 | if (ret > 1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1826 | trash.data = ret + 4; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1827 | ret = spoe_handle_agentdiscon_frame(appctx, frame, ret); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | switch (ret) { |
| 1831 | case -1: /* error */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1832 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1833 | " - error on frame (%s)\n", |
| 1834 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1835 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1836 | __FUNCTION__, appctx, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1837 | spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1838 | goto exit; |
| 1839 | |
| 1840 | case 0: /* ignore */ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1841 | goto next; |
| 1842 | |
| 1843 | case 1: /* retry */ |
| 1844 | goto stop; |
| 1845 | |
| 1846 | default: |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1847 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1848 | " - disconnected by peer (%d): %.*s\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1849 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1850 | ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id, |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1851 | __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code, |
| 1852 | SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1853 | goto exit; |
| 1854 | } |
| 1855 | |
| 1856 | next: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1857 | /* Do not forget to remove processed frame from the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1858 | if (trash.data) |
| 1859 | co_skip(si_oc(appctx->owner), trash.data); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1860 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1861 | return 0; |
| 1862 | stop: |
| 1863 | return 1; |
| 1864 | exit: |
| 1865 | appctx->st0 = SPOE_APPCTX_ST_EXIT; |
| 1866 | return 0; |
| 1867 | } |
| 1868 | |
| 1869 | /* I/O Handler processing messages exchanged with the agent */ |
| 1870 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1871 | spoe_handle_appctx(struct appctx *appctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1872 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1873 | struct stream_interface *si = appctx->owner; |
| 1874 | struct spoe_agent *agent; |
| 1875 | |
| 1876 | if (SPOE_APPCTX(appctx) == NULL) |
| 1877 | return; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1878 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1879 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
| 1880 | agent = SPOE_APPCTX(appctx)->agent; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1881 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1882 | switchstate: |
| 1883 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p" |
| 1884 | " - appctx-state=%s\n", |
| 1885 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 1886 | __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]); |
| 1887 | |
| 1888 | switch (appctx->st0) { |
| 1889 | case SPOE_APPCTX_ST_CONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1890 | if (spoe_handle_connect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1891 | goto out; |
| 1892 | goto switchstate; |
| 1893 | |
| 1894 | case SPOE_APPCTX_ST_CONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1895 | if (spoe_handle_connecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1896 | goto out; |
| 1897 | goto switchstate; |
| 1898 | |
| 1899 | case SPOE_APPCTX_ST_IDLE: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1900 | _HA_ATOMIC_DEC(&agent->counters.idles); |
Christopher Faulet | 7d9f1ba | 2018-02-28 13:33:26 +0100 | [diff] [blame] | 1901 | eb32_delete(&SPOE_APPCTX(appctx)->node); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1902 | if (stopping && |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1903 | LIST_ISEMPTY(&agent->rt[tid].sending_queue) && |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1904 | LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1905 | SPOE_APPCTX(appctx)->task->expire = |
| 1906 | tick_add_ifset(now_ms, agent->timeout.idle); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1907 | appctx->st0 = SPOE_APPCTX_ST_DISCONNECT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1908 | goto switchstate; |
| 1909 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1910 | appctx->st0 = SPOE_APPCTX_ST_PROCESSING; |
| 1911 | /* fall through */ |
| 1912 | |
| 1913 | case SPOE_APPCTX_ST_PROCESSING: |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 1914 | case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY: |
| 1915 | case SPOE_APPCTX_ST_WAITING_SYNC_ACK: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1916 | if (spoe_handle_processing_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1917 | goto out; |
| 1918 | goto switchstate; |
| 1919 | |
| 1920 | case SPOE_APPCTX_ST_DISCONNECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1921 | if (spoe_handle_disconnect_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1922 | goto out; |
| 1923 | goto switchstate; |
| 1924 | |
| 1925 | case SPOE_APPCTX_ST_DISCONNECTING: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1926 | if (spoe_handle_disconnecting_appctx(appctx)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1927 | goto out; |
| 1928 | goto switchstate; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1929 | |
| 1930 | case SPOE_APPCTX_ST_EXIT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1931 | appctx->st0 = SPOE_APPCTX_ST_END; |
| 1932 | SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY; |
| 1933 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1934 | si_shutw(si); |
| 1935 | si_shutr(si); |
| 1936 | si_ic(si)->flags |= CF_READ_NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1937 | /* fall through */ |
| 1938 | |
| 1939 | case SPOE_APPCTX_ST_END: |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 1940 | return; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1941 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1942 | out: |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 1943 | if (stopping) |
| 1944 | spoe_wakeup_appctx(appctx); |
| 1945 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1946 | if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY) |
| 1947 | task_queue(SPOE_APPCTX(appctx)->task); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | struct applet spoe_applet = { |
| 1951 | .obj_type = OBJ_TYPE_APPLET, |
| 1952 | .name = "<SPOE>", /* used for logging */ |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1953 | .fct = spoe_handle_appctx, |
| 1954 | .release = spoe_release_appctx, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1955 | }; |
| 1956 | |
| 1957 | /* Create a SPOE applet. On success, the created applet is returned, else |
| 1958 | * NULL. */ |
| 1959 | static struct appctx * |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1960 | spoe_create_appctx(struct spoe_config *conf) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1961 | { |
| 1962 | struct appctx *appctx; |
| 1963 | struct session *sess; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1964 | struct stream *strm; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1965 | |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 1966 | if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1967 | goto out_error; |
| 1968 | |
Willy Tarreau | c9ef9bc | 2021-03-22 21:04:50 +0100 | [diff] [blame] | 1969 | appctx->ctx.spoe.ptr = pool_zalloc(pool_head_spoe_appctx); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1970 | if (SPOE_APPCTX(appctx) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1971 | goto out_free_appctx; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 1972 | |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1973 | appctx->st0 = SPOE_APPCTX_ST_CONNECT; |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 1974 | if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL) |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1975 | goto out_free_spoe_appctx; |
| 1976 | |
| 1977 | SPOE_APPCTX(appctx)->owner = appctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1978 | SPOE_APPCTX(appctx)->task->process = spoe_process_appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1979 | SPOE_APPCTX(appctx)->task->context = appctx; |
| 1980 | SPOE_APPCTX(appctx)->agent = conf->agent; |
| 1981 | SPOE_APPCTX(appctx)->version = 0; |
| 1982 | SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size; |
| 1983 | SPOE_APPCTX(appctx)->flags = 0; |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 1984 | SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1985 | SPOE_APPCTX(appctx)->buffer = BUF_NULL; |
Christopher Faulet | 8f82b20 | 2018-01-24 16:23:03 +0100 | [diff] [blame] | 1986 | SPOE_APPCTX(appctx)->cur_fpa = 0; |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1987 | |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 1988 | LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list); |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 1989 | SPOE_APPCTX(appctx)->buffer_wait.target = appctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 1990 | SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 1991 | |
| 1992 | LIST_INIT(&SPOE_APPCTX(appctx)->list); |
| 1993 | LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1994 | |
Willy Tarreau | 5820a36 | 2016-12-22 15:59:02 +0100 | [diff] [blame] | 1995 | sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1996 | if (!sess) |
| 1997 | goto out_free_spoe; |
| 1998 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 1999 | if ((strm = stream_new(sess, &appctx->obj_type, &BUF_NULL)) == NULL) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2000 | goto out_free_sess; |
| 2001 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2002 | stream_set_backend(strm, conf->agent->b.be); |
| 2003 | |
| 2004 | /* applet is waiting for data */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2005 | si_cant_get(&strm->si[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2006 | appctx_wakeup(appctx); |
| 2007 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2008 | strm->do_log = NULL; |
| 2009 | strm->res.flags |= CF_READ_DONTWAIT; |
| 2010 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2011 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2012 | LIST_APPEND(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2013 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2014 | _HA_ATOMIC_INC(&conf->agent->counters.applets); |
Emeric Brun | 5f77fef | 2017-05-29 15:26:51 +0200 | [diff] [blame] | 2015 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 2016 | task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT); |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2017 | task_wakeup(strm->task, TASK_WOKEN_INIT); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2018 | return appctx; |
| 2019 | |
| 2020 | /* Error unrolling */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2021 | out_free_sess: |
| 2022 | session_free(sess); |
| 2023 | out_free_spoe: |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 2024 | task_destroy(SPOE_APPCTX(appctx)->task); |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2025 | out_free_spoe_appctx: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2026 | pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx)); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2027 | out_free_appctx: |
| 2028 | appctx_free(appctx); |
| 2029 | out_error: |
| 2030 | return NULL; |
| 2031 | } |
| 2032 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2033 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2034 | spoe_queue_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2035 | { |
| 2036 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2037 | struct spoe_agent *agent = conf->agent; |
| 2038 | struct appctx *appctx; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2039 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2040 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2041 | /* Check if we need to create a new SPOE applet or not. */ |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 2042 | if (!eb_is_empty(&agent->rt[tid].idle_applets) && |
Christopher Faulet | eccfa61 | 2020-06-22 15:32:14 +0200 | [diff] [blame] | 2043 | (agent->rt[tid].processing == 1 || agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2044 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2045 | |
| 2046 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2047 | " - try to create new SPOE appctx\n", |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2048 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__, |
| 2049 | ctx->strm); |
| 2050 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2051 | /* Do not try to create a new applet if there is no server up for the |
| 2052 | * agent's backend. */ |
| 2053 | if (!agent->b.be->srv_act && !agent->b.be->srv_bck) { |
| 2054 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2055 | " - cannot create SPOE appctx: no server up\n", |
| 2056 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2057 | __FUNCTION__, ctx->strm); |
| 2058 | goto end; |
| 2059 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2060 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2061 | /* Do not try to create a new applet if we have reached the maximum of |
| 2062 | * connection per seconds */ |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 2063 | if (agent->cps_max > 0) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2064 | 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] | 2065 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2066 | " - cannot create SPOE appctx: max CPS reached\n", |
| 2067 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2068 | __FUNCTION__, ctx->strm); |
| 2069 | goto end; |
| 2070 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2071 | } |
| 2072 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2073 | appctx = spoe_create_appctx(conf); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2074 | if (appctx == NULL) { |
| 2075 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2076 | " - failed to create SPOE appctx\n", |
| 2077 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2078 | __FUNCTION__, ctx->strm); |
Christopher Faulet | 3b8e349 | 2018-03-26 17:20:58 +0200 | [diff] [blame] | 2079 | send_log(&conf->agent_fe, LOG_EMERG, |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 2080 | "SPOE: [%s] failed to create SPOE applet\n", |
| 2081 | agent->id); |
| 2082 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2083 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2084 | } |
| 2085 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2086 | /* Increase the per-process number of cumulated connections */ |
| 2087 | if (agent->cps_max > 0) |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2088 | update_freq_ctr(&agent->rt[tid].conn_per_sec, 1); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2089 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2090 | end: |
| 2091 | /* 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] | 2092 | if (LIST_ISEMPTY(&agent->rt[tid].applets)) { |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2093 | ctx->status_code = SPOE_CTX_ERR_RES; |
| 2094 | return -1; |
| 2095 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2096 | |
Christopher Faulet | 3e86cec | 2019-04-10 14:02:12 +0200 | [diff] [blame] | 2097 | /* Add the SPOE context in the sending queue if the stream has no applet |
| 2098 | * already assigned and wakeup all idle applets. Otherwise, don't queue |
| 2099 | * it. */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2100 | _HA_ATOMIC_INC(&agent->counters.nb_sending); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2101 | spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request); |
| 2102 | ctx->stats.tv_queue = now; |
Christopher Faulet | 3e86cec | 2019-04-10 14:02:12 +0200 | [diff] [blame] | 2103 | if (ctx->spoe_appctx) |
| 2104 | return 1; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2105 | LIST_APPEND(&agent->rt[tid].sending_queue, &ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2106 | |
| 2107 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2108 | " - Add stream in sending queue" |
Christopher Faulet | 68db023 | 2018-04-06 11:34:12 +0200 | [diff] [blame] | 2109 | " - applets=%u - idles=%u - processing=%u\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2110 | (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__, |
Christopher Faulet | 68db023 | 2018-04-06 11:34:12 +0200 | [diff] [blame] | 2111 | ctx->strm, agent->counters.applets, agent->counters.idles, |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2112 | agent->rt[tid].processing); |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2113 | |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 2114 | /* Finally try to wakeup an IDLE applet. */ |
| 2115 | if (!eb_is_empty(&agent->rt[tid].idle_applets)) { |
| 2116 | struct eb32_node *node; |
| 2117 | |
| 2118 | node = eb32_first(&agent->rt[tid].idle_applets); |
| 2119 | spoe_appctx = eb32_entry(node, struct spoe_appctx, node); |
| 2120 | if (node && spoe_appctx) { |
| 2121 | eb32_delete(&spoe_appctx->node); |
| 2122 | spoe_appctx->node.key++; |
| 2123 | eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node); |
| 2124 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2125 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2126 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2127 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | /*************************************************************************** |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2131 | * Functions that encode SPOE messages |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2132 | **************************************************************************/ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2133 | /* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle |
| 2134 | * fragmented_content. If the next message can be processed, it returns 0. If |
| 2135 | * the message is too big, it returns -1.*/ |
| 2136 | static int |
| 2137 | spoe_encode_message(struct stream *s, struct spoe_context *ctx, |
| 2138 | struct spoe_message *msg, int dir, |
| 2139 | char **buf, char *end) |
| 2140 | { |
| 2141 | struct sample *smp; |
| 2142 | struct spoe_arg *arg; |
| 2143 | int ret; |
| 2144 | |
| 2145 | if (msg->cond) { |
| 2146 | ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2147 | ret = acl_pass(ret); |
| 2148 | if (msg->cond->pol == ACL_COND_UNLESS) |
| 2149 | ret = !ret; |
| 2150 | |
| 2151 | /* the rule does not match */ |
| 2152 | if (!ret) |
| 2153 | goto next; |
| 2154 | } |
| 2155 | |
| 2156 | /* Resume encoding of a SPOE argument */ |
| 2157 | if (ctx->frag_ctx.curarg != NULL) { |
| 2158 | arg = ctx->frag_ctx.curarg; |
| 2159 | goto encode_argument; |
| 2160 | } |
| 2161 | |
| 2162 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2163 | goto encode_msg_payload; |
| 2164 | |
| 2165 | /* Check if there is enough space for the message name and the |
| 2166 | * number of arguments. It implies <msg->id_len> is encoded on 2 |
| 2167 | * bytes, at most (< 2288). */ |
| 2168 | if (*buf + 2 + msg->id_len + 1 > end) |
| 2169 | goto too_big; |
| 2170 | |
| 2171 | /* Encode the message name */ |
| 2172 | if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1) |
| 2173 | goto too_big; |
| 2174 | |
| 2175 | /* Set the number of arguments for this message */ |
| 2176 | **buf = msg->nargs; |
| 2177 | (*buf)++; |
| 2178 | |
| 2179 | ctx->frag_ctx.curoff = 0; |
| 2180 | encode_msg_payload: |
| 2181 | |
| 2182 | /* Loop on arguments */ |
| 2183 | list_for_each_entry(arg, &msg->args, list) { |
| 2184 | ctx->frag_ctx.curarg = arg; |
| 2185 | ctx->frag_ctx.curoff = UINT_MAX; |
Kevin Zhu | f7f5428 | 2019-04-26 14:00:01 +0800 | [diff] [blame] | 2186 | ctx->frag_ctx.curlen = 0; |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2187 | |
| 2188 | encode_argument: |
| 2189 | if (ctx->frag_ctx.curoff != UINT_MAX) |
| 2190 | goto encode_arg_value; |
| 2191 | |
Joseph Herlant | f7f6031 | 2018-11-15 13:46:49 -0800 | [diff] [blame] | 2192 | /* Encode the argument name as a string. It can by NULL */ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2193 | if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1) |
| 2194 | goto too_big; |
| 2195 | |
| 2196 | ctx->frag_ctx.curoff = 0; |
| 2197 | encode_arg_value: |
| 2198 | |
Joseph Herlant | f7f6031 | 2018-11-15 13:46:49 -0800 | [diff] [blame] | 2199 | /* Fetch the argument value */ |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2200 | 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] | 2201 | if (smp) { |
| 2202 | smp->ctx.a[0] = &ctx->frag_ctx.curlen; |
| 2203 | smp->ctx.a[1] = &ctx->frag_ctx.curoff; |
| 2204 | } |
Christopher Faulet | 85db321 | 2019-04-26 14:30:15 +0200 | [diff] [blame] | 2205 | ret = spoe_encode_data(smp, buf, end); |
Christopher Faulet | 10e3767 | 2017-09-21 16:38:22 +0200 | [diff] [blame] | 2206 | if (ret == -1 || ctx->frag_ctx.curoff) |
| 2207 | goto too_big; |
| 2208 | } |
| 2209 | |
| 2210 | next: |
| 2211 | return 0; |
| 2212 | |
| 2213 | too_big: |
| 2214 | return -1; |
| 2215 | } |
| 2216 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2217 | /* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to |
| 2218 | * handle fragmented content. On success it returns 1. If an error occurred, -1 |
| 2219 | * is returned. If nothing has been encoded, it returns 0 (this is only possible |
| 2220 | * for unfragmented payload). */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2221 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2222 | spoe_encode_messages(struct stream *s, struct spoe_context *ctx, |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2223 | struct list *messages, int dir, int type) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2224 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2225 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2226 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2227 | struct spoe_message *msg; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2228 | char *p, *end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2229 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2230 | p = b_head(&ctx->buffer); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2231 | end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2232 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2233 | if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */ |
| 2234 | /* Resume encoding of a SPOE message */ |
| 2235 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2236 | msg = ctx->frag_ctx.curmsg; |
| 2237 | goto encode_evt_message; |
| 2238 | } |
| 2239 | |
| 2240 | list_for_each_entry(msg, messages, by_evt) { |
| 2241 | ctx->frag_ctx.curmsg = msg; |
| 2242 | ctx->frag_ctx.curarg = NULL; |
| 2243 | ctx->frag_ctx.curoff = UINT_MAX; |
| 2244 | |
| 2245 | encode_evt_message: |
| 2246 | if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1) |
| 2247 | goto too_big; |
| 2248 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2249 | } |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2250 | else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */ |
| 2251 | /* Resume encoding of a SPOE message */ |
| 2252 | if (ctx->frag_ctx.curmsg != NULL) { |
| 2253 | msg = ctx->frag_ctx.curmsg; |
| 2254 | goto encode_grp_message; |
| 2255 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2256 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2257 | list_for_each_entry(msg, messages, by_grp) { |
| 2258 | ctx->frag_ctx.curmsg = msg; |
| 2259 | ctx->frag_ctx.curarg = NULL; |
| 2260 | ctx->frag_ctx.curoff = UINT_MAX; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2261 | |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2262 | encode_grp_message: |
| 2263 | if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1) |
| 2264 | goto too_big; |
| 2265 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2266 | } |
Christopher Faulet | c718b82 | 2017-09-21 16:50:56 +0200 | [diff] [blame] | 2267 | else |
| 2268 | goto skip; |
| 2269 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2270 | |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2271 | /* nothing has been encoded for an unfragmented payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2272 | if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer)) |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2273 | goto skip; |
| 2274 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2275 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2276 | " - encode %s messages - spoe_appctx=%p" |
| 2277 | "- max_size=%u - encoded=%ld\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2278 | (int)now.tv_sec, (int)now.tv_usec, |
| 2279 | agent->id, __FUNCTION__, s, |
| 2280 | ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"), |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2281 | ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE), |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2282 | p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2283 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2284 | b_set_data(&ctx->buffer, p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2285 | ctx->frag_ctx.curmsg = NULL; |
| 2286 | ctx->frag_ctx.curarg = NULL; |
| 2287 | ctx->frag_ctx.curoff = 0; |
| 2288 | ctx->frag_ctx.flags = SPOE_FRM_FL_FIN; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2289 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2290 | return 1; |
| 2291 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2292 | too_big: |
Christopher Faulet | a715ea8 | 2019-04-10 14:21:51 +0200 | [diff] [blame] | 2293 | /* Return an error if fragmentation is unsupported or if nothing has |
| 2294 | * been encoded because its too big and not splittable. */ |
| 2295 | if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) { |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 2296 | ctx->status_code = SPOE_CTX_ERR_TOO_BIG; |
| 2297 | return -1; |
| 2298 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2299 | |
| 2300 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2301 | " - encode fragmented messages - spoe_appctx=%p" |
| 2302 | " - curmsg=%p - curarg=%p - curoff=%u" |
| 2303 | " - max_size=%u - encoded=%ld\n", |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2304 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2305 | agent->id, __FUNCTION__, s, ctx->spoe_appctx, |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2306 | ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff, |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2307 | (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] | 2308 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2309 | b_set_data(&ctx->buffer, p - b_head(&ctx->buffer)); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2310 | ctx->flags |= SPOE_CTX_FL_FRAGMENTED; |
| 2311 | ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN; |
| 2312 | return 1; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2313 | |
| 2314 | skip: |
| 2315 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2316 | " - skip the frame because nothing has been encoded\n", |
| 2317 | (int)now.tv_sec, (int)now.tv_usec, |
| 2318 | agent->id, __FUNCTION__, s); |
| 2319 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2320 | } |
| 2321 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2322 | |
| 2323 | /*************************************************************************** |
| 2324 | * Functions that handle SPOE actions |
| 2325 | **************************************************************************/ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2326 | /* Helper function to set a variable */ |
| 2327 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2328 | 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] | 2329 | struct sample *smp) |
| 2330 | { |
| 2331 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2332 | struct spoe_agent *agent = conf->agent; |
| 2333 | char varname[64]; |
| 2334 | |
| 2335 | memset(varname, 0, sizeof(varname)); |
| 2336 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2337 | scope, agent->var_pfx, len, name); |
Etienne Carriere | aec8989 | 2017-12-14 09:36:40 +0000 | [diff] [blame] | 2338 | if (agent->flags & SPOE_FL_FORCE_SET_VAR) |
| 2339 | vars_set_by_name(varname, len, smp); |
| 2340 | else |
| 2341 | vars_set_by_name_ifexist(varname, len, smp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | /* Helper function to unset a variable */ |
| 2345 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2346 | 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] | 2347 | struct sample *smp) |
| 2348 | { |
| 2349 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2350 | struct spoe_agent *agent = conf->agent; |
| 2351 | char varname[64]; |
| 2352 | |
| 2353 | memset(varname, 0, sizeof(varname)); |
| 2354 | len = snprintf(varname, sizeof(varname), "%s.%s.%.*s", |
| 2355 | scope, agent->var_pfx, len, name); |
| 2356 | vars_unset_by_name_ifexist(varname, len, smp); |
| 2357 | } |
| 2358 | |
| 2359 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2360 | static inline int |
| 2361 | spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx, |
| 2362 | char **buf, char *end, int dir) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2363 | { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2364 | char *str, *scope, *p = *buf; |
| 2365 | struct sample smp; |
| 2366 | uint64_t sz; |
| 2367 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2368 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2369 | if (p + 2 >= end) |
| 2370 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2371 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2372 | /* SET-VAR requires 3 arguments */ |
| 2373 | if (*p++ != 3) |
| 2374 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2375 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2376 | switch (*p++) { |
| 2377 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2378 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2379 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2380 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2381 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2382 | default: goto skip; |
| 2383 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2384 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2385 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2386 | goto skip; |
| 2387 | memset(&smp, 0, sizeof(smp)); |
| 2388 | 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] | 2389 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2390 | if (spoe_decode_data(&p, end, &smp) == -1) |
| 2391 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2392 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2393 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2394 | " - set-var '%s.%s.%.*s'\n", |
| 2395 | (int)now.tv_sec, (int)now.tv_usec, |
| 2396 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2397 | __FUNCTION__, s, scope, |
| 2398 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2399 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2400 | |
Christopher Faulet | 2469eba | 2020-10-15 16:08:30 +0200 | [diff] [blame] | 2401 | if (smp.data.type == SMP_T_ANY) |
| 2402 | spoe_unset_var(ctx, scope, str, sz, &smp); |
| 2403 | else |
| 2404 | spoe_set_var(ctx, scope, str, sz, &smp); |
Christopher Faulet | b5cff60 | 2016-11-24 14:53:22 +0100 | [diff] [blame] | 2405 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2406 | ret = (p - *buf); |
| 2407 | *buf = p; |
| 2408 | return ret; |
| 2409 | skip: |
| 2410 | return 0; |
| 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 | static inline int |
| 2414 | spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx, |
| 2415 | char **buf, char *end, int dir) |
| 2416 | { |
| 2417 | char *str, *scope, *p = *buf; |
| 2418 | struct sample smp; |
| 2419 | uint64_t sz; |
| 2420 | int ret; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2421 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2422 | if (p + 2 >= end) |
| 2423 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2424 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2425 | /* UNSET-VAR requires 2 arguments */ |
| 2426 | if (*p++ != 2) |
| 2427 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2428 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2429 | switch (*p++) { |
| 2430 | case SPOE_SCOPE_PROC: scope = "proc"; break; |
| 2431 | case SPOE_SCOPE_SESS: scope = "sess"; break; |
| 2432 | case SPOE_SCOPE_TXN : scope = "txn"; break; |
| 2433 | case SPOE_SCOPE_REQ : scope = "req"; break; |
| 2434 | case SPOE_SCOPE_RES : scope = "res"; break; |
| 2435 | default: goto skip; |
| 2436 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2437 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2438 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 2439 | goto skip; |
| 2440 | memset(&smp, 0, sizeof(smp)); |
| 2441 | 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] | 2442 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2443 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2444 | " - unset-var '%s.%s.%.*s'\n", |
| 2445 | (int)now.tv_sec, (int)now.tv_usec, |
| 2446 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id, |
| 2447 | __FUNCTION__, s, scope, |
| 2448 | ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx, |
| 2449 | (int)sz, str); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2450 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2451 | spoe_unset_var(ctx, scope, str, sz, &smp); |
| 2452 | |
| 2453 | ret = (p - *buf); |
| 2454 | *buf = p; |
| 2455 | return ret; |
| 2456 | skip: |
| 2457 | return 0; |
| 2458 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2459 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2460 | /* Process SPOE actions for a specific event. It returns 1 on success. If an |
| 2461 | * error occurred, 0 is returned. */ |
| 2462 | static int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2463 | spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir) |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2464 | { |
| 2465 | char *p, *end; |
| 2466 | int ret; |
| 2467 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2468 | p = b_head(&ctx->buffer); |
| 2469 | end = p + b_data(&ctx->buffer); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2470 | |
| 2471 | while (p < end) { |
| 2472 | enum spoe_action_type type; |
| 2473 | |
| 2474 | type = *p++; |
| 2475 | switch (type) { |
| 2476 | case SPOE_ACT_T_SET_VAR: |
| 2477 | ret = spoe_decode_action_set_var(s, ctx, &p, end, dir); |
| 2478 | if (!ret) |
| 2479 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2480 | break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2481 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2482 | case SPOE_ACT_T_UNSET_VAR: |
| 2483 | ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir); |
| 2484 | if (!ret) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2485 | goto skip; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2486 | break; |
| 2487 | |
| 2488 | default: |
| 2489 | goto skip; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2490 | } |
| 2491 | } |
| 2492 | |
| 2493 | return 1; |
| 2494 | skip: |
| 2495 | return 0; |
| 2496 | } |
| 2497 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2498 | /*************************************************************************** |
| 2499 | * Functions that process SPOE events |
| 2500 | **************************************************************************/ |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2501 | static void |
| 2502 | spoe_update_stats(struct stream *s, struct spoe_agent *agent, |
| 2503 | struct spoe_context *ctx, int dir) |
| 2504 | { |
| 2505 | if (!tv_iszero(&ctx->stats.tv_start)) { |
| 2506 | spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process); |
| 2507 | ctx->stats.t_total += ctx->stats.t_process; |
| 2508 | tv_zero(&ctx->stats.tv_request); |
| 2509 | tv_zero(&ctx->stats.tv_queue); |
| 2510 | tv_zero(&ctx->stats.tv_wait); |
| 2511 | tv_zero(&ctx->stats.tv_response); |
| 2512 | } |
| 2513 | |
| 2514 | if (agent->var_t_process) { |
| 2515 | struct sample smp; |
| 2516 | |
| 2517 | memset(&smp, 0, sizeof(smp)); |
| 2518 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2519 | smp.data.u.sint = ctx->stats.t_process; |
| 2520 | smp.data.type = SMP_T_SINT; |
| 2521 | |
| 2522 | spoe_set_var(ctx, "txn", agent->var_t_process, |
| 2523 | strlen(agent->var_t_process), &smp); |
| 2524 | } |
| 2525 | |
| 2526 | if (agent->var_t_total) { |
| 2527 | struct sample smp; |
| 2528 | |
| 2529 | memset(&smp, 0, sizeof(smp)); |
| 2530 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2531 | smp.data.u.sint = ctx->stats.t_total; |
| 2532 | smp.data.type = SMP_T_SINT; |
| 2533 | |
| 2534 | spoe_set_var(ctx, "txn", agent->var_t_total, |
| 2535 | strlen(agent->var_t_total), &smp); |
| 2536 | } |
| 2537 | } |
| 2538 | |
| 2539 | static void |
| 2540 | spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent, |
| 2541 | struct spoe_context *ctx, int dir) |
| 2542 | { |
| 2543 | if (agent->eps_max > 0) |
| 2544 | update_freq_ctr(&agent->rt[tid].err_per_sec, 1); |
| 2545 | |
| 2546 | if (agent->var_on_error) { |
| 2547 | struct sample smp; |
| 2548 | |
| 2549 | memset(&smp, 0, sizeof(smp)); |
| 2550 | smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL); |
| 2551 | smp.data.u.sint = ctx->status_code; |
| 2552 | smp.data.type = SMP_T_BOOL; |
| 2553 | |
| 2554 | spoe_set_var(ctx, "txn", agent->var_on_error, |
| 2555 | strlen(agent->var_on_error), &smp); |
| 2556 | } |
| 2557 | |
| 2558 | ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR) |
| 2559 | ? SPOE_CTX_ST_READY |
| 2560 | : SPOE_CTX_ST_NONE); |
| 2561 | } |
| 2562 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2563 | static inline int |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2564 | 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] | 2565 | { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2566 | /* If a process is already started for this SPOE context, retry |
| 2567 | * later. */ |
| 2568 | if (ctx->flags & SPOE_CTX_FL_PROCESS) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2569 | return 0; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2570 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2571 | agent->rt[tid].processing++; |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2572 | ctx->stats.tv_start = now; |
| 2573 | ctx->stats.tv_request = now; |
| 2574 | ctx->stats.t_request = -1; |
| 2575 | ctx->stats.t_queue = -1; |
| 2576 | ctx->stats.t_waiting = -1; |
| 2577 | ctx->stats.t_response = -1; |
| 2578 | ctx->stats.t_process = -1; |
| 2579 | |
| 2580 | ctx->status_code = 0; |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2581 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2582 | /* Set the right flag to prevent request and response processing |
| 2583 | * in same time. */ |
| 2584 | ctx->flags |= ((dir == SMP_OPT_DIR_REQ) |
| 2585 | ? SPOE_CTX_FL_REQ_PROCESS |
| 2586 | : SPOE_CTX_FL_RSP_PROCESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2587 | return 1; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2588 | } |
| 2589 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2590 | static inline void |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2591 | spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2592 | { |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2593 | struct spoe_appctx *sa = ctx->spoe_appctx; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2594 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2595 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) |
| 2596 | return; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2597 | _HA_ATOMIC_INC(&agent->counters.nb_processed); |
Christopher Faulet | 879dca9 | 2018-03-23 11:53:24 +0100 | [diff] [blame] | 2598 | if (sa) { |
| 2599 | if (sa->frag_ctx.ctx == ctx) { |
| 2600 | sa->frag_ctx.ctx = NULL; |
| 2601 | spoe_wakeup_appctx(sa->owner); |
| 2602 | } |
| 2603 | else |
| 2604 | sa->cur_fpa--; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2605 | } |
| 2606 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2607 | /* Reset the flag to allow next processing */ |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2608 | agent->rt[tid].processing--; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2609 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2610 | |
| 2611 | /* Reset processing timer */ |
| 2612 | ctx->process_exp = TICK_ETERNITY; |
| 2613 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2614 | spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2615 | |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2616 | ctx->spoe_appctx = NULL; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2617 | ctx->frag_ctx.curmsg = NULL; |
| 2618 | ctx->frag_ctx.curarg = NULL; |
| 2619 | ctx->frag_ctx.curoff = 0; |
| 2620 | ctx->frag_ctx.flags = 0; |
| 2621 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2622 | if (!LIST_ISEMPTY(&ctx->list)) { |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2623 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2624 | _HA_ATOMIC_DEC(&agent->counters.nb_sending); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2625 | else |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2626 | _HA_ATOMIC_DEC(&agent->counters.nb_waiting); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2627 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2628 | LIST_DELETE(&ctx->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2629 | LIST_INIT(&ctx->list); |
| 2630 | } |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2631 | } |
| 2632 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2633 | /* Process a list of SPOE messages. First, this functions will process messages |
| 2634 | * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame |
| 2635 | * to process corresponding actions. During all the processing, it returns 0 |
| 2636 | * and it returns 1 when the processing is finished. If an error occurred, -1 |
| 2637 | * is returned. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2638 | static int |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2639 | spoe_process_messages(struct stream *s, struct spoe_context *ctx, |
| 2640 | struct list *messages, int dir, int type) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2641 | { |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2642 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2643 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2644 | int ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2645 | |
| 2646 | if (ctx->state == SPOE_CTX_ST_ERROR) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2647 | goto end; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2648 | |
| 2649 | if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) { |
| 2650 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2651 | " - failed to process messages: timeout\n", |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2652 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2653 | agent->id, __FUNCTION__, s); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2654 | ctx->status_code = SPOE_CTX_ERR_TOUT; |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2655 | goto end; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2656 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2657 | |
| 2658 | if (ctx->state == SPOE_CTX_ST_READY) { |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2659 | if (agent->eps_max > 0) { |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2660 | 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] | 2661 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2662 | " - skip processing of messages: max EPS reached\n", |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2663 | (int)now.tv_sec, (int)now.tv_usec, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2664 | agent->id, __FUNCTION__, s); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2665 | goto skip; |
| 2666 | } |
| 2667 | } |
| 2668 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2669 | if (!tick_isset(ctx->process_exp)) { |
| 2670 | ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing); |
| 2671 | s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire), |
| 2672 | ctx->process_exp); |
| 2673 | } |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2674 | ret = spoe_start_processing(agent, ctx, dir); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2675 | if (!ret) |
| 2676 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2677 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2678 | ctx->state = SPOE_CTX_ST_ENCODING_MSGS; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2679 | /* fall through */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2680 | } |
| 2681 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2682 | if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) { |
Christopher Faulet | 63263e5 | 2019-04-10 14:47:18 +0200 | [diff] [blame] | 2683 | if (tv_iszero(&ctx->stats.tv_request)) |
| 2684 | ctx->stats.tv_request = now; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2685 | if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait)) |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2686 | goto out; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2687 | ret = spoe_encode_messages(s, ctx, messages, dir, type); |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2688 | if (ret < 0) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2689 | goto end; |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 2690 | if (!ret) |
| 2691 | goto skip; |
Christopher Faulet | 333694d | 2018-01-12 10:45:47 +0100 | [diff] [blame] | 2692 | if (spoe_queue_context(ctx) < 0) |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2693 | goto end; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2694 | ctx->state = SPOE_CTX_ST_SENDING_MSGS; |
| 2695 | } |
| 2696 | |
| 2697 | if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) { |
Christopher Faulet | fce747b | 2018-01-24 15:59:32 +0100 | [diff] [blame] | 2698 | if (ctx->spoe_appctx) |
| 2699 | spoe_wakeup_appctx(ctx->spoe_appctx->owner); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2700 | ret = 0; |
| 2701 | goto out; |
| 2702 | } |
| 2703 | |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 2704 | if (ctx->state == SPOE_CTX_ST_WAITING_ACK) { |
| 2705 | ret = 0; |
| 2706 | goto out; |
| 2707 | } |
| 2708 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2709 | if (ctx->state == SPOE_CTX_ST_DONE) { |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2710 | spoe_process_actions(s, ctx, dir); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2711 | ret = 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2712 | ctx->frame_id++; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2713 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2714 | spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2715 | goto end; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | out: |
| 2719 | return ret; |
| 2720 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2721 | skip: |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2722 | tv_zero(&ctx->stats.tv_start); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2723 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2724 | spoe_stop_processing(agent, ctx); |
| 2725 | return 1; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2726 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2727 | end: |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2728 | spoe_update_stats(s, agent, ctx, dir); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2729 | spoe_stop_processing(agent, ctx); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2730 | if (ctx->status_code) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2731 | _HA_ATOMIC_INC(&agent->counters.nb_errors); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2732 | spoe_handle_processing_error(s, agent, ctx, dir); |
| 2733 | ret = 1; |
| 2734 | } |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2735 | return ret; |
| 2736 | } |
| 2737 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2738 | /* Process a SPOE group, ie the list of messages attached to the group <grp>. |
| 2739 | * See spoe_process_message for details. */ |
| 2740 | static int |
| 2741 | spoe_process_group(struct stream *s, struct spoe_context *ctx, |
| 2742 | struct spoe_group *group, int dir) |
| 2743 | { |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2744 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2745 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2746 | int ret; |
| 2747 | |
| 2748 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 2749 | " - ctx-state=%s - Process messages for group=%s\n", |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2750 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2751 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2752 | group->id); |
| 2753 | |
| 2754 | if (LIST_ISEMPTY(&group->messages)) |
| 2755 | return 1; |
| 2756 | |
| 2757 | 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] | 2758 | if (ret && ctx->stats.t_process != -1) { |
| 2759 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2760 | " - <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu %u/%u\n", |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2761 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2762 | __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code, |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2763 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2764 | ctx->stats.t_response, ctx->stats.t_process, |
| 2765 | agent->counters.idles, agent->counters.applets, |
| 2766 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2767 | agent->counters.nb_errors, agent->counters.nb_processed, |
| 2768 | agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec)); |
| 2769 | if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM)) |
| 2770 | send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING), |
| 2771 | "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n", |
| 2772 | agent->id, group->id, s->uniq_id, ctx->status_code, |
| 2773 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
| 2774 | ctx->stats.t_response, ctx->stats.t_process, |
| 2775 | agent->counters.idles, agent->counters.applets, |
| 2776 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2777 | agent->counters.nb_errors, agent->counters.nb_processed); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2778 | } |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2779 | return ret; |
| 2780 | } |
| 2781 | |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2782 | /* Process a SPOE event, ie the list of messages attached to the event <ev>. |
| 2783 | * See spoe_process_message for details. */ |
| 2784 | static int |
| 2785 | spoe_process_event(struct stream *s, struct spoe_context *ctx, |
| 2786 | enum spoe_event ev) |
| 2787 | { |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2788 | struct spoe_config *conf = FLT_CONF(ctx->filter); |
| 2789 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2790 | int dir, ret; |
| 2791 | |
| 2792 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 2793 | " - ctx-state=%s - Process messages for event=%s\n", |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2794 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | 58d0368 | 2017-09-21 16:57:24 +0200 | [diff] [blame] | 2795 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 2796 | spoe_event_str[ev]); |
| 2797 | |
| 2798 | dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 2799 | |
| 2800 | if (LIST_ISEMPTY(&(ctx->events[ev]))) |
| 2801 | return 1; |
| 2802 | |
| 2803 | 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] | 2804 | if (ret && ctx->stats.t_process != -1) { |
| 2805 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2806 | " - <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu %u/%u\n", |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2807 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 2808 | __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code, |
| 2809 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 2810 | ctx->stats.t_response, ctx->stats.t_process, |
| 2811 | agent->counters.idles, agent->counters.applets, |
| 2812 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2813 | agent->counters.nb_errors, agent->counters.nb_processed, |
| 2814 | agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec)); |
| 2815 | if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM)) |
| 2816 | send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING), |
| 2817 | "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n", |
| 2818 | agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code, |
| 2819 | ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting, |
| 2820 | ctx->stats.t_response, ctx->stats.t_process, |
| 2821 | agent->counters.idles, agent->counters.applets, |
| 2822 | agent->counters.nb_sending, agent->counters.nb_waiting, |
| 2823 | agent->counters.nb_errors, agent->counters.nb_processed); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2824 | } |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2825 | return ret; |
| 2826 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2827 | |
| 2828 | /*************************************************************************** |
| 2829 | * Functions that create/destroy SPOE contexts |
| 2830 | **************************************************************************/ |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2831 | static int |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2832 | spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2833 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2834 | if (buf->size) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2835 | return 1; |
| 2836 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2837 | if (LIST_INLIST(&buffer_wait->list)) |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2838 | LIST_DEL_INIT(&buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2839 | |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 2840 | if (b_alloc(buf)) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2841 | return 1; |
| 2842 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2843 | LIST_APPEND(&ti->buffer_wq, &buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2844 | return 0; |
| 2845 | } |
| 2846 | |
| 2847 | static void |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2848 | spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait) |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2849 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2850 | if (LIST_INLIST(&buffer_wait->list)) |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2851 | LIST_DEL_INIT(&buffer_wait->list); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2852 | |
| 2853 | /* Release the buffer if needed */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2854 | if (buf->size) { |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2855 | b_free(buf); |
Willy Tarreau | 4d77bbf | 2021-02-20 12:02:46 +0100 | [diff] [blame] | 2856 | offer_buffers(buffer_wait->target, 1); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2857 | } |
| 2858 | } |
| 2859 | |
Christopher Faulet | 4596fb7 | 2017-01-11 14:05:19 +0100 | [diff] [blame] | 2860 | static int |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2861 | spoe_wakeup_context(struct spoe_context *ctx) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2862 | { |
| 2863 | task_wakeup(ctx->strm->task, TASK_WOKEN_MSG); |
| 2864 | return 1; |
| 2865 | } |
| 2866 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2867 | static struct spoe_context * |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2868 | spoe_create_context(struct stream *s, struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2869 | { |
| 2870 | struct spoe_config *conf = FLT_CONF(filter); |
| 2871 | struct spoe_context *ctx; |
| 2872 | |
Willy Tarreau | c9ef9bc | 2021-03-22 21:04:50 +0100 | [diff] [blame] | 2873 | ctx = pool_zalloc(pool_head_spoe_ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2874 | if (ctx == NULL) { |
| 2875 | return NULL; |
| 2876 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 2877 | ctx->filter = filter; |
| 2878 | ctx->state = SPOE_CTX_ST_NONE; |
| 2879 | ctx->status_code = SPOE_CTX_ERR_NONE; |
| 2880 | ctx->flags = 0; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 2881 | ctx->events = conf->agent->events; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 2882 | ctx->groups = &conf->agent->groups; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2883 | ctx->buffer = BUF_NULL; |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 2884 | LIST_INIT(&ctx->buffer_wait.list); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2885 | ctx->buffer_wait.target = ctx; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2886 | ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 2887 | LIST_INIT(&ctx->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2888 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 2889 | ctx->stream_id = 0; |
| 2890 | ctx->frame_id = 1; |
| 2891 | ctx->process_exp = TICK_ETERNITY; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2892 | |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2893 | tv_zero(&ctx->stats.tv_start); |
| 2894 | tv_zero(&ctx->stats.tv_request); |
| 2895 | tv_zero(&ctx->stats.tv_queue); |
| 2896 | tv_zero(&ctx->stats.tv_wait); |
| 2897 | tv_zero(&ctx->stats.tv_response); |
| 2898 | ctx->stats.t_request = -1; |
| 2899 | ctx->stats.t_queue = -1; |
| 2900 | ctx->stats.t_waiting = -1; |
| 2901 | ctx->stats.t_response = -1; |
| 2902 | ctx->stats.t_process = -1; |
| 2903 | ctx->stats.t_total = 0; |
| 2904 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2905 | ctx->strm = s; |
| 2906 | ctx->state = SPOE_CTX_ST_READY; |
| 2907 | filter->ctx = ctx; |
| 2908 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2909 | return ctx; |
| 2910 | } |
| 2911 | |
| 2912 | static void |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2913 | spoe_destroy_context(struct filter *filter) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2914 | { |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2915 | struct spoe_config *conf = FLT_CONF(filter); |
| 2916 | struct spoe_context *ctx = filter->ctx; |
| 2917 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2918 | if (!ctx) |
| 2919 | return; |
| 2920 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2921 | spoe_stop_processing(conf->agent, ctx); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2922 | pool_free(pool_head_spoe_ctx, ctx); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 2923 | filter->ctx = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2924 | } |
| 2925 | |
| 2926 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2927 | spoe_reset_context(struct spoe_context *ctx) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2928 | { |
| 2929 | ctx->state = SPOE_CTX_ST_READY; |
Christopher Faulet | f032c3e | 2017-02-17 15:18:35 +0100 | [diff] [blame] | 2930 | ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED); |
Christopher Faulet | b2dd1e0 | 2018-03-22 09:07:41 +0100 | [diff] [blame] | 2931 | |
| 2932 | tv_zero(&ctx->stats.tv_start); |
| 2933 | tv_zero(&ctx->stats.tv_request); |
| 2934 | tv_zero(&ctx->stats.tv_queue); |
| 2935 | tv_zero(&ctx->stats.tv_wait); |
| 2936 | tv_zero(&ctx->stats.tv_response); |
| 2937 | ctx->stats.t_request = -1; |
| 2938 | ctx->stats.t_queue = -1; |
| 2939 | ctx->stats.t_waiting = -1; |
| 2940 | ctx->stats.t_response = -1; |
| 2941 | ctx->stats.t_process = -1; |
| 2942 | ctx->stats.t_total = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | |
| 2946 | /*************************************************************************** |
| 2947 | * Hooks that manage the filter lifecycle (init/check/deinit) |
| 2948 | **************************************************************************/ |
| 2949 | /* Signal handler: Do a soft stop, wakeup SPOE applet */ |
| 2950 | static void |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 2951 | spoe_sig_stop(struct sig_handler *sh) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2952 | { |
| 2953 | struct proxy *p; |
| 2954 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 2955 | p = proxies_list; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2956 | while (p) { |
| 2957 | struct flt_conf *fconf; |
| 2958 | |
| 2959 | list_for_each_entry(fconf, &p->filter_configs, list) { |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 2960 | struct spoe_config *conf; |
| 2961 | struct spoe_agent *agent; |
Christopher Faulet | 42bfa46 | 2017-01-04 14:14:19 +0100 | [diff] [blame] | 2962 | struct spoe_appctx *spoe_appctx; |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2963 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2964 | |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 2965 | if (fconf->id != spoe_filter_id) |
| 2966 | continue; |
| 2967 | |
| 2968 | conf = fconf->conf; |
| 2969 | agent = conf->agent; |
| 2970 | |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2971 | for (i = 0; i < global.nbthread; ++i) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2972 | HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock); |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 2973 | list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list) |
| 2974 | spoe_wakeup_appctx(spoe_appctx->owner); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2975 | HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2976 | } |
| 2977 | } |
| 2978 | p = p->next; |
| 2979 | } |
| 2980 | } |
| 2981 | |
| 2982 | |
| 2983 | /* Initialize the SPOE filter. Returns -1 on error, else 0. */ |
| 2984 | static int |
| 2985 | spoe_init(struct proxy *px, struct flt_conf *fconf) |
| 2986 | { |
| 2987 | struct spoe_config *conf = fconf->conf; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2988 | |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 2989 | /* conf->agent_fe was already initialized during the config |
| 2990 | * parsing. Finish initialization. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2991 | conf->agent_fe.last_change = now.tv_sec; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 2992 | conf->agent_fe.cap = PR_CAP_FE; |
| 2993 | conf->agent_fe.mode = PR_MODE_TCP; |
| 2994 | conf->agent_fe.maxconn = 0; |
| 2995 | conf->agent_fe.options2 |= PR_O2_INDEPSTR; |
| 2996 | conf->agent_fe.conn_retries = CONN_RETRIES; |
| 2997 | conf->agent_fe.accept = frontend_accept; |
| 2998 | conf->agent_fe.srv = NULL; |
| 2999 | conf->agent_fe.timeout.client = TICK_ETERNITY; |
| 3000 | conf->agent_fe.default_target = &spoe_applet.obj_type; |
| 3001 | conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES; |
| 3002 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3003 | if (!sighandler_registered) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3004 | signal_register_fct(0, spoe_sig_stop, 0); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3005 | sighandler_registered = 1; |
| 3006 | } |
| 3007 | |
Christopher Faulet | 0029235 | 2019-01-09 15:05:10 +0100 | [diff] [blame] | 3008 | fconf->flags |= FLT_CFG_FL_HTX; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3009 | return 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3010 | } |
| 3011 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3012 | /* Free resources allocated by the SPOE filter. */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3013 | static void |
| 3014 | spoe_deinit(struct proxy *px, struct flt_conf *fconf) |
| 3015 | { |
| 3016 | struct spoe_config *conf = fconf->conf; |
| 3017 | |
| 3018 | if (conf) { |
| 3019 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3020 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3021 | spoe_release_agent(agent); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3022 | free(conf->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3023 | free(conf); |
| 3024 | } |
| 3025 | fconf->conf = NULL; |
| 3026 | } |
| 3027 | |
| 3028 | /* Check configuration of a SPOE filter for a specified proxy. |
| 3029 | * Return 1 on error, else 0. */ |
| 3030 | static int |
| 3031 | spoe_check(struct proxy *px, struct flt_conf *fconf) |
| 3032 | { |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3033 | struct flt_conf *f; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3034 | struct spoe_config *conf = fconf->conf; |
| 3035 | struct proxy *target; |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3036 | struct logsrv *logsrv; |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3037 | int i; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3038 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3039 | /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names |
| 3040 | * are uniq */ |
| 3041 | list_for_each_entry(f, &px->filter_configs, list) { |
| 3042 | struct spoe_config *c = f->conf; |
| 3043 | |
| 3044 | /* This is not an SPOE filter */ |
| 3045 | if (f->id != spoe_filter_id) |
| 3046 | continue; |
| 3047 | /* This is the current SPOE filter */ |
| 3048 | if (f == fconf) |
| 3049 | continue; |
| 3050 | |
| 3051 | /* Check engine Id. It should be uniq */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3052 | if (strcmp(conf->id, c->id) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3053 | ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n", |
| 3054 | px->id, conf->id); |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 3055 | return 1; |
| 3056 | } |
| 3057 | } |
| 3058 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3059 | target = proxy_be_by_name(conf->agent->b.name); |
| 3060 | if (target == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3061 | ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'" |
| 3062 | " declared at %s:%d.\n", |
| 3063 | px->id, conf->agent->b.name, conf->agent->id, |
| 3064 | conf->agent->conf.file, conf->agent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3065 | return 1; |
| 3066 | } |
| 3067 | if (target->mode != PR_MODE_TCP) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3068 | ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared" |
| 3069 | " at %s:%d does not support HTTP mode.\n", |
| 3070 | px->id, target->id, conf->agent->id, |
| 3071 | conf->agent->conf.file, conf->agent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3072 | return 1; |
| 3073 | } |
| 3074 | |
Willy Tarreau | 2bdcfde | 2019-02-05 13:37:19 +0100 | [diff] [blame] | 3075 | if (px->bind_proc & ~target->bind_proc) { |
| 3076 | ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared" |
| 3077 | " at %s:%d does not cover all of its processes.\n", |
| 3078 | px->id, target->id, conf->agent->id, |
| 3079 | conf->agent->conf.file, conf->agent->conf.line); |
| 3080 | return 1; |
| 3081 | } |
| 3082 | |
Christopher Faulet | fe26155 | 2019-03-18 13:57:42 +0100 | [diff] [blame] | 3083 | if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) { |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3084 | ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n", |
| 3085 | px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line); |
| 3086 | return 1; |
| 3087 | } |
| 3088 | for (i = 0; i < global.nbthread; ++i) { |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3089 | conf->agent->rt[i].engine_id = NULL; |
Christopher Faulet | fe26155 | 2019-03-18 13:57:42 +0100 | [diff] [blame] | 3090 | conf->agent->rt[i].frame_size = conf->agent->max_frame_size; |
| 3091 | conf->agent->rt[i].processing = 0; |
| 3092 | LIST_INIT(&conf->agent->rt[i].applets); |
| 3093 | LIST_INIT(&conf->agent->rt[i].sending_queue); |
| 3094 | LIST_INIT(&conf->agent->rt[i].waiting_queue); |
| 3095 | HA_SPIN_INIT(&conf->agent->rt[i].lock); |
Willy Tarreau | b0769b2 | 2019-02-07 13:40:33 +0100 | [diff] [blame] | 3096 | } |
| 3097 | |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3098 | list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) { |
| 3099 | if (logsrv->type == LOG_TARGET_BUFFER) { |
| 3100 | struct sink *sink = sink_find(logsrv->ring_name); |
| 3101 | |
| 3102 | if (!sink || sink->type != SINK_TYPE_BUFFER) { |
| 3103 | ha_alert("Proxy %s : log server used by SPOE agent '%s' declared" |
Ilya Shipitsin | d7a988c | 2021-03-04 23:26:15 +0500 | [diff] [blame] | 3104 | " at %s:%d uses unknown ring named '%s'.\n", |
Christopher Faulet | 1d7d0f8 | 2021-02-19 10:56:41 +0100 | [diff] [blame] | 3105 | px->id, conf->agent->id, conf->agent->conf.file, |
| 3106 | conf->agent->conf.line, logsrv->ring_name); |
| 3107 | return 1; |
| 3108 | } |
| 3109 | logsrv->sink = sink; |
| 3110 | } |
| 3111 | } |
| 3112 | |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3113 | ha_free(&conf->agent->b.name); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3114 | conf->agent->b.be = target; |
| 3115 | return 0; |
| 3116 | } |
| 3117 | |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3118 | /* Initializes the SPOE filter for a proxy for a specific thread. |
| 3119 | * Returns a negative value if an error occurs. */ |
| 3120 | static int |
| 3121 | spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf) |
| 3122 | { |
| 3123 | struct spoe_config *conf = fconf->conf; |
| 3124 | struct spoe_agent *agent = conf->agent; |
| 3125 | |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3126 | agent->rt[tid].engine_id = generate_pseudo_uuid(); |
| 3127 | if (agent->rt[tid].engine_id == NULL) |
| 3128 | return -1; |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3129 | return 0; |
| 3130 | } |
| 3131 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3132 | /************************************************************************** |
| 3133 | * Hooks attached to a stream |
| 3134 | *************************************************************************/ |
| 3135 | /* Called when a filter instance is created and attach to a stream. It creates |
| 3136 | * the context that will be used to process this stream. */ |
| 3137 | static int |
| 3138 | spoe_start(struct stream *s, struct filter *filter) |
| 3139 | { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3140 | struct spoe_config *conf = FLT_CONF(filter); |
| 3141 | struct spoe_agent *agent = conf->agent; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3142 | struct spoe_context *ctx; |
| 3143 | |
| 3144 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3145 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3146 | __FUNCTION__, s); |
| 3147 | |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 3148 | if ((ctx = spoe_create_context(s, filter)) == NULL) { |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3149 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 3150 | " - failed to create SPOE context\n", |
| 3151 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
Christopher Faulet | ccbc3fd | 2017-09-15 11:51:18 +0200 | [diff] [blame] | 3152 | __FUNCTION__, s); |
Christopher Faulet | 3b8e349 | 2018-03-26 17:20:58 +0200 | [diff] [blame] | 3153 | send_log(&conf->agent_fe, LOG_EMERG, |
Christopher Faulet | 72bcc47 | 2017-01-04 16:39:41 +0100 | [diff] [blame] | 3154 | "SPOE: [%s] failed to create SPOE context\n", |
| 3155 | agent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3156 | return 0; |
| 3157 | } |
| 3158 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3159 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3160 | filter->pre_analyzers |= AN_REQ_INSPECT_FE; |
| 3161 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3162 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3163 | filter->pre_analyzers |= AN_REQ_INSPECT_BE; |
| 3164 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3165 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3166 | filter->pre_analyzers |= AN_RES_INSPECT; |
| 3167 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3168 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3169 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE; |
| 3170 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3171 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3172 | filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE; |
| 3173 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3174 | if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP])) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3175 | filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE; |
| 3176 | |
| 3177 | return 1; |
| 3178 | } |
| 3179 | |
| 3180 | /* Called when a filter instance is detached from a stream. It release the |
| 3181 | * attached SPOE context. */ |
| 3182 | static void |
| 3183 | spoe_stop(struct stream *s, struct filter *filter) |
| 3184 | { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3185 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n", |
| 3186 | (int)now.tv_sec, (int)now.tv_usec, |
| 3187 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3188 | __FUNCTION__, s); |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 3189 | spoe_destroy_context(filter); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3190 | } |
| 3191 | |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3192 | |
| 3193 | /* |
| 3194 | * Called when the stream is woken up because of expired timer. |
| 3195 | */ |
| 3196 | static void |
| 3197 | spoe_check_timeouts(struct stream *s, struct filter *filter) |
| 3198 | { |
| 3199 | struct spoe_context *ctx = filter->ctx; |
| 3200 | |
Christopher Faulet | ac58060 | 2018-03-20 16:09:20 +0100 | [diff] [blame] | 3201 | if (tick_is_expired(ctx->process_exp, now_ms)) |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3202 | s->pending_events |= TASK_WOKEN_MSG; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3203 | } |
| 3204 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3205 | /* Called when we are ready to filter data on a channel */ |
| 3206 | static int |
| 3207 | spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 3208 | { |
| 3209 | struct spoe_context *ctx = filter->ctx; |
| 3210 | int ret = 1; |
| 3211 | |
| 3212 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3213 | " - ctx-flags=0x%08x\n", |
| 3214 | (int)now.tv_sec, (int)now.tv_usec, |
| 3215 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3216 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 3217 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3218 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 3219 | goto out; |
| 3220 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3221 | if (!(chn->flags & CF_ISRESP)) { |
| 3222 | if (filter->pre_analyzers & AN_REQ_INSPECT_FE) |
| 3223 | chn->analysers |= AN_REQ_INSPECT_FE; |
| 3224 | if (filter->pre_analyzers & AN_REQ_INSPECT_BE) |
| 3225 | chn->analysers |= AN_REQ_INSPECT_BE; |
| 3226 | |
| 3227 | if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED) |
| 3228 | goto out; |
| 3229 | |
| 3230 | ctx->stream_id = s->uniq_id; |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3231 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3232 | if (!ret) |
| 3233 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3234 | ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED; |
| 3235 | } |
| 3236 | else { |
Miroslav Zagorac | 8840326 | 2020-06-19 22:17:17 +0200 | [diff] [blame] | 3237 | if (filter->pre_analyzers & AN_RES_INSPECT) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3238 | chn->analysers |= AN_RES_INSPECT; |
| 3239 | |
| 3240 | if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED) |
| 3241 | goto out; |
| 3242 | |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3243 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3244 | if (!ret) { |
| 3245 | channel_dont_read(chn); |
| 3246 | channel_dont_close(chn); |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3247 | goto out; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3248 | } |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3249 | ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3250 | } |
| 3251 | |
| 3252 | out: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3253 | return ret; |
| 3254 | } |
| 3255 | |
| 3256 | /* Called before a processing happens on a given channel */ |
| 3257 | static int |
| 3258 | spoe_chn_pre_analyze(struct stream *s, struct filter *filter, |
| 3259 | struct channel *chn, unsigned an_bit) |
| 3260 | { |
| 3261 | struct spoe_context *ctx = filter->ctx; |
| 3262 | int ret = 1; |
| 3263 | |
| 3264 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3265 | " - ctx-flags=0x%08x - ana=0x%08x\n", |
| 3266 | (int)now.tv_sec, (int)now.tv_usec, |
| 3267 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3268 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], |
| 3269 | ctx->flags, an_bit); |
| 3270 | |
Christopher Faulet | b067b06 | 2017-01-04 16:39:11 +0100 | [diff] [blame] | 3271 | if (ctx->state == SPOE_CTX_ST_NONE) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3272 | goto out; |
| 3273 | |
| 3274 | switch (an_bit) { |
| 3275 | case AN_REQ_INSPECT_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3276 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3277 | break; |
| 3278 | case AN_REQ_INSPECT_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3279 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3280 | break; |
| 3281 | case AN_RES_INSPECT: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3282 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3283 | break; |
| 3284 | case AN_REQ_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3285 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3286 | break; |
| 3287 | case AN_REQ_HTTP_PROCESS_BE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3288 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3289 | break; |
| 3290 | case AN_RES_HTTP_PROCESS_FE: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3291 | ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3292 | break; |
| 3293 | } |
| 3294 | |
| 3295 | out: |
Christopher Faulet | 9cdca97 | 2018-02-01 08:45:45 +0100 | [diff] [blame] | 3296 | if (!ret && (chn->flags & CF_ISRESP)) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3297 | channel_dont_read(chn); |
| 3298 | channel_dont_close(chn); |
| 3299 | } |
| 3300 | return ret; |
| 3301 | } |
| 3302 | |
| 3303 | /* Called when the filtering on the channel ends. */ |
| 3304 | static int |
| 3305 | spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 3306 | { |
| 3307 | struct spoe_context *ctx = filter->ctx; |
| 3308 | |
| 3309 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s" |
| 3310 | " - ctx-flags=0x%08x\n", |
| 3311 | (int)now.tv_sec, (int)now.tv_usec, |
| 3312 | ((struct spoe_config *)FLT_CONF(filter))->agent->id, |
| 3313 | __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags); |
| 3314 | |
| 3315 | if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) { |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 3316 | spoe_reset_context(ctx); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | return 1; |
| 3320 | } |
| 3321 | |
| 3322 | /******************************************************************** |
| 3323 | * Functions that manage the filter initialization |
| 3324 | ********************************************************************/ |
| 3325 | struct flt_ops spoe_ops = { |
| 3326 | /* Manage SPOE filter, called for each filter declaration */ |
| 3327 | .init = spoe_init, |
| 3328 | .deinit = spoe_deinit, |
| 3329 | .check = spoe_check, |
Kevin Zhu | d87b1a5 | 2019-09-17 15:05:45 +0200 | [diff] [blame] | 3330 | .init_per_thread = spoe_init_per_thread, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3331 | |
| 3332 | /* Handle start/stop of SPOE */ |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3333 | .attach = spoe_start, |
| 3334 | .detach = spoe_stop, |
| 3335 | .check_timeouts = spoe_check_timeouts, |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3336 | |
| 3337 | /* Handle channels activity */ |
| 3338 | .channel_start_analyze = spoe_start_analyze, |
| 3339 | .channel_pre_analyze = spoe_chn_pre_analyze, |
| 3340 | .channel_end_analyze = spoe_end_analyze, |
| 3341 | }; |
| 3342 | |
| 3343 | |
| 3344 | static int |
| 3345 | cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm) |
| 3346 | { |
| 3347 | const char *err; |
| 3348 | int i, err_code = 0; |
| 3349 | |
| 3350 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3351 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3352 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3353 | goto out; |
| 3354 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3355 | if (strcmp(args[0], "spoe-agent") == 0) { /* new spoe-agent section */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3356 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3357 | ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n", |
| 3358 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3359 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3360 | goto out; |
| 3361 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3362 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3363 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3364 | goto out; |
| 3365 | } |
| 3366 | |
| 3367 | err = invalid_char(args[1]); |
| 3368 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3369 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3370 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3371 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3372 | goto out; |
| 3373 | } |
| 3374 | |
| 3375 | if (curagent != NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3376 | ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n", |
| 3377 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3378 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3379 | goto out; |
| 3380 | } |
| 3381 | if ((curagent = calloc(1, sizeof(*curagent))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3382 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3383 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3384 | goto out; |
| 3385 | } |
| 3386 | |
| 3387 | curagent->id = strdup(args[1]); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3388 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3389 | curagent->conf.file = strdup(file); |
| 3390 | curagent->conf.line = linenum; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3391 | |
| 3392 | curagent->timeout.hello = TICK_ETERNITY; |
| 3393 | curagent->timeout.idle = TICK_ETERNITY; |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3394 | curagent->timeout.processing = TICK_ETERNITY; |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3395 | |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3396 | curagent->var_pfx = NULL; |
| 3397 | curagent->var_on_error = NULL; |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3398 | curagent->var_t_process = NULL; |
| 3399 | curagent->var_t_total = NULL; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3400 | curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION); |
Christopher Faulet | a1cda02 | 2016-12-21 08:58:06 +0100 | [diff] [blame] | 3401 | curagent->cps_max = 0; |
| 3402 | curagent->eps_max = 0; |
Christopher Faulet | 7aa0b2b | 2017-01-13 11:30:50 +0100 | [diff] [blame] | 3403 | curagent->max_frame_size = MAX_FRAME_SIZE; |
Christopher Faulet | b077cdc | 2018-01-24 16:37:57 +0100 | [diff] [blame] | 3404 | curagent->max_fpa = 20; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3405 | |
| 3406 | for (i = 0; i < SPOE_EV_EVENTS; ++i) |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3407 | LIST_INIT(&curagent->events[i]); |
| 3408 | LIST_INIT(&curagent->groups); |
| 3409 | LIST_INIT(&curagent->messages); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3410 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3411 | else if (strcmp(args[0], "use-backend") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3412 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3413 | ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n", |
| 3414 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3415 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3416 | goto out; |
| 3417 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3418 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3419 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3420 | free(curagent->b.name); |
| 3421 | curagent->b.name = strdup(args[1]); |
| 3422 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3423 | else if (strcmp(args[0], "messages") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3424 | int cur_arg = 1; |
| 3425 | while (*args[cur_arg]) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3426 | struct spoe_placeholder *ph = NULL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3427 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3428 | list_for_each_entry(ph, &curmphs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3429 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3430 | ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3431 | file, linenum, args[cur_arg]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3432 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3433 | goto out; |
| 3434 | } |
| 3435 | } |
| 3436 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3437 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3438 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3439 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3440 | goto out; |
| 3441 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3442 | ph->id = strdup(args[cur_arg]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3443 | LIST_APPEND(&curmphs, &ph->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3444 | cur_arg++; |
| 3445 | } |
| 3446 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3447 | else if (strcmp(args[0], "groups") == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3448 | int cur_arg = 1; |
| 3449 | while (*args[cur_arg]) { |
| 3450 | struct spoe_placeholder *ph = NULL; |
| 3451 | |
| 3452 | list_for_each_entry(ph, &curgphs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3453 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3454 | ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n", |
| 3455 | file, linenum, args[cur_arg]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3456 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3457 | goto out; |
| 3458 | } |
| 3459 | } |
| 3460 | |
| 3461 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3462 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3463 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3464 | goto out; |
| 3465 | } |
| 3466 | ph->id = strdup(args[cur_arg]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3467 | LIST_APPEND(&curgphs, &ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3468 | cur_arg++; |
| 3469 | } |
| 3470 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3471 | else if (strcmp(args[0], "timeout") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3472 | unsigned int *tv = NULL; |
| 3473 | const char *res; |
| 3474 | unsigned timeout; |
| 3475 | |
| 3476 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3477 | ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n", |
| 3478 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3479 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3480 | goto out; |
| 3481 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3482 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3483 | goto out; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3484 | if (strcmp(args[1], "hello") == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3485 | tv = &curagent->timeout.hello; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3486 | else if (strcmp(args[1], "idle") == 0) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3487 | tv = &curagent->timeout.idle; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3488 | else if (strcmp(args[1], "processing") == 0) |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 3489 | tv = &curagent->timeout.processing; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3490 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3491 | ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n", |
| 3492 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3493 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3494 | goto out; |
| 3495 | } |
| 3496 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3497 | ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n", |
| 3498 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3499 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3500 | goto out; |
| 3501 | } |
| 3502 | res = parse_time_err(args[2], &timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 3503 | if (res == PARSE_TIME_OVER) { |
| 3504 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n", |
| 3505 | file, linenum, args[2], args[0], args[1]); |
| 3506 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3507 | goto out; |
| 3508 | } |
| 3509 | else if (res == PARSE_TIME_UNDER) { |
| 3510 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n", |
| 3511 | file, linenum, args[2], args[0], args[1]); |
| 3512 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3513 | goto out; |
| 3514 | } |
| 3515 | else if (res) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3516 | ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n", |
| 3517 | file, linenum, *res, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3518 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3519 | goto out; |
| 3520 | } |
| 3521 | *tv = MS_TO_TICKS(timeout); |
| 3522 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3523 | else if (strcmp(args[0], "option") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3524 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3525 | ha_alert("parsing [%s:%d]: '%s' expects an option name.\n", |
| 3526 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3527 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3528 | goto out; |
| 3529 | } |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3530 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3531 | if (strcmp(args[1], "pipelining") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3532 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3533 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3534 | if (kwm == 1) |
| 3535 | curagent->flags &= ~SPOE_FL_PIPELINING; |
| 3536 | else |
| 3537 | curagent->flags |= SPOE_FL_PIPELINING; |
| 3538 | goto out; |
| 3539 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3540 | else if (strcmp(args[1], "async") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3541 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3542 | goto out; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3543 | if (kwm == 1) |
| 3544 | curagent->flags &= ~SPOE_FL_ASYNC; |
Christopher Faulet | b1bb1af | 2019-09-17 11:55:52 +0200 | [diff] [blame] | 3545 | else |
| 3546 | curagent->flags |= SPOE_FL_ASYNC; |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3547 | goto out; |
| 3548 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3549 | else if (strcmp(args[1], "send-frag-payload") == 0) { |
Christopher Faulet | cecd852 | 2017-02-24 22:11:21 +0100 | [diff] [blame] | 3550 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3551 | goto out; |
| 3552 | if (kwm == 1) |
| 3553 | curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION; |
| 3554 | else |
| 3555 | curagent->flags |= SPOE_FL_SND_FRAGMENTATION; |
| 3556 | goto out; |
| 3557 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3558 | else if (strcmp(args[1], "dontlog-normal") == 0) { |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 3559 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3560 | goto out; |
| 3561 | if (kwm == 1) |
| 3562 | curpxopts2 &= ~PR_O2_NOLOGNORM; |
| 3563 | else |
| 3564 | curpxopts2 |= PR_O2_NOLOGNORM; |
Christopher Faulet | 799f518 | 2018-04-26 11:36:34 +0200 | [diff] [blame] | 3565 | goto out; |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 3566 | } |
Christopher Faulet | 305c607 | 2017-02-23 16:17:53 +0100 | [diff] [blame] | 3567 | |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3568 | /* Following options does not support negation */ |
| 3569 | if (kwm == 1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3570 | ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n", |
| 3571 | file, linenum, args[1]); |
Christopher Faulet | 6a2940c | 2017-02-23 15:06:26 +0100 | [diff] [blame] | 3572 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3573 | goto out; |
| 3574 | } |
| 3575 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3576 | if (strcmp(args[1], "var-prefix") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3577 | char *tmp; |
| 3578 | |
| 3579 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3580 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3581 | file, linenum, args[0], |
| 3582 | args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3583 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3584 | goto out; |
| 3585 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3586 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3587 | goto out; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3588 | tmp = args[2]; |
| 3589 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3590 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3591 | 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] | 3592 | file, linenum, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3593 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3594 | goto out; |
| 3595 | } |
| 3596 | tmp++; |
| 3597 | } |
| 3598 | curagent->var_pfx = strdup(args[2]); |
| 3599 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3600 | else if (strcmp(args[1], "force-set-var") == 0) { |
Etienne Carriere | aec8989 | 2017-12-14 09:36:40 +0000 | [diff] [blame] | 3601 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3602 | goto out; |
| 3603 | curagent->flags |= SPOE_FL_FORCE_SET_VAR; |
| 3604 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3605 | else if (strcmp(args[1], "continue-on-error") == 0) { |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3606 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3607 | goto out; |
Christopher Faulet | ea62c2a | 2016-11-14 10:54:21 +0100 | [diff] [blame] | 3608 | curagent->flags |= SPOE_FL_CONT_ON_ERR; |
| 3609 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3610 | else if (strcmp(args[1], "set-on-error") == 0) { |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3611 | char *tmp; |
| 3612 | |
| 3613 | if (!*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3614 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3615 | file, linenum, args[0], |
| 3616 | args[1]); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3617 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3618 | goto out; |
| 3619 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3620 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3621 | goto out; |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3622 | tmp = args[2]; |
| 3623 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3624 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3625 | ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3626 | file, linenum, args[0], args[1]); |
Christopher Faulet | 985532d | 2016-11-16 15:36:19 +0100 | [diff] [blame] | 3627 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3628 | goto out; |
| 3629 | } |
| 3630 | tmp++; |
| 3631 | } |
| 3632 | curagent->var_on_error = strdup(args[2]); |
| 3633 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3634 | else if (strcmp(args[1], "set-process-time") == 0) { |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3635 | char *tmp; |
| 3636 | |
| 3637 | if (!*args[2]) { |
| 3638 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3639 | file, linenum, args[0], |
| 3640 | args[1]); |
| 3641 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3642 | goto out; |
| 3643 | } |
| 3644 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3645 | goto out; |
| 3646 | tmp = args[2]; |
| 3647 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3648 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3649 | 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] | 3650 | file, linenum, args[0], args[1]); |
| 3651 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3652 | goto out; |
| 3653 | } |
| 3654 | tmp++; |
| 3655 | } |
| 3656 | curagent->var_t_process = strdup(args[2]); |
| 3657 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3658 | else if (strcmp(args[1], "set-total-time") == 0) { |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 3659 | char *tmp; |
| 3660 | |
| 3661 | if (!*args[2]) { |
| 3662 | ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n", |
| 3663 | file, linenum, args[0], |
| 3664 | args[1]); |
| 3665 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3666 | goto out; |
| 3667 | } |
| 3668 | if (alertif_too_many_args(2, file, linenum, args, &err_code)) |
| 3669 | goto out; |
| 3670 | tmp = args[2]; |
| 3671 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 3672 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Thierry FOURNIER | 01a3f20 | 2018-05-10 16:41:26 +0200 | [diff] [blame] | 3673 | 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] | 3674 | file, linenum, args[0], args[1]); |
| 3675 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3676 | goto out; |
| 3677 | } |
| 3678 | tmp++; |
| 3679 | } |
| 3680 | curagent->var_t_total = strdup(args[2]); |
| 3681 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3682 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3683 | ha_alert("parsing [%s:%d]: option '%s' is not supported.\n", |
| 3684 | file, linenum, args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3685 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3686 | goto out; |
| 3687 | } |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3688 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3689 | else if (strcmp(args[0], "maxconnrate") == 0) { |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3690 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3691 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3692 | file, linenum, args[0]); |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3693 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3694 | goto out; |
| 3695 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3696 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3697 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3698 | curagent->cps_max = atol(args[1]); |
| 3699 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3700 | else if (strcmp(args[0], "maxerrrate") == 0) { |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3701 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3702 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3703 | file, linenum, args[0]); |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3704 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3705 | goto out; |
| 3706 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3707 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3708 | goto out; |
Christopher Faulet | 4802672 | 2016-11-16 15:01:12 +0100 | [diff] [blame] | 3709 | curagent->eps_max = atol(args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3710 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3711 | else if (strcmp(args[0], "max-frame-size") == 0) { |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3712 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3713 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3714 | file, linenum, args[0]); |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3715 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3716 | goto out; |
| 3717 | } |
| 3718 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3719 | goto out; |
| 3720 | curagent->max_frame_size = atol(args[1]); |
| 3721 | if (curagent->max_frame_size < MIN_FRAME_SIZE || |
| 3722 | curagent->max_frame_size > MAX_FRAME_SIZE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3723 | ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n", |
| 3724 | file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE); |
Christopher Faulet | 2eca6b5 | 2017-02-27 09:40:34 +0100 | [diff] [blame] | 3725 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3726 | goto out; |
| 3727 | } |
| 3728 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3729 | else if (strcmp(args[0], "max-waiting-frames") == 0) { |
Christopher Faulet | e8ade38 | 2018-01-25 15:32:22 +0100 | [diff] [blame] | 3730 | if (!*args[1]) { |
| 3731 | ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n", |
| 3732 | file, linenum, args[0]); |
| 3733 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3734 | goto out; |
| 3735 | } |
| 3736 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) |
| 3737 | goto out; |
| 3738 | curagent->max_fpa = atol(args[1]); |
| 3739 | if (curagent->max_fpa < 1) { |
| 3740 | ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n", |
| 3741 | file, linenum, args[0]); |
| 3742 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3743 | goto out; |
| 3744 | } |
| 3745 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3746 | else if (strcmp(args[0], "register-var-names") == 0) { |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3747 | int cur_arg; |
| 3748 | |
| 3749 | if (!*args[1]) { |
| 3750 | ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n", |
| 3751 | file, linenum, args[0]); |
| 3752 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3753 | goto out; |
| 3754 | } |
| 3755 | cur_arg = 1; |
| 3756 | while (*args[cur_arg]) { |
| 3757 | struct spoe_var_placeholder *vph; |
| 3758 | |
| 3759 | if ((vph = calloc(1, sizeof(*vph))) == NULL) { |
| 3760 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3761 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3762 | goto out; |
| 3763 | } |
| 3764 | if ((vph->name = strdup(args[cur_arg])) == NULL) { |
Tim Duesterhus | b298613 | 2019-06-23 22:10:13 +0200 | [diff] [blame] | 3765 | free(vph); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3766 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 3767 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3768 | goto out; |
| 3769 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3770 | LIST_APPEND(&curvars, &vph->list); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 3771 | cur_arg++; |
| 3772 | } |
| 3773 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3774 | else if (strcmp(args[0], "log") == 0) { |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3775 | char *errmsg = NULL; |
| 3776 | |
Emeric Brun | 9533a70 | 2021-04-02 10:13:43 +0200 | [diff] [blame] | 3777 | if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), file, linenum, &errmsg)) { |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 3778 | ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg); |
| 3779 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3780 | goto out; |
| 3781 | } |
| 3782 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3783 | else if (*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3784 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n", |
| 3785 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3786 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3787 | goto out; |
| 3788 | } |
| 3789 | out: |
| 3790 | return err_code; |
| 3791 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3792 | static int |
| 3793 | cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm) |
| 3794 | { |
| 3795 | struct spoe_group *grp; |
| 3796 | const char *err; |
| 3797 | int err_code = 0; |
| 3798 | |
| 3799 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3800 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3801 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3802 | goto out; |
| 3803 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3804 | if (strcmp(args[0], "spoe-group") == 0) { /* new spoe-group section */ |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3805 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3806 | ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n", |
| 3807 | file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3808 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3809 | goto out; |
| 3810 | } |
| 3811 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3812 | err_code |= ERR_ABORT; |
| 3813 | goto out; |
| 3814 | } |
| 3815 | |
| 3816 | err = invalid_char(args[1]); |
| 3817 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3818 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3819 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3820 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3821 | goto out; |
| 3822 | } |
| 3823 | |
| 3824 | list_for_each_entry(grp, &curgrps, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3825 | if (strcmp(grp->id, args[1]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3826 | ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same" |
| 3827 | " name as another one declared at %s:%d.\n", |
| 3828 | file, linenum, args[1], grp->conf.file, grp->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3829 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3830 | goto out; |
| 3831 | } |
| 3832 | } |
| 3833 | |
| 3834 | if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3835 | ha_alert("parsing [%s:%d] : out of memory.\n", 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 | |
| 3840 | curgrp->id = strdup(args[1]); |
| 3841 | curgrp->conf.file = strdup(file); |
| 3842 | curgrp->conf.line = linenum; |
| 3843 | LIST_INIT(&curgrp->phs); |
| 3844 | LIST_INIT(&curgrp->messages); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3845 | LIST_APPEND(&curgrps, &curgrp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3846 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3847 | else if (strcmp(args[0], "messages") == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3848 | int cur_arg = 1; |
| 3849 | while (*args[cur_arg]) { |
| 3850 | struct spoe_placeholder *ph = NULL; |
| 3851 | |
| 3852 | list_for_each_entry(ph, &curgrp->phs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3853 | if (strcmp(ph->id, args[cur_arg]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3854 | ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n", |
| 3855 | file, linenum, args[cur_arg]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3856 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3857 | goto out; |
| 3858 | } |
| 3859 | } |
| 3860 | |
| 3861 | if ((ph = calloc(1, sizeof(*ph))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3862 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3863 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3864 | goto out; |
| 3865 | } |
| 3866 | ph->id = strdup(args[cur_arg]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3867 | LIST_APPEND(&curgrp->phs, &ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3868 | cur_arg++; |
| 3869 | } |
| 3870 | } |
| 3871 | else if (*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3872 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n", |
| 3873 | file, linenum, args[0]); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3874 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3875 | goto out; |
| 3876 | } |
| 3877 | out: |
| 3878 | return err_code; |
| 3879 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3880 | |
| 3881 | static int |
| 3882 | cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm) |
| 3883 | { |
| 3884 | struct spoe_message *msg; |
| 3885 | struct spoe_arg *arg; |
| 3886 | const char *err; |
| 3887 | char *errmsg = NULL; |
| 3888 | int err_code = 0; |
| 3889 | |
| 3890 | if ((cfg_scope == NULL && curengine != NULL) || |
| 3891 | (cfg_scope != NULL && curengine == NULL) || |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3892 | (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0)) |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3893 | goto out; |
| 3894 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3895 | if (strcmp(args[0], "spoe-message") == 0) { /* new spoe-message section */ |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3896 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3897 | ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n", |
| 3898 | file, linenum); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3899 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3900 | goto out; |
| 3901 | } |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 3902 | if (alertif_too_many_args(1, file, linenum, args, &err_code)) { |
| 3903 | err_code |= ERR_ABORT; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3904 | goto out; |
| 3905 | } |
| 3906 | |
| 3907 | err = invalid_char(args[1]); |
| 3908 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3909 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n", |
| 3910 | file, linenum, *err, args[0], args[1]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3911 | err_code |= ERR_ALERT | ERR_ABORT; |
| 3912 | goto out; |
| 3913 | } |
| 3914 | |
| 3915 | list_for_each_entry(msg, &curmsgs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3916 | if (strcmp(msg->id, args[1]) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3917 | ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same" |
| 3918 | " name as another one declared at %s:%d.\n", |
| 3919 | file, linenum, args[1], msg->conf.file, msg->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3920 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3921 | goto out; |
| 3922 | } |
| 3923 | } |
| 3924 | |
| 3925 | if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3926 | ha_alert("parsing [%s:%d] : out of memory.\n", 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 | } |
| 3930 | |
| 3931 | curmsg->id = strdup(args[1]); |
| 3932 | curmsg->id_len = strlen(curmsg->id); |
| 3933 | curmsg->event = SPOE_EV_NONE; |
| 3934 | curmsg->conf.file = strdup(file); |
| 3935 | curmsg->conf.line = linenum; |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 3936 | curmsg->nargs = 0; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3937 | LIST_INIT(&curmsg->args); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3938 | LIST_INIT(&curmsg->acls); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 3939 | LIST_INIT(&curmsg->by_evt); |
| 3940 | LIST_INIT(&curmsg->by_grp); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3941 | LIST_APPEND(&curmsgs, &curmsg->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3942 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3943 | else if (strcmp(args[0], "args") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3944 | int cur_arg = 1; |
| 3945 | |
| 3946 | curproxy->conf.args.ctx = ARGC_SPOE; |
| 3947 | curproxy->conf.args.file = file; |
| 3948 | curproxy->conf.args.line = linenum; |
| 3949 | while (*args[cur_arg]) { |
| 3950 | char *delim = strchr(args[cur_arg], '='); |
| 3951 | int idx = 0; |
| 3952 | |
| 3953 | if ((arg = calloc(1, sizeof(*arg))) == 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 | if (!delim) { |
| 3960 | arg->name = NULL; |
| 3961 | arg->name_len = 0; |
| 3962 | delim = args[cur_arg]; |
| 3963 | } |
| 3964 | else { |
| 3965 | arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]); |
| 3966 | arg->name_len = delim - args[cur_arg]; |
| 3967 | delim++; |
| 3968 | } |
Christopher Faulet | b0b4238 | 2017-02-23 22:41:09 +0100 | [diff] [blame] | 3969 | arg->expr = sample_parse_expr((char*[]){delim, NULL}, |
| 3970 | &idx, file, linenum, &errmsg, |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 3971 | &curproxy->conf.args, NULL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3972 | if (arg->expr == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3973 | 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] | 3974 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3975 | free(arg->name); |
| 3976 | free(arg); |
| 3977 | goto out; |
| 3978 | } |
Christopher Faulet | f51f5fa | 2017-01-19 10:01:12 +0100 | [diff] [blame] | 3979 | curmsg->nargs++; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3980 | LIST_APPEND(&curmsg->args, &arg->list); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 3981 | cur_arg++; |
| 3982 | } |
| 3983 | curproxy->conf.args.file = NULL; |
| 3984 | curproxy->conf.args.line = 0; |
| 3985 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 3986 | else if (strcmp(args[0], "acl") == 0) { |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3987 | err = invalid_char(args[1]); |
| 3988 | if (err) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3989 | ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n", |
| 3990 | file, linenum, *err, args[1]); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 3991 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3992 | goto out; |
| 3993 | } |
Tim Duesterhus | 0cf811a | 2020-02-05 21:00:50 +0100 | [diff] [blame] | 3994 | if (strcasecmp(args[1], "or") == 0) { |
Tim Duesterhus | f1bc24c | 2020-02-06 22:04:03 +0100 | [diff] [blame] | 3995 | 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] | 3996 | "logical disjunction within a condition.\n", |
| 3997 | file, linenum, args[1]); |
| 3998 | err_code |= ERR_ALERT | ERR_FATAL; |
| 3999 | goto out; |
| 4000 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4001 | 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] | 4002 | ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n", |
| 4003 | file, linenum, args[1], errmsg); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4004 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4005 | goto out; |
| 4006 | } |
| 4007 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4008 | else if (strcmp(args[0], "event") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4009 | if (!*args[1]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4010 | ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4011 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4012 | goto out; |
| 4013 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4014 | /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */ |
| 4015 | /* goto out; */ |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4016 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4017 | 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] | 4018 | curmsg->event = SPOE_EV_ON_CLIENT_SESS; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4019 | 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] | 4020 | curmsg->event = SPOE_EV_ON_SERVER_SESS; |
| 4021 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4022 | 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] | 4023 | curmsg->event = SPOE_EV_ON_TCP_REQ_FE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4024 | 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] | 4025 | curmsg->event = SPOE_EV_ON_TCP_REQ_BE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4026 | 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] | 4027 | curmsg->event = SPOE_EV_ON_TCP_RSP; |
| 4028 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4029 | 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] | 4030 | curmsg->event = SPOE_EV_ON_HTTP_REQ_FE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4031 | 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] | 4032 | curmsg->event = SPOE_EV_ON_HTTP_REQ_BE; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4033 | 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] | 4034 | curmsg->event = SPOE_EV_ON_HTTP_RSP; |
| 4035 | else { |
Joseph Herlant | f1da69d | 2018-11-15 13:49:02 -0800 | [diff] [blame] | 4036 | ha_alert("parsing [%s:%d] : unknown event '%s'.\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4037 | file, linenum, args[1]); |
Christopher Faulet | ecc537a | 2017-02-23 22:52:39 +0100 | [diff] [blame] | 4038 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4039 | goto out; |
| 4040 | } |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4041 | |
| 4042 | if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) { |
| 4043 | struct acl_cond *cond; |
| 4044 | |
| 4045 | cond = build_acl_cond(file, linenum, &curmsg->acls, |
| 4046 | curproxy, (const char **)args+2, |
| 4047 | &errmsg); |
| 4048 | if (cond == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4049 | ha_alert("parsing [%s:%d] : error detected while " |
| 4050 | "parsing an 'event %s' condition : %s.\n", |
| 4051 | file, linenum, args[1], errmsg); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4052 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4053 | goto out; |
| 4054 | } |
| 4055 | curmsg->cond = cond; |
| 4056 | } |
| 4057 | else if (*args[2]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4058 | ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' " |
| 4059 | "or 'unless' followed by a condition but found '%s'.\n", |
| 4060 | file, linenum, args[1], args[2]); |
Christopher Faulet | 57583e4 | 2017-09-04 15:41:09 +0200 | [diff] [blame] | 4061 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4062 | goto out; |
| 4063 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4064 | } |
| 4065 | else if (!*args[0]) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4066 | ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n", |
| 4067 | file, linenum, args[0]); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4068 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4069 | goto out; |
| 4070 | } |
| 4071 | out: |
| 4072 | free(errmsg); |
| 4073 | return err_code; |
| 4074 | } |
| 4075 | |
| 4076 | /* Return -1 on error, else 0 */ |
| 4077 | static int |
| 4078 | parse_spoe_flt(char **args, int *cur_arg, struct proxy *px, |
| 4079 | struct flt_conf *fconf, char **err, void *private) |
| 4080 | { |
| 4081 | struct list backup_sections; |
| 4082 | struct spoe_config *conf; |
| 4083 | struct spoe_message *msg, *msgback; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4084 | struct spoe_group *grp, *grpback; |
| 4085 | struct spoe_placeholder *ph, *phback; |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4086 | struct spoe_var_placeholder *vph, *vphback; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4087 | struct logsrv *logsrv, *logsrvback; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4088 | char *file = NULL, *engine = NULL; |
| 4089 | int ret, pos = *cur_arg + 1; |
| 4090 | |
Christopher Faulet | 84c844e | 2018-03-23 14:37:14 +0100 | [diff] [blame] | 4091 | LIST_INIT(&curmsgs); |
| 4092 | LIST_INIT(&curgrps); |
| 4093 | LIST_INIT(&curmphs); |
| 4094 | LIST_INIT(&curgphs); |
| 4095 | LIST_INIT(&curvars); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4096 | LIST_INIT(&curlogsrvs); |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 4097 | curpxopts = 0; |
| 4098 | curpxopts2 = 0; |
Christopher Faulet | 84c844e | 2018-03-23 14:37:14 +0100 | [diff] [blame] | 4099 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4100 | conf = calloc(1, sizeof(*conf)); |
| 4101 | if (conf == NULL) { |
| 4102 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 4103 | goto error; |
| 4104 | } |
| 4105 | conf->proxy = px; |
| 4106 | |
| 4107 | while (*args[pos]) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4108 | if (strcmp(args[pos], "config") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4109 | if (!*args[pos+1]) { |
| 4110 | memprintf(err, "'%s' : '%s' option without value", |
| 4111 | args[*cur_arg], args[pos]); |
| 4112 | goto error; |
| 4113 | } |
| 4114 | file = args[pos+1]; |
| 4115 | pos += 2; |
| 4116 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4117 | else if (strcmp(args[pos], "engine") == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4118 | if (!*args[pos+1]) { |
| 4119 | memprintf(err, "'%s' : '%s' option without value", |
| 4120 | args[*cur_arg], args[pos]); |
| 4121 | goto error; |
| 4122 | } |
| 4123 | engine = args[pos+1]; |
| 4124 | pos += 2; |
| 4125 | } |
| 4126 | else { |
| 4127 | memprintf(err, "unknown keyword '%s'", args[pos]); |
| 4128 | goto error; |
| 4129 | } |
| 4130 | } |
| 4131 | if (file == NULL) { |
| 4132 | memprintf(err, "'%s' : missing config file", args[*cur_arg]); |
| 4133 | goto error; |
| 4134 | } |
| 4135 | |
| 4136 | /* backup sections and register SPOE sections */ |
| 4137 | LIST_INIT(&backup_sections); |
| 4138 | cfg_backup_sections(&backup_sections); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4139 | cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL); |
| 4140 | cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL); |
William Lallemand | d2ff56d | 2017-10-16 11:06:50 +0200 | [diff] [blame] | 4141 | cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4142 | |
| 4143 | /* Parse SPOE filter configuration file */ |
| 4144 | curengine = engine; |
| 4145 | curproxy = px; |
| 4146 | curagent = NULL; |
| 4147 | curmsg = NULL; |
| 4148 | ret = readcfgfile(file); |
| 4149 | curproxy = NULL; |
| 4150 | |
| 4151 | /* unregister SPOE sections and restore previous sections */ |
| 4152 | cfg_unregister_sections(); |
| 4153 | cfg_restore_sections(&backup_sections); |
| 4154 | |
| 4155 | if (ret == -1) { |
| 4156 | memprintf(err, "Could not open configuration file %s : %s", |
| 4157 | file, strerror(errno)); |
| 4158 | goto error; |
| 4159 | } |
| 4160 | if (ret & (ERR_ABORT|ERR_FATAL)) { |
| 4161 | memprintf(err, "Error(s) found in configuration file %s", file); |
| 4162 | goto error; |
| 4163 | } |
| 4164 | |
| 4165 | /* Check SPOE agent */ |
| 4166 | if (curagent == NULL) { |
| 4167 | memprintf(err, "No SPOE agent found in file %s", file); |
| 4168 | goto error; |
| 4169 | } |
| 4170 | if (curagent->b.name == NULL) { |
| 4171 | memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d", |
| 4172 | curagent->id, curagent->conf.file, curagent->conf.line); |
| 4173 | goto error; |
| 4174 | } |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 4175 | if (curagent->timeout.hello == TICK_ETERNITY || |
| 4176 | curagent->timeout.idle == TICK_ETERNITY || |
Christopher Faulet | f7a3092 | 2016-11-10 15:04:51 +0100 | [diff] [blame] | 4177 | curagent->timeout.processing == TICK_ETERNITY) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4178 | ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n" |
| 4179 | " | While not properly invalid, you will certainly encounter various problems\n" |
| 4180 | " | with such a configuration. To fix this, please ensure that all following\n" |
| 4181 | " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n", |
| 4182 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4183 | } |
| 4184 | if (curagent->var_pfx == NULL) { |
| 4185 | char *tmp = curagent->id; |
| 4186 | |
| 4187 | while (*tmp) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4188 | if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4189 | memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. " |
| 4190 | "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n", |
| 4191 | curagent->id, curagent->id, curagent->conf.file, curagent->conf.line); |
| 4192 | goto error; |
| 4193 | } |
| 4194 | tmp++; |
| 4195 | } |
| 4196 | curagent->var_pfx = strdup(curagent->id); |
| 4197 | } |
| 4198 | |
Christopher Faulet | b7426d1 | 2018-03-21 14:12:17 +0100 | [diff] [blame] | 4199 | if (curagent->var_on_error) { |
| 4200 | struct arg arg; |
| 4201 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4202 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | b7426d1 | 2018-03-21 14:12:17 +0100 | [diff] [blame] | 4203 | curagent->var_pfx, curagent->var_on_error); |
| 4204 | |
| 4205 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4206 | arg.data.str.area = trash.area; |
| 4207 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4208 | 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] | 4209 | if (!vars_check_arg(&arg, err)) { |
| 4210 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4211 | curagent->id, curagent->var_pfx, curagent->var_on_error, *err); |
| 4212 | goto error; |
| 4213 | } |
| 4214 | } |
| 4215 | |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4216 | if (curagent->var_t_process) { |
| 4217 | struct arg arg; |
| 4218 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4219 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4220 | curagent->var_pfx, curagent->var_t_process); |
| 4221 | |
| 4222 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4223 | arg.data.str.area = trash.area; |
| 4224 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4225 | 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] | 4226 | if (!vars_check_arg(&arg, err)) { |
| 4227 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4228 | curagent->id, curagent->var_pfx, curagent->var_t_process, *err); |
| 4229 | goto error; |
| 4230 | } |
| 4231 | } |
| 4232 | |
| 4233 | if (curagent->var_t_total) { |
| 4234 | struct arg arg; |
| 4235 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4236 | trash.data = snprintf(trash.area, trash.size, "txn.%s.%s", |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4237 | curagent->var_pfx, curagent->var_t_total); |
| 4238 | |
| 4239 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4240 | arg.data.str.area = trash.area; |
| 4241 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4242 | arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */ |
Christopher Faulet | 36bda1c | 2018-03-22 09:08:20 +0100 | [diff] [blame] | 4243 | if (!vars_check_arg(&arg, err)) { |
| 4244 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4245 | curagent->id, curagent->var_pfx, curagent->var_t_process, *err); |
| 4246 | goto error; |
| 4247 | } |
| 4248 | } |
| 4249 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4250 | if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4251 | ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n", |
| 4252 | px->id, curagent->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4253 | goto finish; |
| 4254 | } |
| 4255 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4256 | /* Replace placeholders by the corresponding messages for the SPOE |
| 4257 | * agent */ |
| 4258 | list_for_each_entry(ph, &curmphs, list) { |
| 4259 | list_for_each_entry(msg, &curmsgs, list) { |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4260 | struct spoe_arg *arg; |
| 4261 | unsigned int where; |
| 4262 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4263 | if (strcmp(msg->id, ph->id) == 0) { |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4264 | if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) { |
| 4265 | if (msg->event == SPOE_EV_ON_TCP_REQ_BE) |
| 4266 | msg->event = SPOE_EV_ON_TCP_REQ_FE; |
| 4267 | if (msg->event == SPOE_EV_ON_HTTP_REQ_BE) |
| 4268 | msg->event = SPOE_EV_ON_HTTP_REQ_FE; |
| 4269 | } |
| 4270 | if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS || |
| 4271 | msg->event == SPOE_EV_ON_TCP_REQ_FE || |
| 4272 | msg->event == SPOE_EV_ON_HTTP_REQ_FE)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4273 | ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n", |
| 4274 | px->id, msg->conf.file, msg->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4275 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4276 | } |
| 4277 | if (msg->event == SPOE_EV_NONE) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4278 | ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n", |
| 4279 | px->id, msg->id, msg->conf.file, msg->conf.line); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4280 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4281 | } |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4282 | |
| 4283 | where = 0; |
| 4284 | switch (msg->event) { |
| 4285 | case SPOE_EV_ON_CLIENT_SESS: |
| 4286 | where |= SMP_VAL_FE_CON_ACC; |
| 4287 | break; |
| 4288 | |
| 4289 | case SPOE_EV_ON_TCP_REQ_FE: |
| 4290 | where |= SMP_VAL_FE_REQ_CNT; |
| 4291 | break; |
| 4292 | |
| 4293 | case SPOE_EV_ON_HTTP_REQ_FE: |
| 4294 | where |= SMP_VAL_FE_HRQ_HDR; |
| 4295 | break; |
| 4296 | |
| 4297 | case SPOE_EV_ON_TCP_REQ_BE: |
| 4298 | if (px->cap & PR_CAP_FE) |
| 4299 | where |= SMP_VAL_FE_REQ_CNT; |
| 4300 | if (px->cap & PR_CAP_BE) |
| 4301 | where |= SMP_VAL_BE_REQ_CNT; |
| 4302 | break; |
| 4303 | |
| 4304 | case SPOE_EV_ON_HTTP_REQ_BE: |
| 4305 | if (px->cap & PR_CAP_FE) |
| 4306 | where |= SMP_VAL_FE_HRQ_HDR; |
| 4307 | if (px->cap & PR_CAP_BE) |
| 4308 | where |= SMP_VAL_BE_HRQ_HDR; |
| 4309 | break; |
| 4310 | |
| 4311 | case SPOE_EV_ON_SERVER_SESS: |
| 4312 | where |= SMP_VAL_BE_SRV_CON; |
| 4313 | break; |
| 4314 | |
| 4315 | case SPOE_EV_ON_TCP_RSP: |
| 4316 | if (px->cap & PR_CAP_FE) |
| 4317 | where |= SMP_VAL_FE_RES_CNT; |
| 4318 | if (px->cap & PR_CAP_BE) |
| 4319 | where |= SMP_VAL_BE_RES_CNT; |
| 4320 | break; |
| 4321 | |
| 4322 | case SPOE_EV_ON_HTTP_RSP: |
| 4323 | if (px->cap & PR_CAP_FE) |
| 4324 | where |= SMP_VAL_FE_HRS_HDR; |
| 4325 | if (px->cap & PR_CAP_BE) |
| 4326 | where |= SMP_VAL_BE_HRS_HDR; |
| 4327 | break; |
| 4328 | |
| 4329 | default: |
| 4330 | break; |
| 4331 | } |
| 4332 | |
| 4333 | list_for_each_entry(arg, &msg->args, list) { |
| 4334 | if (!(arg->expr->fetch->val & where)) { |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4335 | memprintf(err, "Ignore SPOE message '%s' at %s:%d: " |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4336 | "some args extract information from '%s', " |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4337 | "none of which is available here ('%s')", |
| 4338 | msg->id, msg->conf.file, msg->conf.line, |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4339 | sample_ckp_names(arg->expr->fetch->use), |
| 4340 | sample_ckp_names(where)); |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4341 | goto error; |
Christopher Faulet | a21b064 | 2017-01-09 16:56:23 +0100 | [diff] [blame] | 4342 | } |
| 4343 | } |
| 4344 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4345 | msg->agent = curagent; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4346 | LIST_APPEND(&curagent->events[msg->event], &msg->by_evt); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4347 | goto next_mph; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4348 | } |
| 4349 | } |
| 4350 | 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] | 4351 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4352 | goto error; |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4353 | next_mph: |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4354 | continue; |
| 4355 | } |
| 4356 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4357 | /* Replace placeholders by the corresponding groups for the SPOE |
| 4358 | * agent */ |
| 4359 | list_for_each_entry(ph, &curgphs, list) { |
| 4360 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4361 | if (strcmp(grp->id, ph->id) == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4362 | grp->agent = curagent; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4363 | LIST_DELETE(&grp->list); |
| 4364 | LIST_APPEND(&curagent->groups, &grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4365 | goto next_aph; |
| 4366 | } |
| 4367 | } |
| 4368 | memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d", |
| 4369 | curagent->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 4370 | goto error; |
| 4371 | next_aph: |
| 4372 | continue; |
| 4373 | } |
| 4374 | |
| 4375 | /* Replace placeholders by the corresponding message for each SPOE |
| 4376 | * group of the SPOE agent */ |
| 4377 | list_for_each_entry(grp, &curagent->groups, list) { |
| 4378 | list_for_each_entry_safe(ph, phback, &grp->phs, list) { |
| 4379 | list_for_each_entry(msg, &curmsgs, list) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4380 | if (strcmp(msg->id, ph->id) == 0) { |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4381 | if (msg->group != NULL) { |
| 4382 | memprintf(err, "SPOE message '%s' already belongs to " |
| 4383 | "the SPOE group '%s' declare at %s:%d", |
| 4384 | msg->id, msg->group->id, |
| 4385 | msg->group->conf.file, |
| 4386 | msg->group->conf.line); |
| 4387 | goto error; |
| 4388 | } |
| 4389 | |
| 4390 | /* Scope for arguments are not checked for now. We will check |
| 4391 | * them only if a rule use the corresponding SPOE group. */ |
| 4392 | msg->agent = curagent; |
| 4393 | msg->group = grp; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4394 | LIST_DELETE(&ph->list); |
| 4395 | LIST_APPEND(&grp->messages, &msg->by_grp); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4396 | goto next_mph_grp; |
| 4397 | } |
| 4398 | } |
| 4399 | memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d", |
| 4400 | grp->id, ph->id, curagent->conf.file, curagent->conf.line); |
| 4401 | goto error; |
| 4402 | next_mph_grp: |
| 4403 | continue; |
| 4404 | } |
| 4405 | } |
| 4406 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4407 | finish: |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4408 | /* move curmsgs to the agent message list */ |
| 4409 | curmsgs.n->p = &curagent->messages; |
| 4410 | curmsgs.p->n = &curagent->messages; |
| 4411 | curagent->messages = curmsgs; |
| 4412 | LIST_INIT(&curmsgs); |
| 4413 | |
Christopher Faulet | 7ee8667 | 2017-09-19 11:08:28 +0200 | [diff] [blame] | 4414 | conf->id = strdup(engine ? engine : curagent->id); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4415 | conf->agent = curagent; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4416 | |
| 4417 | /* Start agent's proxy initialization here. It will be finished during |
| 4418 | * the filter init. */ |
| 4419 | memset(&conf->agent_fe, 0, sizeof(conf->agent_fe)); |
| 4420 | init_new_proxy(&conf->agent_fe); |
| 4421 | conf->agent_fe.id = conf->agent->id; |
| 4422 | conf->agent_fe.parent = conf->agent; |
Christopher Faulet | 0e0f085 | 2018-03-26 17:20:36 +0200 | [diff] [blame] | 4423 | conf->agent_fe.options |= curpxopts; |
| 4424 | conf->agent_fe.options2 |= curpxopts2; |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4425 | |
| 4426 | list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4427 | LIST_DELETE(&logsrv->list); |
| 4428 | LIST_APPEND(&conf->agent_fe.logsrvs, &logsrv->list); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4429 | } |
| 4430 | |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4431 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4432 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4433 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4434 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4435 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4436 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4437 | spoe_release_placeholder(ph); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4438 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4439 | list_for_each_entry_safe(vph, vphback, &curvars, list) { |
| 4440 | struct arg arg; |
| 4441 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4442 | trash.data = snprintf(trash.area, trash.size, "proc.%s.%s", |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4443 | curagent->var_pfx, vph->name); |
| 4444 | |
| 4445 | arg.type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4446 | arg.data.str.area = trash.area; |
| 4447 | arg.data.str.data = trash.data; |
William Dauchy | afb9368 | 2021-01-05 11:14:58 +0100 | [diff] [blame] | 4448 | 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] | 4449 | if (!vars_check_arg(&arg, err)) { |
| 4450 | memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)", |
| 4451 | curagent->id, curagent->var_pfx, vph->name, *err); |
| 4452 | goto error; |
| 4453 | } |
| 4454 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4455 | LIST_DELETE(&vph->list); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4456 | free(vph->name); |
| 4457 | free(vph); |
| 4458 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4459 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4460 | LIST_DELETE(&grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4461 | spoe_release_group(grp); |
| 4462 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4463 | *cur_arg = pos; |
Christopher Faulet | 3b386a3 | 2017-02-23 10:17:15 +0100 | [diff] [blame] | 4464 | fconf->id = spoe_filter_id; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4465 | fconf->ops = &spoe_ops; |
| 4466 | fconf->conf = conf; |
| 4467 | return 0; |
| 4468 | |
| 4469 | error: |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4470 | spoe_release_agent(curagent); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4471 | list_for_each_entry_safe(ph, phback, &curmphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4472 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4473 | spoe_release_placeholder(ph); |
| 4474 | } |
| 4475 | list_for_each_entry_safe(ph, phback, &curgphs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4476 | LIST_DELETE(&ph->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4477 | spoe_release_placeholder(ph); |
| 4478 | } |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4479 | list_for_each_entry_safe(vph, vphback, &curvars, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4480 | LIST_DELETE(&vph->list); |
Christopher Faulet | 336d3ef | 2017-12-22 10:00:55 +0100 | [diff] [blame] | 4481 | free(vph->name); |
| 4482 | free(vph); |
| 4483 | } |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4484 | list_for_each_entry_safe(grp, grpback, &curgrps, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4485 | LIST_DELETE(&grp->list); |
Christopher Faulet | 11610f3 | 2017-09-21 10:23:10 +0200 | [diff] [blame] | 4486 | spoe_release_group(grp); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4487 | } |
| 4488 | list_for_each_entry_safe(msg, msgback, &curmsgs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4489 | LIST_DELETE(&msg->list); |
Christopher Faulet | 8ef7525 | 2017-02-20 22:56:03 +0100 | [diff] [blame] | 4490 | spoe_release_message(msg); |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4491 | } |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4492 | list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4493 | LIST_DELETE(&logsrv->list); |
Christopher Faulet | 7250b8f | 2018-03-26 17:19:01 +0200 | [diff] [blame] | 4494 | free(logsrv); |
| 4495 | } |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4496 | free(conf); |
| 4497 | return -1; |
| 4498 | } |
| 4499 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4500 | /* Send message of a SPOE group. This is the action_ptr callback of a rule |
| 4501 | * associated to a "send-spoe-group" action. |
| 4502 | * |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4503 | * It returns ACT_RET_CONT if processing is finished (with error or not), it returns |
| 4504 | * ACT_RET_YIELD if the action is in progress. */ |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4505 | static enum act_return |
| 4506 | spoe_send_group(struct act_rule *rule, struct proxy *px, |
| 4507 | struct session *sess, struct stream *s, int flags) |
| 4508 | { |
| 4509 | struct filter *filter; |
| 4510 | struct spoe_agent *agent = NULL; |
| 4511 | struct spoe_group *group = NULL; |
| 4512 | struct spoe_context *ctx = NULL; |
| 4513 | int ret, dir; |
| 4514 | |
| 4515 | list_for_each_entry(filter, &s->strm_flt.filters, list) { |
| 4516 | if (filter->config == rule->arg.act.p[0]) { |
| 4517 | agent = rule->arg.act.p[2]; |
| 4518 | group = rule->arg.act.p[3]; |
| 4519 | ctx = filter->ctx; |
| 4520 | break; |
| 4521 | } |
| 4522 | } |
| 4523 | if (agent == NULL || group == NULL || ctx == NULL) |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4524 | return ACT_RET_CONT; |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4525 | if (ctx->state == SPOE_CTX_ST_NONE) |
| 4526 | return ACT_RET_CONT; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4527 | |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4528 | switch (rule->from) { |
| 4529 | case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break; |
| 4530 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 4531 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 4532 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 4533 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
| 4534 | default: |
| 4535 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 4536 | " - internal error while execute spoe-send-group\n", |
| 4537 | (int)now.tv_sec, (int)now.tv_usec, agent->id, |
| 4538 | __FUNCTION__, s); |
| 4539 | send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n", |
| 4540 | agent->id); |
| 4541 | return ACT_RET_CONT; |
| 4542 | } |
| 4543 | |
| 4544 | ret = spoe_process_group(s, ctx, group, dir); |
| 4545 | if (ret == 1) |
| 4546 | return ACT_RET_CONT; |
| 4547 | else if (ret == 0) { |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 4548 | if (flags & ACT_OPT_FINAL) { |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4549 | SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p" |
| 4550 | " - failed to process group '%s': interrupted by caller\n", |
| 4551 | (int)now.tv_sec, (int)now.tv_usec, |
| 4552 | agent->id, __FUNCTION__, s, group->id); |
| 4553 | ctx->status_code = SPOE_CTX_ERR_INTERRUPT; |
Christopher Faulet | 6f9ea4f | 2018-01-24 16:13:48 +0100 | [diff] [blame] | 4554 | spoe_stop_processing(agent, ctx); |
Christopher Faulet | caf2fec | 2018-04-04 10:25:50 +0200 | [diff] [blame] | 4555 | spoe_handle_processing_error(s, agent, ctx, dir); |
Christopher Faulet | 344c4ab | 2017-09-22 10:20:13 +0200 | [diff] [blame] | 4556 | return ACT_RET_CONT; |
| 4557 | } |
| 4558 | return ACT_RET_YIELD; |
| 4559 | } |
| 4560 | else |
Christopher Faulet | 1340376 | 2019-12-13 09:01:57 +0100 | [diff] [blame] | 4561 | return ACT_RET_CONT; |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | /* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE |
| 4565 | * group associated to <rule>. The format of an rule using 'send-spoe-group' |
| 4566 | * action should be: |
| 4567 | * |
| 4568 | * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id> |
| 4569 | * |
| 4570 | * So, we'll loop on each configured SPOE filter for the proxy <px> to find the |
| 4571 | * SPOE engine matching <engine-id>. And then, we'll try to find the good group |
| 4572 | * matching <group-id>. Finally, we'll check all messages referenced by the SPOE |
| 4573 | * group. |
| 4574 | * |
| 4575 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 4576 | * filled. |
| 4577 | */ |
| 4578 | static int |
| 4579 | check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err) |
| 4580 | { |
| 4581 | struct flt_conf *fconf; |
| 4582 | struct spoe_config *conf; |
| 4583 | struct spoe_agent *agent = NULL; |
| 4584 | struct spoe_group *group; |
| 4585 | struct spoe_message *msg; |
| 4586 | char *engine_id = rule->arg.act.p[0]; |
| 4587 | char *group_id = rule->arg.act.p[1]; |
| 4588 | unsigned int where = 0; |
| 4589 | |
| 4590 | switch (rule->from) { |
| 4591 | case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break; |
| 4592 | case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break; |
| 4593 | case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break; |
| 4594 | case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break; |
| 4595 | case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break; |
| 4596 | default: |
| 4597 | memprintf(err, |
| 4598 | "internal error, unexpected rule->from=%d, please report this bug!", |
| 4599 | rule->from); |
| 4600 | goto error; |
| 4601 | } |
| 4602 | |
| 4603 | /* Try to find the SPOE engine by checking all SPOE filters for proxy |
| 4604 | * <px> */ |
| 4605 | list_for_each_entry(fconf, &px->filter_configs, list) { |
| 4606 | conf = fconf->conf; |
| 4607 | |
| 4608 | /* This is not an SPOE filter */ |
| 4609 | if (fconf->id != spoe_filter_id) |
| 4610 | continue; |
| 4611 | |
| 4612 | /* This is the good engine */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4613 | if (strcmp(conf->id, engine_id) == 0) { |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4614 | agent = conf->agent; |
| 4615 | break; |
| 4616 | } |
| 4617 | } |
| 4618 | if (agent == NULL) { |
| 4619 | memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'", |
| 4620 | engine_id, group_id); |
| 4621 | goto error; |
| 4622 | } |
| 4623 | |
| 4624 | /* Try to find the right group */ |
| 4625 | list_for_each_entry(group, &agent->groups, list) { |
| 4626 | /* This is the good group */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4627 | if (strcmp(group->id, group_id) == 0) |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4628 | break; |
| 4629 | } |
| 4630 | if (&group->list == &agent->groups) { |
| 4631 | memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration", |
| 4632 | group_id, engine_id); |
| 4633 | goto error; |
| 4634 | } |
| 4635 | |
| 4636 | /* Ok, we found the group, we need to check messages and their |
| 4637 | * arguments */ |
| 4638 | list_for_each_entry(msg, &group->messages, by_grp) { |
| 4639 | struct spoe_arg *arg; |
| 4640 | |
| 4641 | list_for_each_entry(arg, &msg->args, list) { |
| 4642 | if (!(arg->expr->fetch->val & where)) { |
| 4643 | memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: " |
| 4644 | "some args extract information from '%s'," |
| 4645 | "none of which is available here ('%s')", |
| 4646 | msg->id, group->id, msg->conf.file, msg->conf.line, |
| 4647 | sample_ckp_names(arg->expr->fetch->use), |
| 4648 | sample_ckp_names(where)); |
| 4649 | goto error; |
| 4650 | } |
| 4651 | } |
| 4652 | } |
| 4653 | |
| 4654 | free(engine_id); |
| 4655 | free(group_id); |
| 4656 | rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */ |
| 4657 | rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */ |
| 4658 | rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */ |
| 4659 | rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */ |
| 4660 | return 1; |
| 4661 | |
| 4662 | error: |
| 4663 | free(engine_id); |
| 4664 | free(group_id); |
| 4665 | return 0; |
| 4666 | } |
| 4667 | |
| 4668 | /* Parse 'send-spoe-group' action following the format: |
| 4669 | * |
| 4670 | * ... send-spoe-group <engine-id> <group-id> |
| 4671 | * |
| 4672 | * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error |
| 4673 | * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group |
| 4674 | * ids are saved and used later, when the rule will be checked. |
| 4675 | */ |
| 4676 | static enum act_parse_ret |
| 4677 | parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px, |
| 4678 | struct act_rule *rule, char **err) |
| 4679 | { |
| 4680 | if (!*args[*orig_arg] || !*args[*orig_arg+1] || |
| 4681 | (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) { |
| 4682 | memprintf(err, "expects 2 arguments: <engine-id> <group-id>"); |
| 4683 | return ACT_RET_PRS_ERR; |
| 4684 | } |
| 4685 | rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */ |
| 4686 | rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */ |
| 4687 | |
| 4688 | (*orig_arg) += 2; |
| 4689 | |
| 4690 | rule->action = ACT_CUSTOM; |
| 4691 | rule->action_ptr = spoe_send_group; |
| 4692 | rule->check_ptr = check_send_spoe_group; |
| 4693 | return ACT_RET_PRS_OK; |
| 4694 | } |
| 4695 | |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 4696 | |
| 4697 | /* Declare the filter parser for "spoe" keyword */ |
| 4698 | static struct flt_kw_list flt_kws = { "SPOE", { }, { |
| 4699 | { "spoe", parse_spoe_flt, NULL }, |
| 4700 | { NULL, NULL, NULL }, |
| 4701 | } |
| 4702 | }; |
| 4703 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4704 | INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws); |
| 4705 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4706 | /* Delcate the action parser for "spoe-action" keyword */ |
| 4707 | static struct action_kw_list tcp_req_action_kws = { { }, { |
| 4708 | { "send-spoe-group", parse_send_spoe_group }, |
| 4709 | { /* END */ }, |
| 4710 | } |
| 4711 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4712 | |
| 4713 | INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws); |
| 4714 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4715 | static struct action_kw_list tcp_res_action_kws = { { }, { |
| 4716 | { "send-spoe-group", parse_send_spoe_group }, |
| 4717 | { /* END */ }, |
| 4718 | } |
| 4719 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4720 | |
| 4721 | INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws); |
| 4722 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4723 | static struct action_kw_list http_req_action_kws = { { }, { |
| 4724 | { "send-spoe-group", parse_send_spoe_group }, |
| 4725 | { /* END */ }, |
| 4726 | } |
| 4727 | }; |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4728 | |
| 4729 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws); |
| 4730 | |
Christopher Faulet | 76c09ef | 2017-09-21 11:03:52 +0200 | [diff] [blame] | 4731 | static struct action_kw_list http_res_action_kws = { { }, { |
| 4732 | { "send-spoe-group", parse_send_spoe_group }, |
| 4733 | { /* END */ }, |
| 4734 | } |
| 4735 | }; |
| 4736 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4737 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws); |