Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Event sink management |
| 3 | * |
| 4 | * Copyright (C) 2000-2019 Willy Tarreau - w@1wt.eu |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation, version 2.1 |
| 9 | * exclusively. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 21 | #include <import/ist.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 22 | #include <haproxy/api.h> |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 23 | #include <haproxy/applet.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 24 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 25 | #include <haproxy/cli.h> |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 26 | #include <haproxy/conn_stream.h> |
| 27 | #include <haproxy/cs_utils.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 28 | #include <haproxy/errors.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 29 | #include <haproxy/list.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 30 | #include <haproxy/log.h> |
Willy Tarreau | 817538e | 2021-05-08 20:20:21 +0200 | [diff] [blame] | 31 | #include <haproxy/proxy.h> |
Willy Tarreau | d2ad57c | 2020-06-03 19:43:35 +0200 | [diff] [blame] | 32 | #include <haproxy/ring.h> |
Willy Tarreau | 3727a8a | 2020-06-04 17:37:26 +0200 | [diff] [blame] | 33 | #include <haproxy/signal.h> |
Willy Tarreau | ba2f73d | 2020-06-03 20:02:28 +0200 | [diff] [blame] | 34 | #include <haproxy/sink.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 35 | #include <haproxy/time.h> |
Willy Tarreau | 4bad5e2 | 2021-05-08 13:05:30 +0200 | [diff] [blame] | 36 | #include <haproxy/tools.h> |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 37 | |
| 38 | struct list sink_list = LIST_HEAD_INIT(sink_list); |
| 39 | |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 40 | struct sink *cfg_sink; |
| 41 | |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 42 | struct sink *sink_find(const char *name) |
| 43 | { |
| 44 | struct sink *sink; |
| 45 | |
| 46 | list_for_each_entry(sink, &sink_list, sink_list) |
| 47 | if (strcmp(sink->name, name) == 0) |
| 48 | return sink; |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | /* creates a new sink and adds it to the list, it's still generic and not fully |
| 53 | * initialized. Returns NULL on allocation failure. If another one already |
| 54 | * exists with the same name, it will be returned. The caller can detect it as |
| 55 | * a newly created one has type SINK_TYPE_NEW. |
| 56 | */ |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 57 | static struct sink *__sink_new(const char *name, const char *desc, int fmt) |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 58 | { |
| 59 | struct sink *sink; |
| 60 | |
| 61 | sink = sink_find(name); |
| 62 | if (sink) |
| 63 | goto end; |
| 64 | |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 65 | sink = calloc(1, sizeof(*sink)); |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 66 | if (!sink) |
| 67 | goto end; |
| 68 | |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 69 | sink->name = strdup(name); |
Tim Duesterhus | a7ebffe | 2021-01-03 19:54:11 +0100 | [diff] [blame] | 70 | if (!sink->name) |
| 71 | goto err; |
| 72 | |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 73 | sink->desc = strdup(desc); |
Tim Duesterhus | a7ebffe | 2021-01-03 19:54:11 +0100 | [diff] [blame] | 74 | if (!sink->desc) |
| 75 | goto err; |
| 76 | |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 77 | sink->fmt = fmt; |
| 78 | sink->type = SINK_TYPE_NEW; |
Christopher Faulet | a63a5c2 | 2019-11-15 15:10:12 +0100 | [diff] [blame] | 79 | sink->maxlen = BUFSIZE; |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 80 | /* address will be filled by the caller if needed */ |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 81 | sink->ctx.fd = -1; |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 82 | sink->ctx.dropped = 0; |
| 83 | HA_RWLOCK_INIT(&sink->ctx.lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 84 | LIST_APPEND(&sink_list, &sink->sink_list); |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 85 | end: |
| 86 | return sink; |
Tim Duesterhus | a7ebffe | 2021-01-03 19:54:11 +0100 | [diff] [blame] | 87 | |
| 88 | err: |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 89 | ha_free(&sink->name); |
| 90 | ha_free(&sink->desc); |
| 91 | ha_free(&sink); |
Tim Duesterhus | a7ebffe | 2021-01-03 19:54:11 +0100 | [diff] [blame] | 92 | |
| 93 | return NULL; |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 96 | /* creates a sink called <name> of type FD associated to fd <fd>, format <fmt>, |
| 97 | * and description <desc>. Returns NULL on allocation failure or conflict. |
| 98 | * Perfect duplicates are merged (same type, fd, and name). |
| 99 | */ |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 100 | struct sink *sink_new_fd(const char *name, const char *desc, enum log_fmt fmt, int fd) |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 101 | { |
| 102 | struct sink *sink; |
| 103 | |
| 104 | sink = __sink_new(name, desc, fmt); |
| 105 | if (!sink || (sink->type == SINK_TYPE_FD && sink->ctx.fd == fd)) |
| 106 | goto end; |
| 107 | |
| 108 | if (sink->type != SINK_TYPE_NEW) { |
| 109 | sink = NULL; |
| 110 | goto end; |
| 111 | } |
| 112 | |
| 113 | sink->type = SINK_TYPE_FD; |
| 114 | sink->ctx.fd = fd; |
| 115 | end: |
| 116 | return sink; |
| 117 | } |
| 118 | |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 119 | /* creates a sink called <name> of type BUF of size <size>, format <fmt>, |
| 120 | * and description <desc>. Returns NULL on allocation failure or conflict. |
| 121 | * Perfect duplicates are merged (same type and name). If sizes differ, the |
| 122 | * largest one is kept. |
| 123 | */ |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 124 | struct sink *sink_new_buf(const char *name, const char *desc, enum log_fmt fmt, size_t size) |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 125 | { |
| 126 | struct sink *sink; |
| 127 | |
| 128 | sink = __sink_new(name, desc, fmt); |
| 129 | if (!sink) |
| 130 | goto fail; |
| 131 | |
| 132 | if (sink->type == SINK_TYPE_BUFFER) { |
| 133 | /* such a buffer already exists, we may have to resize it */ |
| 134 | if (!ring_resize(sink->ctx.ring, size)) |
| 135 | goto fail; |
| 136 | goto end; |
| 137 | } |
| 138 | |
| 139 | if (sink->type != SINK_TYPE_NEW) { |
| 140 | /* already exists of another type */ |
| 141 | goto fail; |
| 142 | } |
| 143 | |
| 144 | sink->ctx.ring = ring_new(size); |
| 145 | if (!sink->ctx.ring) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 146 | LIST_DELETE(&sink->sink_list); |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 147 | free(sink->name); |
| 148 | free(sink->desc); |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 149 | free(sink); |
| 150 | goto fail; |
| 151 | } |
| 152 | |
| 153 | sink->type = SINK_TYPE_BUFFER; |
| 154 | end: |
| 155 | return sink; |
| 156 | fail: |
| 157 | return NULL; |
| 158 | } |
| 159 | |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 160 | /* tries to send <nmsg> message parts (up to 8, ignored above) from message |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 161 | * array <msg> to sink <sink>. Formatting according to the sink's preference is |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 162 | * done here. Lost messages are NOT accounted for. It is preferable to call |
| 163 | * sink_write() instead which will also try to emit the number of dropped |
| 164 | * messages when there are any. It returns >0 if it could write anything, |
| 165 | * <=0 otherwise. |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 166 | */ |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 167 | ssize_t __sink_write(struct sink *sink, const struct ist msg[], size_t nmsg, |
| 168 | int level, int facility, struct ist *metadata) |
| 169 | { |
| 170 | struct ist *pfx = NULL; |
Willy Tarreau | a1426de | 2019-08-27 14:21:02 +0200 | [diff] [blame] | 171 | size_t npfx = 0; |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 172 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 173 | if (sink->fmt == LOG_FORMAT_RAW) |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 174 | goto send; |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 175 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 176 | pfx = build_log_header(sink->fmt, level, facility, metadata, &npfx); |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 177 | |
| 178 | send: |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 179 | if (sink->type == SINK_TYPE_FD) { |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 180 | return fd_write_frag_line(sink->ctx.fd, sink->maxlen, pfx, npfx, msg, nmsg, 1); |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 181 | } |
| 182 | else if (sink->type == SINK_TYPE_BUFFER) { |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 183 | return ring_write(sink->ctx.ring, sink->maxlen, pfx, npfx, msg, nmsg); |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 184 | } |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 185 | return 0; |
| 186 | } |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 187 | |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 188 | /* Tries to emit a message indicating the number of dropped events. In case of |
| 189 | * success, the amount of drops is reduced by as much. It's supposed to be |
| 190 | * called under an exclusive lock on the sink to avoid multiple produces doing |
| 191 | * the same. On success, >0 is returned, otherwise <=0 on failure. |
| 192 | */ |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 193 | int sink_announce_dropped(struct sink *sink, int facility) |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 194 | { |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 195 | static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS]; |
| 196 | static THREAD_LOCAL pid_t curr_pid; |
| 197 | static THREAD_LOCAL char pidstr[16]; |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 198 | unsigned int dropped; |
| 199 | struct buffer msg; |
| 200 | struct ist msgvec[1]; |
| 201 | char logbuf[64]; |
| 202 | |
| 203 | while (unlikely((dropped = sink->ctx.dropped) > 0)) { |
| 204 | chunk_init(&msg, logbuf, sizeof(logbuf)); |
| 205 | chunk_printf(&msg, "%u event%s dropped", dropped, dropped > 1 ? "s" : ""); |
| 206 | msgvec[0] = ist2(msg.area, msg.data); |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 207 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 208 | if (!metadata[LOG_META_HOST].len) { |
| 209 | if (global.log_send_hostname) |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 210 | metadata[LOG_META_HOST] = ist(global.log_send_hostname); |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | if (!metadata[LOG_META_TAG].len) |
| 214 | metadata[LOG_META_TAG] = ist2(global.log_tag.area, global.log_tag.data); |
| 215 | |
| 216 | if (unlikely(curr_pid != getpid())) |
| 217 | metadata[LOG_META_PID].len = 0; |
| 218 | |
| 219 | if (!metadata[LOG_META_PID].len) { |
| 220 | curr_pid = getpid(); |
| 221 | ltoa_o(curr_pid, pidstr, sizeof(pidstr)); |
| 222 | metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr)); |
| 223 | } |
| 224 | |
| 225 | if (__sink_write(sink, msgvec, 1, LOG_NOTICE, facility, metadata) <= 0) |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 226 | return 0; |
| 227 | /* success! */ |
| 228 | HA_ATOMIC_SUB(&sink->ctx.dropped, dropped); |
| 229 | } |
| 230 | return 1; |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 233 | /* parse the "show events" command, returns 1 if a message is returned, otherwise zero */ |
| 234 | static int cli_parse_show_events(char **args, char *payload, struct appctx *appctx, void *private) |
| 235 | { |
| 236 | struct sink *sink; |
Willy Tarreau | cba8838 | 2022-05-05 15:18:57 +0200 | [diff] [blame] | 237 | uint ring_flags; |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 238 | int arg; |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 239 | |
| 240 | args++; // make args[1] the 1st arg |
| 241 | |
| 242 | if (!*args[1]) { |
| 243 | /* no arg => report the list of supported sink */ |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 244 | chunk_printf(&trash, "Supported events sinks are listed below. Add -w(wait), -n(new). Any key to stop\n"); |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 245 | list_for_each_entry(sink, &sink_list, sink_list) { |
| 246 | chunk_appendf(&trash, " %-10s : type=%s, %u dropped, %s\n", |
| 247 | sink->name, |
| 248 | sink->type == SINK_TYPE_NEW ? "init" : |
| 249 | sink->type == SINK_TYPE_FD ? "fd" : |
| 250 | sink->type == SINK_TYPE_BUFFER ? "buffer" : "?", |
| 251 | sink->ctx.dropped, sink->desc); |
| 252 | } |
| 253 | |
| 254 | trash.area[trash.data] = 0; |
| 255 | return cli_msg(appctx, LOG_WARNING, trash.area); |
| 256 | } |
| 257 | |
| 258 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 259 | return 1; |
| 260 | |
| 261 | sink = sink_find(args[1]); |
| 262 | if (!sink) |
| 263 | return cli_err(appctx, "No such event sink"); |
| 264 | |
| 265 | if (sink->type != SINK_TYPE_BUFFER) |
| 266 | return cli_msg(appctx, LOG_NOTICE, "Nothing to report for this sink"); |
| 267 | |
Willy Tarreau | cba8838 | 2022-05-05 15:18:57 +0200 | [diff] [blame] | 268 | ring_flags = 0; |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 269 | for (arg = 2; *args[arg]; arg++) { |
| 270 | if (strcmp(args[arg], "-w") == 0) |
Willy Tarreau | cba8838 | 2022-05-05 15:18:57 +0200 | [diff] [blame] | 271 | ring_flags |= RING_WF_WAIT_MODE; |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 272 | else if (strcmp(args[arg], "-n") == 0) |
Willy Tarreau | cba8838 | 2022-05-05 15:18:57 +0200 | [diff] [blame] | 273 | ring_flags |= RING_WF_SEEK_NEW; |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 274 | else if (strcmp(args[arg], "-nw") == 0 || strcmp(args[arg], "-wn") == 0) |
Willy Tarreau | cba8838 | 2022-05-05 15:18:57 +0200 | [diff] [blame] | 275 | ring_flags |= RING_WF_WAIT_MODE | RING_WF_SEEK_NEW; |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 276 | else |
| 277 | return cli_err(appctx, "unknown option"); |
| 278 | } |
Willy Tarreau | cba8838 | 2022-05-05 15:18:57 +0200 | [diff] [blame] | 279 | return ring_attach_cli(sink->ctx.ring, appctx, ring_flags); |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 282 | /* Pre-configures a ring proxy to emit connections */ |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 283 | void sink_setup_proxy(struct proxy *px) |
| 284 | { |
| 285 | px->last_change = now.tv_sec; |
| 286 | px->cap = PR_CAP_FE | PR_CAP_BE; |
| 287 | px->maxconn = 0; |
| 288 | px->conn_retries = 1; |
| 289 | px->timeout.server = TICK_ETERNITY; |
| 290 | px->timeout.client = TICK_ETERNITY; |
| 291 | px->timeout.connect = TICK_ETERNITY; |
| 292 | px->accept = NULL; |
| 293 | px->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /* |
Willy Tarreau | 42cc831 | 2022-05-04 20:42:23 +0200 | [diff] [blame] | 297 | * IO Handler to handle message push to syslog tcp server. |
| 298 | * It takes its context from appctx->svcctx. |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 299 | */ |
| 300 | static void sink_forward_io_handler(struct appctx *appctx) |
| 301 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 302 | struct conn_stream *cs = appctx->owner; |
| 303 | struct stream *s = __cs_strm(cs); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 304 | struct sink *sink = strm_fe(s)->parent; |
Willy Tarreau | 42cc831 | 2022-05-04 20:42:23 +0200 | [diff] [blame] | 305 | struct sink_forward_target *sft = appctx->svcctx; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 306 | struct ring *ring = sink->ctx.ring; |
| 307 | struct buffer *buf = &ring->buf; |
| 308 | uint64_t msg_len; |
| 309 | size_t len, cnt, ofs; |
| 310 | int ret = 0; |
| 311 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 312 | /* if stopping was requested, close immediately */ |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 313 | if (unlikely(stopping)) |
| 314 | goto close; |
| 315 | |
| 316 | /* for rex because it seems reset to timeout |
| 317 | * and we don't want expire on this case |
| 318 | * with a syslog server |
| 319 | */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 320 | cs_oc(cs)->rex = TICK_ETERNITY; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 321 | /* rto should not change but it seems the case */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 322 | cs_oc(cs)->rto = TICK_ETERNITY; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 323 | |
| 324 | /* an error was detected */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 325 | if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 326 | goto close; |
| 327 | |
| 328 | /* con closed by server side */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 329 | if ((cs_oc(cs)->flags & CF_SHUTW)) |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 330 | goto close; |
| 331 | |
| 332 | /* if the connection is not established, inform the stream that we want |
| 333 | * to be notified whenever the connection completes. |
| 334 | */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 335 | if (cs_opposite(cs)->state < CS_ST_EST) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 336 | cs_cant_get(cs); |
| 337 | cs_rx_conn_blk(cs); |
| 338 | cs_rx_endp_more(cs); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 339 | return; |
| 340 | } |
| 341 | |
| 342 | HA_SPIN_LOCK(SFT_LOCK, &sft->lock); |
| 343 | if (appctx != sft->appctx) { |
| 344 | HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock); |
| 345 | goto close; |
| 346 | } |
| 347 | ofs = sft->ofs; |
| 348 | |
| 349 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
| 350 | LIST_DEL_INIT(&appctx->wait_entry); |
| 351 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 352 | |
| 353 | HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock); |
| 354 | |
| 355 | /* explanation for the initialization below: it would be better to do |
| 356 | * this in the parsing function but this would occasionally result in |
| 357 | * dropped events because we'd take a reference on the oldest message |
| 358 | * and keep it while being scheduled. Thus instead let's take it the |
| 359 | * first time we enter here so that we have a chance to pass many |
| 360 | * existing messages before grabbing a reference to a location. This |
| 361 | * value cannot be produced after initialization. |
| 362 | */ |
| 363 | if (unlikely(ofs == ~0)) { |
| 364 | ofs = 0; |
| 365 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 366 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 367 | ofs += ring->ofs; |
| 368 | } |
| 369 | |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 370 | /* in this loop, ofs always points to the counter byte that precedes |
| 371 | * the message so that we can take our reference there if we have to |
| 372 | * stop before the end (ret=0). |
| 373 | */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 374 | if (cs_opposite(cs)->state == CS_ST_EST) { |
Emeric Brun | fdabf49 | 2020-12-02 17:02:09 +0100 | [diff] [blame] | 375 | /* we were already there, adjust the offset to be relative to |
| 376 | * the buffer's head and remove us from the counter. |
| 377 | */ |
| 378 | ofs -= ring->ofs; |
| 379 | BUG_ON(ofs >= buf->size); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 380 | HA_ATOMIC_DEC(b_peek(buf, ofs)); |
Emeric Brun | fdabf49 | 2020-12-02 17:02:09 +0100 | [diff] [blame] | 381 | |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 382 | ret = 1; |
| 383 | while (ofs + 1 < b_data(buf)) { |
| 384 | cnt = 1; |
| 385 | len = b_peek_varint(buf, ofs + cnt, &msg_len); |
| 386 | if (!len) |
| 387 | break; |
| 388 | cnt += len; |
| 389 | BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf)); |
| 390 | |
| 391 | if (unlikely(msg_len + 1 > b_size(&trash))) { |
| 392 | /* too large a message to ever fit, let's skip it */ |
| 393 | ofs += cnt + msg_len; |
| 394 | continue; |
| 395 | } |
| 396 | |
| 397 | chunk_reset(&trash); |
| 398 | len = b_getblk(buf, trash.area, msg_len, ofs + cnt); |
| 399 | trash.data += len; |
| 400 | trash.area[trash.data++] = '\n'; |
| 401 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 402 | if (ci_putchk(cs_ic(cs), &trash) == -1) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 403 | cs_rx_room_blk(cs); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 404 | ret = 0; |
| 405 | break; |
| 406 | } |
| 407 | ofs += cnt + msg_len; |
| 408 | } |
| 409 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 410 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 411 | ofs += ring->ofs; |
| 412 | sft->ofs = ofs; |
| 413 | } |
| 414 | HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 415 | |
| 416 | if (ret) { |
| 417 | /* let's be woken up once new data arrive */ |
| 418 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 419 | LIST_APPEND(&ring->waiters, &appctx->wait_entry); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 420 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 421 | cs_rx_endp_done(cs); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 422 | } |
| 423 | HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock); |
| 424 | |
| 425 | /* always drain data from server */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 426 | co_skip(cs_oc(cs), cs_oc(cs)->output); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 427 | return; |
| 428 | |
| 429 | close: |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 430 | cs_shutw(cs); |
| 431 | cs_shutr(cs); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 432 | cs_ic(cs)->flags |= CF_READ_NULL; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 435 | /* |
| 436 | * IO Handler to handle message push to syslog tcp server |
| 437 | * using octet counting frames |
Willy Tarreau | 42cc831 | 2022-05-04 20:42:23 +0200 | [diff] [blame] | 438 | * It takes its context from appctx->svcctx. |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 439 | */ |
| 440 | static void sink_forward_oc_io_handler(struct appctx *appctx) |
| 441 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 442 | struct conn_stream *cs = appctx->owner; |
| 443 | struct stream *s = __cs_strm(cs); |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 444 | struct sink *sink = strm_fe(s)->parent; |
Willy Tarreau | 42cc831 | 2022-05-04 20:42:23 +0200 | [diff] [blame] | 445 | struct sink_forward_target *sft = appctx->svcctx; |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 446 | struct ring *ring = sink->ctx.ring; |
| 447 | struct buffer *buf = &ring->buf; |
| 448 | uint64_t msg_len; |
| 449 | size_t len, cnt, ofs; |
| 450 | int ret = 0; |
| 451 | char *p; |
| 452 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 453 | /* if stopping was requested, close immediately */ |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 454 | if (unlikely(stopping)) |
| 455 | goto close; |
| 456 | |
| 457 | /* for rex because it seems reset to timeout |
| 458 | * and we don't want expire on this case |
| 459 | * with a syslog server |
| 460 | */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 461 | cs_oc(cs)->rex = TICK_ETERNITY; |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 462 | /* rto should not change but it seems the case */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 463 | cs_oc(cs)->rto = TICK_ETERNITY; |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 464 | |
| 465 | /* an error was detected */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 466 | if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 467 | goto close; |
| 468 | |
| 469 | /* con closed by server side */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 470 | if ((cs_oc(cs)->flags & CF_SHUTW)) |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 471 | goto close; |
| 472 | |
| 473 | /* if the connection is not established, inform the stream that we want |
| 474 | * to be notified whenever the connection completes. |
| 475 | */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 476 | if (cs_opposite(cs)->state < CS_ST_EST) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 477 | cs_cant_get(cs); |
| 478 | cs_rx_conn_blk(cs); |
| 479 | cs_rx_endp_more(cs); |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 480 | return; |
| 481 | } |
| 482 | |
| 483 | HA_SPIN_LOCK(SFT_LOCK, &sft->lock); |
| 484 | if (appctx != sft->appctx) { |
| 485 | HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock); |
| 486 | goto close; |
| 487 | } |
| 488 | ofs = sft->ofs; |
| 489 | |
| 490 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
| 491 | LIST_DEL_INIT(&appctx->wait_entry); |
| 492 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 493 | |
| 494 | HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock); |
| 495 | |
| 496 | /* explanation for the initialization below: it would be better to do |
| 497 | * this in the parsing function but this would occasionally result in |
| 498 | * dropped events because we'd take a reference on the oldest message |
| 499 | * and keep it while being scheduled. Thus instead let's take it the |
| 500 | * first time we enter here so that we have a chance to pass many |
| 501 | * existing messages before grabbing a reference to a location. This |
| 502 | * value cannot be produced after initialization. |
| 503 | */ |
| 504 | if (unlikely(ofs == ~0)) { |
| 505 | ofs = 0; |
| 506 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 507 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 508 | ofs += ring->ofs; |
| 509 | } |
| 510 | |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 511 | /* in this loop, ofs always points to the counter byte that precedes |
| 512 | * the message so that we can take our reference there if we have to |
| 513 | * stop before the end (ret=0). |
| 514 | */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 515 | if (cs_opposite(cs)->state == CS_ST_EST) { |
Emeric Brun | fdabf49 | 2020-12-02 17:02:09 +0100 | [diff] [blame] | 516 | /* we were already there, adjust the offset to be relative to |
| 517 | * the buffer's head and remove us from the counter. |
| 518 | */ |
| 519 | ofs -= ring->ofs; |
| 520 | BUG_ON(ofs >= buf->size); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 521 | HA_ATOMIC_DEC(b_peek(buf, ofs)); |
Emeric Brun | fdabf49 | 2020-12-02 17:02:09 +0100 | [diff] [blame] | 522 | |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 523 | ret = 1; |
| 524 | while (ofs + 1 < b_data(buf)) { |
| 525 | cnt = 1; |
| 526 | len = b_peek_varint(buf, ofs + cnt, &msg_len); |
| 527 | if (!len) |
| 528 | break; |
| 529 | cnt += len; |
| 530 | BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf)); |
| 531 | |
| 532 | chunk_reset(&trash); |
| 533 | p = ulltoa(msg_len, trash.area, b_size(&trash)); |
| 534 | if (p) { |
| 535 | trash.data = (p - trash.area) + 1; |
| 536 | *p = ' '; |
| 537 | } |
| 538 | |
| 539 | if (!p || (trash.data + msg_len > b_size(&trash))) { |
| 540 | /* too large a message to ever fit, let's skip it */ |
| 541 | ofs += cnt + msg_len; |
| 542 | continue; |
| 543 | } |
| 544 | |
| 545 | trash.data += b_getblk(buf, p + 1, msg_len, ofs + cnt); |
| 546 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 547 | if (ci_putchk(cs_ic(cs), &trash) == -1) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 548 | cs_rx_room_blk(cs); |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 549 | ret = 0; |
| 550 | break; |
| 551 | } |
| 552 | ofs += cnt + msg_len; |
| 553 | } |
| 554 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 555 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 556 | ofs += ring->ofs; |
| 557 | sft->ofs = ofs; |
| 558 | } |
| 559 | HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 560 | |
| 561 | if (ret) { |
| 562 | /* let's be woken up once new data arrive */ |
| 563 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 564 | LIST_APPEND(&ring->waiters, &appctx->wait_entry); |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 565 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 566 | cs_rx_endp_done(cs); |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 567 | } |
| 568 | HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock); |
| 569 | |
| 570 | /* always drain data from server */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 571 | co_skip(cs_oc(cs), cs_oc(cs)->output); |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 572 | return; |
| 573 | |
| 574 | close: |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 575 | cs_shutw(cs); |
| 576 | cs_shutr(cs); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 577 | cs_ic(cs)->flags |= CF_READ_NULL; |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 580 | void __sink_forward_session_deinit(struct sink_forward_target *sft) |
| 581 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 582 | struct stream *s = __cs_strm(sft->appctx->owner); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 583 | struct sink *sink; |
| 584 | |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 585 | sink = strm_fe(s)->parent; |
| 586 | if (!sink) |
| 587 | return; |
| 588 | |
| 589 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &sink->ctx.ring->lock); |
| 590 | LIST_DEL_INIT(&sft->appctx->wait_entry); |
| 591 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &sink->ctx.ring->lock); |
| 592 | |
| 593 | sft->appctx = NULL; |
| 594 | task_wakeup(sink->forward_task, TASK_WOKEN_MSG); |
| 595 | } |
| 596 | |
| 597 | |
| 598 | static void sink_forward_session_release(struct appctx *appctx) |
| 599 | { |
Willy Tarreau | 42cc831 | 2022-05-04 20:42:23 +0200 | [diff] [blame] | 600 | struct sink_forward_target *sft = appctx->svcctx; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 601 | |
| 602 | if (!sft) |
| 603 | return; |
| 604 | |
| 605 | HA_SPIN_LOCK(SFT_LOCK, &sft->lock); |
| 606 | if (sft->appctx == appctx) |
| 607 | __sink_forward_session_deinit(sft); |
| 608 | HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock); |
| 609 | } |
| 610 | |
| 611 | static struct applet sink_forward_applet = { |
| 612 | .obj_type = OBJ_TYPE_APPLET, |
| 613 | .name = "<SINKFWD>", /* used for logging */ |
| 614 | .fct = sink_forward_io_handler, |
| 615 | .release = sink_forward_session_release, |
| 616 | }; |
| 617 | |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 618 | static struct applet sink_forward_oc_applet = { |
| 619 | .obj_type = OBJ_TYPE_APPLET, |
| 620 | .name = "<SINKFWDOC>", /* used for logging */ |
| 621 | .fct = sink_forward_oc_io_handler, |
| 622 | .release = sink_forward_session_release, |
| 623 | }; |
| 624 | |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 625 | /* |
| 626 | * Create a new peer session in assigned state (connect will start automatically) |
Willy Tarreau | 42cc831 | 2022-05-04 20:42:23 +0200 | [diff] [blame] | 627 | * It sets its context into appctx->svcctx. |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 628 | */ |
| 629 | static struct appctx *sink_forward_session_create(struct sink *sink, struct sink_forward_target *sft) |
| 630 | { |
| 631 | struct proxy *p = sink->forward_px; |
| 632 | struct appctx *appctx; |
| 633 | struct session *sess; |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 634 | struct conn_stream *cs; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 635 | struct stream *s; |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 636 | struct applet *applet = &sink_forward_applet; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 637 | struct sockaddr_storage *addr = NULL; |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 638 | |
| 639 | if (sft->srv->log_proto == SRV_LOG_PROTO_OCTET_COUNTING) |
| 640 | applet = &sink_forward_oc_applet; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 641 | |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 642 | appctx = appctx_new(applet, NULL); |
Christopher Faulet | 2479e5f | 2022-01-19 14:50:11 +0100 | [diff] [blame] | 643 | if (!appctx) |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 644 | goto out_close; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 645 | |
Willy Tarreau | 42cc831 | 2022-05-04 20:42:23 +0200 | [diff] [blame] | 646 | appctx->svcctx = (void *)sft; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 647 | |
| 648 | sess = session_new(p, NULL, &appctx->obj_type); |
| 649 | if (!sess) { |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 650 | ha_alert("out of memory in sink_forward_session_create().\n"); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 651 | goto out_free_appctx; |
| 652 | } |
| 653 | |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 654 | if (!sockaddr_alloc(&addr, &sft->srv->addr, sizeof(sft->srv->addr))) |
Christopher Faulet | 2479e5f | 2022-01-19 14:50:11 +0100 | [diff] [blame] | 655 | goto out_free_sess; |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 656 | |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 657 | cs = cs_new_from_applet(appctx->endp, sess, &BUF_NULL); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 658 | if (!cs) { |
| 659 | ha_alert("Failed to initialize stream in sink_forward_session_create().\n"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 660 | goto out_free_addr; |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 661 | } |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 662 | s = DISGUISE(cs_strm(cs)); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 663 | |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 664 | s->csb->dst = addr; |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 665 | s->csb->flags |= CS_FL_NOLINGER; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 666 | |
| 667 | s->target = &sft->srv->obj_type; |
Willy Tarreau | 03bd395 | 2022-05-02 16:36:47 +0200 | [diff] [blame] | 668 | s->flags = SF_ASSIGNED; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 669 | |
| 670 | s->do_log = NULL; |
| 671 | s->uniq_id = 0; |
| 672 | |
| 673 | s->res.flags |= CF_READ_DONTWAIT; |
| 674 | /* for rto and rex to eternity to not expire on idle recv: |
| 675 | * We are using a syslog server. |
| 676 | */ |
| 677 | s->res.rto = TICK_ETERNITY; |
| 678 | s->res.rex = TICK_ETERNITY; |
| 679 | sft->appctx = appctx; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 680 | return appctx; |
| 681 | |
| 682 | /* Error unrolling */ |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 683 | out_free_addr: |
| 684 | sockaddr_free(&addr); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 685 | out_free_sess: |
| 686 | session_free(sess); |
| 687 | out_free_appctx: |
| 688 | appctx_free(appctx); |
| 689 | out_close: |
| 690 | return NULL; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * Task to handle connctions to forward servers |
| 695 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 696 | static struct task *process_sink_forward(struct task * task, void *context, unsigned int state) |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 697 | { |
| 698 | struct sink *sink = (struct sink *)context; |
| 699 | struct sink_forward_target *sft = sink->sft; |
| 700 | |
| 701 | task->expire = TICK_ETERNITY; |
| 702 | |
| 703 | if (!stopping) { |
| 704 | while (sft) { |
| 705 | HA_SPIN_LOCK(SFT_LOCK, &sft->lock); |
| 706 | /* if appctx is NULL, start a new session */ |
| 707 | if (!sft->appctx) |
| 708 | sft->appctx = sink_forward_session_create(sink, sft); |
| 709 | HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock); |
| 710 | sft = sft->next; |
| 711 | } |
| 712 | } |
| 713 | else { |
| 714 | while (sft) { |
| 715 | HA_SPIN_LOCK(SFT_LOCK, &sft->lock); |
| 716 | /* awake applet to perform a clean close */ |
| 717 | if (sft->appctx) |
| 718 | appctx_wakeup(sft->appctx); |
| 719 | HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock); |
| 720 | sft = sft->next; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | return task; |
| 725 | } |
| 726 | /* |
| 727 | * Init task to manage connctions to forward servers |
| 728 | * |
| 729 | * returns 0 in case of error. |
| 730 | */ |
| 731 | int sink_init_forward(struct sink *sink) |
| 732 | { |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 733 | sink->forward_task = task_new_anywhere(); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 734 | if (!sink->forward_task) |
| 735 | return 0; |
| 736 | |
| 737 | sink->forward_task->process = process_sink_forward; |
| 738 | sink->forward_task->context = (void *)sink; |
| 739 | sink->forward_sighandler = signal_register_task(0, sink->forward_task, 0); |
| 740 | task_wakeup(sink->forward_task, TASK_WOKEN_INIT); |
| 741 | return 1; |
| 742 | } |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 743 | /* |
| 744 | * Parse "ring" section and create corresponding sink buffer. |
| 745 | * |
| 746 | * The function returns 0 in success case, otherwise, it returns error |
| 747 | * flags. |
| 748 | */ |
| 749 | int cfg_parse_ring(const char *file, int linenum, char **args, int kwm) |
| 750 | { |
| 751 | int err_code = 0; |
| 752 | const char *inv; |
| 753 | size_t size = BUFSIZE; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 754 | struct proxy *p; |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 755 | |
| 756 | if (strcmp(args[0], "ring") == 0) { /* new peers section */ |
| 757 | if (!*args[1]) { |
| 758 | ha_alert("parsing [%s:%d] : missing ring name.\n", file, linenum); |
| 759 | err_code |= ERR_ALERT | ERR_FATAL; |
| 760 | goto err; |
| 761 | } |
| 762 | |
| 763 | inv = invalid_char(args[1]); |
| 764 | if (inv) { |
| 765 | ha_alert("parsing [%s:%d] : invalid ring name '%s' (character '%c' is not permitted).\n", file, linenum, args[1], *inv); |
| 766 | err_code |= ERR_ALERT | ERR_FATAL; |
| 767 | goto err; |
| 768 | } |
| 769 | |
| 770 | if (sink_find(args[1])) { |
| 771 | ha_alert("parsing [%s:%d] : sink named '%s' already exists.\n", file, linenum, args[1]); |
| 772 | err_code |= ERR_ALERT | ERR_FATAL; |
| 773 | goto err; |
| 774 | } |
| 775 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 776 | cfg_sink = sink_new_buf(args[1], args[1], LOG_FORMAT_RAW, size); |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 777 | if (!cfg_sink || cfg_sink->type != SINK_TYPE_BUFFER) { |
| 778 | ha_alert("parsing [%s:%d] : unable to create a new sink buffer for ring '%s'.\n", file, linenum, args[1]); |
| 779 | err_code |= ERR_ALERT | ERR_FATAL; |
| 780 | goto err; |
| 781 | } |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 782 | |
| 783 | /* allocate new proxy to handle forwards */ |
| 784 | p = calloc(1, sizeof *p); |
| 785 | if (!p) { |
| 786 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 787 | err_code |= ERR_ALERT | ERR_FATAL; |
| 788 | goto err; |
| 789 | } |
| 790 | |
| 791 | init_new_proxy(p); |
| 792 | sink_setup_proxy(p); |
| 793 | p->parent = cfg_sink; |
| 794 | p->id = strdup(args[1]); |
| 795 | p->conf.args.file = p->conf.file = strdup(file); |
| 796 | p->conf.args.line = p->conf.line = linenum; |
| 797 | cfg_sink->forward_px = p; |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 798 | } |
| 799 | else if (strcmp(args[0], "size") == 0) { |
| 800 | size = atol(args[1]); |
| 801 | if (!size) { |
| 802 | ha_alert("parsing [%s:%d] : invalid size '%s' for new sink buffer.\n", file, linenum, args[1]); |
| 803 | err_code |= ERR_ALERT | ERR_FATAL; |
| 804 | goto err; |
| 805 | } |
| 806 | |
| 807 | if (!cfg_sink || (cfg_sink->type != SINK_TYPE_BUFFER) |
| 808 | || !ring_resize(cfg_sink->ctx.ring, size)) { |
| 809 | ha_alert("parsing [%s:%d] : fail to set sink buffer size '%s'.\n", file, linenum, args[1]); |
| 810 | err_code |= ERR_ALERT | ERR_FATAL; |
| 811 | goto err; |
| 812 | } |
| 813 | } |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 814 | else if (strcmp(args[0],"server") == 0) { |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 815 | err_code |= parse_server(file, linenum, args, cfg_sink->forward_px, NULL, |
| 816 | SRV_PARSE_PARSE_ADDR|SRV_PARSE_INITIAL_RESOLVE); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 817 | } |
| 818 | else if (strcmp(args[0],"timeout") == 0) { |
| 819 | if (!cfg_sink || !cfg_sink->forward_px) { |
| 820 | ha_alert("parsing [%s:%d] : unable to set timeout '%s'.\n", file, linenum, args[1]); |
| 821 | err_code |= ERR_ALERT | ERR_FATAL; |
| 822 | goto err; |
| 823 | } |
| 824 | |
| 825 | if (strcmp(args[1], "connect") == 0 || |
| 826 | strcmp(args[1], "server") == 0) { |
| 827 | const char *res; |
| 828 | unsigned int tout; |
| 829 | |
| 830 | if (!*args[2]) { |
| 831 | ha_alert("parsing [%s:%d] : '%s %s' expects <time> as argument.\n", |
| 832 | file, linenum, args[0], args[1]); |
| 833 | err_code |= ERR_ALERT | ERR_FATAL; |
| 834 | goto err; |
| 835 | } |
| 836 | res = parse_time_err(args[2], &tout, TIME_UNIT_MS); |
| 837 | if (res == PARSE_TIME_OVER) { |
| 838 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n", |
| 839 | file, linenum, args[2], args[0], args[1]); |
| 840 | err_code |= ERR_ALERT | ERR_FATAL; |
| 841 | goto err; |
| 842 | } |
| 843 | else if (res == PARSE_TIME_UNDER) { |
| 844 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n", |
| 845 | file, linenum, args[2], args[0], args[1]); |
| 846 | err_code |= ERR_ALERT | ERR_FATAL; |
| 847 | goto err; |
| 848 | } |
| 849 | else if (res) { |
| 850 | ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s %s>.\n", |
| 851 | file, linenum, *res, args[0], args[1]); |
| 852 | err_code |= ERR_ALERT | ERR_FATAL; |
| 853 | goto err; |
| 854 | } |
| 855 | if (args[1][2] == 'c') |
| 856 | cfg_sink->forward_px->timeout.connect = tout; |
| 857 | else |
| 858 | cfg_sink->forward_px->timeout.server = tout; |
| 859 | } |
| 860 | } |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 861 | else if (strcmp(args[0],"format") == 0) { |
| 862 | if (!cfg_sink) { |
| 863 | ha_alert("parsing [%s:%d] : unable to set format '%s'.\n", file, linenum, args[1]); |
| 864 | err_code |= ERR_ALERT | ERR_FATAL; |
| 865 | goto err; |
| 866 | } |
| 867 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 868 | cfg_sink->fmt = get_log_format(args[1]); |
| 869 | if (cfg_sink->fmt == LOG_FORMAT_UNSPEC) { |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 870 | ha_alert("parsing [%s:%d] : unknown format '%s'.\n", file, linenum, args[1]); |
| 871 | err_code |= ERR_ALERT | ERR_FATAL; |
| 872 | goto err; |
| 873 | } |
| 874 | } |
| 875 | else if (strcmp(args[0],"maxlen") == 0) { |
| 876 | if (!cfg_sink) { |
| 877 | ha_alert("parsing [%s:%d] : unable to set event max length '%s'.\n", file, linenum, args[1]); |
| 878 | err_code |= ERR_ALERT | ERR_FATAL; |
| 879 | goto err; |
| 880 | } |
| 881 | |
| 882 | cfg_sink->maxlen = atol(args[1]); |
| 883 | if (!cfg_sink->maxlen) { |
| 884 | ha_alert("parsing [%s:%d] : invalid size '%s' for new sink buffer.\n", file, linenum, args[1]); |
| 885 | err_code |= ERR_ALERT | ERR_FATAL; |
| 886 | goto err; |
| 887 | } |
| 888 | } |
| 889 | else if (strcmp(args[0],"description") == 0) { |
| 890 | if (!cfg_sink) { |
| 891 | ha_alert("parsing [%s:%d] : unable to set description '%s'.\n", file, linenum, args[1]); |
| 892 | err_code |= ERR_ALERT | ERR_FATAL; |
| 893 | goto err; |
| 894 | } |
| 895 | |
| 896 | if (!*args[1]) { |
| 897 | ha_alert("parsing [%s:%d] : missing ring description text.\n", file, linenum); |
| 898 | err_code |= ERR_ALERT | ERR_FATAL; |
| 899 | goto err; |
| 900 | } |
| 901 | |
| 902 | free(cfg_sink->desc); |
| 903 | |
| 904 | cfg_sink->desc = strdup(args[1]); |
| 905 | if (!cfg_sink->desc) { |
| 906 | ha_alert("parsing [%s:%d] : fail to set description '%s'.\n", file, linenum, args[1]); |
| 907 | err_code |= ERR_ALERT | ERR_FATAL; |
| 908 | goto err; |
| 909 | } |
| 910 | } |
Emeric Brun | 9f2ff3a | 2020-05-29 15:47:52 +0200 | [diff] [blame] | 911 | else { |
| 912 | ha_alert("parsing [%s:%d] : unknown statement '%s'.\n", file, linenum, args[0]); |
| 913 | err_code |= ERR_ALERT | ERR_FATAL; |
| 914 | goto err; |
| 915 | } |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 916 | |
| 917 | err: |
| 918 | return err_code; |
| 919 | } |
| 920 | |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 921 | /* Creates an new sink buffer from a log server. |
| 922 | * |
| 923 | * It uses the logsrvaddress to declare a forward |
| 924 | * server for this buffer. And it initializes the |
| 925 | * forwarding. |
| 926 | * |
| 927 | * The function returns a pointer on the |
| 928 | * allocated struct sink if allocate |
| 929 | * and initialize succeed, else if it fails |
| 930 | * it returns NULL. |
| 931 | * |
| 932 | * Note: the sink is created using the name |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 933 | * specified into logsrv->ring_name |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 934 | */ |
| 935 | struct sink *sink_new_from_logsrv(struct logsrv *logsrv) |
| 936 | { |
| 937 | struct proxy *p = NULL; |
| 938 | struct sink *sink = NULL; |
| 939 | struct server *srv = NULL; |
| 940 | struct sink_forward_target *sft = NULL; |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 941 | |
| 942 | /* allocate new proxy to handle |
| 943 | * forward to a stream server |
| 944 | */ |
| 945 | p = calloc(1, sizeof *p); |
| 946 | if (!p) { |
| 947 | goto error; |
| 948 | } |
| 949 | |
| 950 | init_new_proxy(p); |
| 951 | sink_setup_proxy(p); |
| 952 | p->id = strdup(logsrv->ring_name); |
| 953 | p->conf.args.file = p->conf.file = strdup(logsrv->conf.file); |
| 954 | p->conf.args.line = p->conf.line = logsrv->conf.line; |
| 955 | |
| 956 | /* allocate a new server to forward messages |
| 957 | * from ring buffer |
| 958 | */ |
| 959 | srv = new_server(p); |
| 960 | if (!srv) |
| 961 | goto error; |
| 962 | |
| 963 | /* init server */ |
| 964 | srv->id = strdup(logsrv->ring_name); |
| 965 | srv->conf.file = strdup(logsrv->conf.file); |
| 966 | srv->conf.line = logsrv->conf.line; |
| 967 | srv->addr = logsrv->addr; |
| 968 | srv->svc_port = get_host_port(&logsrv->addr); |
| 969 | HA_SPIN_INIT(&srv->lock); |
| 970 | |
| 971 | /* process per thread init */ |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 972 | if (srv_init_per_thr(srv) == -1) |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 973 | goto error; |
| 974 | |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 975 | /* the servers are linked backwards |
| 976 | * first into proxy |
| 977 | */ |
| 978 | p->srv = srv; |
| 979 | srv->next = p->srv; |
| 980 | |
| 981 | /* allocate sink_forward_target descriptor */ |
| 982 | sft = calloc(1, sizeof(*sft)); |
| 983 | if (!sft) |
| 984 | goto error; |
| 985 | |
| 986 | /* init sink_forward_target offset */ |
| 987 | sft->srv = srv; |
| 988 | sft->appctx = NULL; |
| 989 | sft->ofs = ~0; |
| 990 | HA_SPIN_INIT(&sft->lock); |
| 991 | |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 992 | /* prepare description for the sink */ |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 993 | chunk_reset(&trash); |
| 994 | chunk_printf(&trash, "created from logserver declared into '%s' at line %d", logsrv->conf.file, logsrv->conf.line); |
| 995 | |
| 996 | /* allocate a new sink buffer */ |
| 997 | sink = sink_new_buf(logsrv->ring_name, trash.area, logsrv->format, BUFSIZE); |
| 998 | if (!sink || sink->type != SINK_TYPE_BUFFER) { |
| 999 | goto error; |
| 1000 | } |
| 1001 | |
| 1002 | /* link sink_forward_target to proxy */ |
| 1003 | sink->forward_px = p; |
| 1004 | p->parent = sink; |
| 1005 | |
| 1006 | /* insert into sink_forward_targets |
| 1007 | * list into sink |
| 1008 | */ |
| 1009 | sft->next = sink->sft; |
| 1010 | sink->sft = sft; |
| 1011 | |
| 1012 | /* mark server as an attached reader to the ring */ |
| 1013 | if (!ring_attach(sink->ctx.ring)) { |
| 1014 | /* should never fail since there is |
| 1015 | * only one reader |
| 1016 | */ |
| 1017 | goto error; |
| 1018 | } |
| 1019 | |
| 1020 | /* initialize sink buffer forwarding */ |
| 1021 | if (!sink_init_forward(sink)) |
| 1022 | goto error; |
| 1023 | |
| 1024 | /* reset familyt of logsrv to consider the ring buffer target */ |
| 1025 | logsrv->addr.ss_family = AF_UNSPEC; |
| 1026 | |
| 1027 | return sink; |
| 1028 | error: |
| 1029 | if (p) { |
| 1030 | if (p->id) |
| 1031 | free(p->id); |
| 1032 | if (p->conf.file) |
| 1033 | free(p->conf.file); |
| 1034 | |
| 1035 | free(p); |
| 1036 | } |
| 1037 | |
| 1038 | if (srv) { |
| 1039 | if (srv->id) |
| 1040 | free(srv->id); |
| 1041 | if (srv->conf.file) |
| 1042 | free((void *)srv->conf.file); |
| 1043 | if (srv->per_thr) |
| 1044 | free(srv->per_thr); |
| 1045 | free(srv); |
| 1046 | } |
| 1047 | |
| 1048 | if (sft) |
| 1049 | free(sft); |
| 1050 | |
| 1051 | if (sink) { |
| 1052 | if (sink->ctx.ring) |
| 1053 | ring_free(sink->ctx.ring); |
| 1054 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1055 | LIST_DELETE(&sink->sink_list); |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 1056 | free(sink->name); |
| 1057 | free(sink->desc); |
| 1058 | free(sink); |
| 1059 | } |
| 1060 | |
| 1061 | return NULL; |
| 1062 | } |
| 1063 | |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1064 | /* |
| 1065 | * Post parsing "ring" section. |
| 1066 | * |
| 1067 | * The function returns 0 in success case, otherwise, it returns error |
| 1068 | * flags. |
| 1069 | */ |
| 1070 | int cfg_post_parse_ring() |
| 1071 | { |
| 1072 | int err_code = 0; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 1073 | struct server *srv; |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1074 | |
| 1075 | if (cfg_sink && (cfg_sink->type == SINK_TYPE_BUFFER)) { |
| 1076 | if (cfg_sink->maxlen > b_size(&cfg_sink->ctx.ring->buf)) { |
| 1077 | ha_warning("ring '%s' event max length '%u' exceeds size, forced to size '%lu'.\n", |
Willy Tarreau | 570a22b | 2020-06-02 12:00:46 +0200 | [diff] [blame] | 1078 | cfg_sink->name, cfg_sink->maxlen, (unsigned long)b_size(&cfg_sink->ctx.ring->buf)); |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1079 | cfg_sink->maxlen = b_size(&cfg_sink->ctx.ring->buf); |
| 1080 | err_code |= ERR_ALERT; |
| 1081 | } |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 1082 | |
| 1083 | /* prepare forward server descriptors */ |
| 1084 | if (cfg_sink->forward_px) { |
| 1085 | srv = cfg_sink->forward_px->srv; |
| 1086 | while (srv) { |
| 1087 | struct sink_forward_target *sft; |
| 1088 | /* init ssl if needed */ |
| 1089 | if (srv->use_ssl == 1 && xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) { |
| 1090 | if (xprt_get(XPRT_SSL)->prepare_srv(srv)) { |
| 1091 | ha_alert("unable to prepare SSL for server '%s' in ring '%s'.\n", srv->id, cfg_sink->name); |
| 1092 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1093 | } |
| 1094 | } |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1095 | |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 1096 | /* allocate sink_forward_target descriptor */ |
| 1097 | sft = calloc(1, sizeof(*sft)); |
| 1098 | if (!sft) { |
| 1099 | ha_alert("memory allocation error initializing server '%s' in ring '%s'.\n",srv->id, cfg_sink->name); |
| 1100 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1101 | break; |
| 1102 | } |
| 1103 | sft->srv = srv; |
| 1104 | sft->appctx = NULL; |
| 1105 | sft->ofs = ~0; /* init ring offset */ |
| 1106 | sft->next = cfg_sink->sft; |
| 1107 | HA_SPIN_INIT(&sft->lock); |
| 1108 | |
| 1109 | /* mark server attached to the ring */ |
| 1110 | if (!ring_attach(cfg_sink->ctx.ring)) { |
| 1111 | ha_alert("server '%s' sets too many watchers > 255 on ring '%s'.\n", srv->id, cfg_sink->name); |
| 1112 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1113 | } |
| 1114 | cfg_sink->sft = sft; |
| 1115 | srv = srv->next; |
| 1116 | } |
| 1117 | sink_init_forward(cfg_sink); |
| 1118 | } |
| 1119 | } |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1120 | cfg_sink = NULL; |
| 1121 | |
| 1122 | return err_code; |
| 1123 | } |
| 1124 | |
| 1125 | /* resolve sink names at end of config. Returns 0 on success otherwise error |
| 1126 | * flags. |
| 1127 | */ |
| 1128 | int post_sink_resolve() |
| 1129 | { |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1130 | int err_code = ERR_NONE; |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1131 | struct logsrv *logsrv, *logb; |
| 1132 | struct sink *sink; |
| 1133 | struct proxy *px; |
| 1134 | |
| 1135 | list_for_each_entry_safe(logsrv, logb, &global.logsrvs, list) { |
| 1136 | if (logsrv->type == LOG_TARGET_BUFFER) { |
| 1137 | sink = sink_find(logsrv->ring_name); |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 1138 | if (!sink) { |
| 1139 | /* LOG_TARGET_BUFFER but !AF_UNSPEC |
| 1140 | * means we must allocate a sink |
| 1141 | * buffer to send messages to this logsrv |
| 1142 | */ |
| 1143 | if (logsrv->addr.ss_family != AF_UNSPEC) { |
| 1144 | sink = sink_new_from_logsrv(logsrv); |
| 1145 | if (!sink) { |
| 1146 | ha_alert("global stream log server declared in file '%s' at line %d cannot be initialized'.\n", |
| 1147 | logsrv->conf.file, logsrv->conf.line); |
| 1148 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1149 | } |
| 1150 | } |
| 1151 | else { |
| 1152 | ha_alert("global log server declared in file '%s' at line %d uses unknown ring named '%s'.\n", |
| 1153 | logsrv->conf.file, logsrv->conf.line, logsrv->ring_name); |
| 1154 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1155 | } |
| 1156 | } |
| 1157 | else if (sink->type != SINK_TYPE_BUFFER) { |
| 1158 | ha_alert("global log server declared in file '%s' at line %d uses incompatible ring '%s'.\n", |
| 1159 | logsrv->conf.file, logsrv->conf.line, logsrv->ring_name); |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1160 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1161 | } |
| 1162 | logsrv->sink = sink; |
| 1163 | } |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 1164 | |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | for (px = proxies_list; px; px = px->next) { |
| 1168 | list_for_each_entry_safe(logsrv, logb, &px->logsrvs, list) { |
| 1169 | if (logsrv->type == LOG_TARGET_BUFFER) { |
| 1170 | sink = sink_find(logsrv->ring_name); |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 1171 | if (!sink) { |
| 1172 | /* LOG_TARGET_BUFFER but !AF_UNSPEC |
| 1173 | * means we must allocate a sink |
| 1174 | * buffer to send messages to this logsrv |
| 1175 | */ |
| 1176 | if (logsrv->addr.ss_family != AF_UNSPEC) { |
| 1177 | sink = sink_new_from_logsrv(logsrv); |
| 1178 | if (!sink) { |
| 1179 | ha_alert("log server declared in proxy section '%s' file '%s' at line %d cannot be initialized'.\n", |
| 1180 | px->id, logsrv->conf.file, logsrv->conf.line); |
| 1181 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1182 | } |
| 1183 | } |
| 1184 | else { |
| 1185 | ha_alert("log server declared in proxy section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n", |
| 1186 | px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name); |
| 1187 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1188 | } |
| 1189 | } |
| 1190 | else if (sink->type != SINK_TYPE_BUFFER) { |
| 1191 | ha_alert("log server declared in proxy section '%s' in file '%s' at line %d uses incomatible ring named '%s'.\n", |
| 1192 | px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name); |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1193 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1194 | } |
| 1195 | logsrv->sink = sink; |
| 1196 | } |
| 1197 | } |
| 1198 | } |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 1199 | |
| 1200 | for (px = cfg_log_forward; px; px = px->next) { |
| 1201 | list_for_each_entry_safe(logsrv, logb, &px->logsrvs, list) { |
| 1202 | if (logsrv->type == LOG_TARGET_BUFFER) { |
| 1203 | sink = sink_find(logsrv->ring_name); |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 1204 | if (!sink) { |
| 1205 | /* LOG_TARGET_BUFFER but !AF_UNSPEC |
| 1206 | * means we must allocate a sink |
| 1207 | * buffer to send messages to this logsrv |
| 1208 | */ |
| 1209 | if (logsrv->addr.ss_family != AF_UNSPEC) { |
| 1210 | sink = sink_new_from_logsrv(logsrv); |
| 1211 | if (!sink) { |
| 1212 | ha_alert("log server declared in log-forward section '%s' file '%s' at line %d cannot be initialized'.\n", |
| 1213 | px->id, logsrv->conf.file, logsrv->conf.line); |
| 1214 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1215 | } |
| 1216 | } |
| 1217 | else { |
| 1218 | ha_alert("log server declared in log-forward section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n", |
| 1219 | px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name); |
| 1220 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1221 | } |
| 1222 | } |
| 1223 | else if (sink->type != SINK_TYPE_BUFFER) { |
| 1224 | ha_alert("log server declared in log-forward section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n", |
| 1225 | px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name); |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 1226 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1227 | } |
| 1228 | logsrv->sink = sink; |
| 1229 | } |
| 1230 | } |
| 1231 | } |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1232 | return err_code; |
| 1233 | } |
| 1234 | |
| 1235 | |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 1236 | static void sink_init() |
| 1237 | { |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1238 | sink_new_fd("stdout", "standard output (fd#1)", LOG_FORMAT_RAW, 1); |
| 1239 | sink_new_fd("stderr", "standard output (fd#2)", LOG_FORMAT_RAW, 2); |
| 1240 | sink_new_buf("buf0", "in-memory ring buffer", LOG_FORMAT_TIMED, 1048576); |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | static void sink_deinit() |
| 1244 | { |
| 1245 | struct sink *sink, *sb; |
| 1246 | |
| 1247 | list_for_each_entry_safe(sink, sb, &sink_list, sink_list) { |
| 1248 | if (sink->type == SINK_TYPE_BUFFER) |
| 1249 | ring_free(sink->ctx.ring); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1250 | LIST_DELETE(&sink->sink_list); |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1251 | free(sink->name); |
| 1252 | free(sink->desc); |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 1253 | free(sink); |
| 1254 | } |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | INITCALL0(STG_REGISTER, sink_init); |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 1258 | REGISTER_POST_DEINIT(sink_deinit); |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 1259 | |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 1260 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 1261 | { { "show", "events", NULL }, "show events [<sink>] [-w] [-n] : show event sink state", cli_parse_show_events, NULL, NULL }, |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 1262 | {{},} |
| 1263 | }}; |
| 1264 | |
| 1265 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 1266 | |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1267 | /* config parsers for this section */ |
| 1268 | REGISTER_CONFIG_SECTION("ring", cfg_parse_ring, cfg_post_parse_ring); |
| 1269 | REGISTER_POST_CHECK(post_sink_resolve); |
| 1270 | |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 1271 | /* |
| 1272 | * Local variables: |
| 1273 | * c-indent-level: 8 |
| 1274 | * c-basic-offset: 8 |
| 1275 | * End: |
| 1276 | */ |