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> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 26 | #include <haproxy/errors.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 27 | #include <haproxy/list.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 28 | #include <haproxy/log.h> |
Willy Tarreau | 817538e | 2021-05-08 20:20:21 +0200 | [diff] [blame] | 29 | #include <haproxy/proxy.h> |
Willy Tarreau | d2ad57c | 2020-06-03 19:43:35 +0200 | [diff] [blame] | 30 | #include <haproxy/ring.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 31 | #include <haproxy/sc_strm.h> |
Willy Tarreau | 3727a8a | 2020-06-04 17:37:26 +0200 | [diff] [blame] | 32 | #include <haproxy/signal.h> |
Willy Tarreau | ba2f73d | 2020-06-03 20:02:28 +0200 | [diff] [blame] | 33 | #include <haproxy/sink.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 34 | #include <haproxy/stconn.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 | { |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 302 | struct stconn *sc = appctx_sc(appctx); |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 303 | struct stream *s = __sc_strm(sc); |
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 | */ |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 320 | sc_oc(sc)->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 */ |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 322 | sc_oc(sc)->rto = TICK_ETERNITY; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 323 | |
| 324 | /* an error was detected */ |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 325 | if (unlikely(sc_ic(sc)->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 */ |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 329 | if ((sc_oc(sc)->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 | */ |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 335 | if (sc_opposite(sc)->state < SC_ST_EST) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 336 | applet_need_more_data(appctx); |
Willy Tarreau | b23edc8 | 2022-05-24 16:49:03 +0200 | [diff] [blame] | 337 | se_need_remote_conn(appctx->sedesc); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 338 | applet_have_more_data(appctx); |
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 | */ |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 374 | if (sc_opposite(sc)->state == SC_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 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 402 | if (applet_putchk(appctx, &trash) == -1) { |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 403 | ret = 0; |
| 404 | break; |
| 405 | } |
| 406 | ofs += cnt + msg_len; |
| 407 | } |
| 408 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 409 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 410 | ofs += ring->ofs; |
| 411 | sft->ofs = ofs; |
| 412 | } |
| 413 | HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 414 | |
| 415 | if (ret) { |
| 416 | /* let's be woken up once new data arrive */ |
| 417 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 418 | LIST_APPEND(&ring->waiters, &appctx->wait_entry); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 419 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 420 | applet_have_no_more_data(appctx); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 421 | } |
| 422 | HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock); |
| 423 | |
| 424 | /* always drain data from server */ |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 425 | co_skip(sc_oc(sc), sc_oc(sc)->output); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 426 | return; |
| 427 | |
| 428 | close: |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 429 | sc_shutw(sc); |
| 430 | sc_shutr(sc); |
| 431 | sc_ic(sc)->flags |= CF_READ_NULL; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 434 | /* |
| 435 | * IO Handler to handle message push to syslog tcp server |
| 436 | * using octet counting frames |
Willy Tarreau | 42cc831 | 2022-05-04 20:42:23 +0200 | [diff] [blame] | 437 | * It takes its context from appctx->svcctx. |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 438 | */ |
| 439 | static void sink_forward_oc_io_handler(struct appctx *appctx) |
| 440 | { |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 441 | struct stconn *sc = appctx_sc(appctx); |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 442 | struct stream *s = __sc_strm(sc); |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 443 | struct sink *sink = strm_fe(s)->parent; |
Willy Tarreau | 42cc831 | 2022-05-04 20:42:23 +0200 | [diff] [blame] | 444 | struct sink_forward_target *sft = appctx->svcctx; |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 445 | struct ring *ring = sink->ctx.ring; |
| 446 | struct buffer *buf = &ring->buf; |
| 447 | uint64_t msg_len; |
| 448 | size_t len, cnt, ofs; |
| 449 | int ret = 0; |
| 450 | char *p; |
| 451 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 452 | /* if stopping was requested, close immediately */ |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 453 | if (unlikely(stopping)) |
| 454 | goto close; |
| 455 | |
| 456 | /* for rex because it seems reset to timeout |
| 457 | * and we don't want expire on this case |
| 458 | * with a syslog server |
| 459 | */ |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 460 | sc_oc(sc)->rex = TICK_ETERNITY; |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 461 | /* rto should not change but it seems the case */ |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 462 | sc_oc(sc)->rto = TICK_ETERNITY; |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 463 | |
| 464 | /* an error was detected */ |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 465 | if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 466 | goto close; |
| 467 | |
| 468 | /* con closed by server side */ |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 469 | if ((sc_oc(sc)->flags & CF_SHUTW)) |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 470 | goto close; |
| 471 | |
| 472 | /* if the connection is not established, inform the stream that we want |
| 473 | * to be notified whenever the connection completes. |
| 474 | */ |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 475 | if (sc_opposite(sc)->state < SC_ST_EST) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 476 | applet_need_more_data(appctx); |
Willy Tarreau | b23edc8 | 2022-05-24 16:49:03 +0200 | [diff] [blame] | 477 | se_need_remote_conn(appctx->sedesc); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 478 | applet_have_more_data(appctx); |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 479 | return; |
| 480 | } |
| 481 | |
| 482 | HA_SPIN_LOCK(SFT_LOCK, &sft->lock); |
| 483 | if (appctx != sft->appctx) { |
| 484 | HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock); |
| 485 | goto close; |
| 486 | } |
| 487 | ofs = sft->ofs; |
| 488 | |
| 489 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
| 490 | LIST_DEL_INIT(&appctx->wait_entry); |
| 491 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 492 | |
| 493 | HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock); |
| 494 | |
| 495 | /* explanation for the initialization below: it would be better to do |
| 496 | * this in the parsing function but this would occasionally result in |
| 497 | * dropped events because we'd take a reference on the oldest message |
| 498 | * and keep it while being scheduled. Thus instead let's take it the |
| 499 | * first time we enter here so that we have a chance to pass many |
| 500 | * existing messages before grabbing a reference to a location. This |
| 501 | * value cannot be produced after initialization. |
| 502 | */ |
| 503 | if (unlikely(ofs == ~0)) { |
| 504 | ofs = 0; |
| 505 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 506 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 507 | ofs += ring->ofs; |
| 508 | } |
| 509 | |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 510 | /* in this loop, ofs always points to the counter byte that precedes |
| 511 | * the message so that we can take our reference there if we have to |
| 512 | * stop before the end (ret=0). |
| 513 | */ |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 514 | if (sc_opposite(sc)->state == SC_ST_EST) { |
Emeric Brun | fdabf49 | 2020-12-02 17:02:09 +0100 | [diff] [blame] | 515 | /* we were already there, adjust the offset to be relative to |
| 516 | * the buffer's head and remove us from the counter. |
| 517 | */ |
| 518 | ofs -= ring->ofs; |
| 519 | BUG_ON(ofs >= buf->size); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 520 | HA_ATOMIC_DEC(b_peek(buf, ofs)); |
Emeric Brun | fdabf49 | 2020-12-02 17:02:09 +0100 | [diff] [blame] | 521 | |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 522 | ret = 1; |
| 523 | while (ofs + 1 < b_data(buf)) { |
| 524 | cnt = 1; |
| 525 | len = b_peek_varint(buf, ofs + cnt, &msg_len); |
| 526 | if (!len) |
| 527 | break; |
| 528 | cnt += len; |
| 529 | BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf)); |
| 530 | |
| 531 | chunk_reset(&trash); |
| 532 | p = ulltoa(msg_len, trash.area, b_size(&trash)); |
| 533 | if (p) { |
| 534 | trash.data = (p - trash.area) + 1; |
| 535 | *p = ' '; |
| 536 | } |
| 537 | |
| 538 | if (!p || (trash.data + msg_len > b_size(&trash))) { |
| 539 | /* too large a message to ever fit, let's skip it */ |
| 540 | ofs += cnt + msg_len; |
| 541 | continue; |
| 542 | } |
| 543 | |
| 544 | trash.data += b_getblk(buf, p + 1, msg_len, ofs + cnt); |
| 545 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 546 | if (applet_putchk(appctx, &trash) == -1) { |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 547 | ret = 0; |
| 548 | break; |
| 549 | } |
| 550 | ofs += cnt + msg_len; |
| 551 | } |
| 552 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 553 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 554 | ofs += ring->ofs; |
| 555 | sft->ofs = ofs; |
| 556 | } |
| 557 | HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock); |
| 558 | |
| 559 | if (ret) { |
| 560 | /* let's be woken up once new data arrive */ |
| 561 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 562 | LIST_APPEND(&ring->waiters, &appctx->wait_entry); |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 563 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 564 | applet_have_no_more_data(appctx); |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 565 | } |
| 566 | HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock); |
| 567 | |
| 568 | /* always drain data from server */ |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 569 | co_skip(sc_oc(sc), sc_oc(sc)->output); |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 570 | return; |
| 571 | |
| 572 | close: |
Willy Tarreau | 0eca539 | 2022-05-27 10:44:25 +0200 | [diff] [blame] | 573 | sc_shutw(sc); |
| 574 | sc_shutr(sc); |
| 575 | sc_ic(sc)->flags |= CF_READ_NULL; |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 576 | } |
| 577 | |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 578 | void __sink_forward_session_deinit(struct sink_forward_target *sft) |
| 579 | { |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 580 | struct stream *s = appctx_strm(sft->appctx); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 581 | struct sink *sink; |
| 582 | |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 583 | sink = strm_fe(s)->parent; |
| 584 | if (!sink) |
| 585 | return; |
| 586 | |
| 587 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &sink->ctx.ring->lock); |
| 588 | LIST_DEL_INIT(&sft->appctx->wait_entry); |
| 589 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &sink->ctx.ring->lock); |
| 590 | |
| 591 | sft->appctx = NULL; |
| 592 | task_wakeup(sink->forward_task, TASK_WOKEN_MSG); |
| 593 | } |
| 594 | |
Christopher Faulet | aee57fc | 2022-05-12 15:34:48 +0200 | [diff] [blame] | 595 | static int sink_forward_session_init(struct appctx *appctx) |
| 596 | { |
| 597 | struct sink_forward_target *sft = appctx->svcctx; |
| 598 | struct stream *s; |
| 599 | struct sockaddr_storage *addr = NULL; |
| 600 | |
| 601 | if (!sockaddr_alloc(&addr, &sft->srv->addr, sizeof(sft->srv->addr))) |
| 602 | goto out_error; |
| 603 | |
| 604 | if (appctx_finalize_startup(appctx, sft->sink->forward_px, &BUF_NULL) == -1) |
| 605 | goto out_free_addr; |
| 606 | |
| 607 | s = appctx_strm(appctx); |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 608 | s->scb->dst = addr; |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 609 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | aee57fc | 2022-05-12 15:34:48 +0200 | [diff] [blame] | 610 | |
| 611 | s->target = &sft->srv->obj_type; |
| 612 | s->flags = SF_ASSIGNED; |
| 613 | |
| 614 | s->do_log = NULL; |
| 615 | s->uniq_id = 0; |
| 616 | |
| 617 | s->res.flags |= CF_READ_DONTWAIT; |
| 618 | /* for rto and rex to eternity to not expire on idle recv: |
| 619 | * We are using a syslog server. |
| 620 | */ |
| 621 | s->res.rto = TICK_ETERNITY; |
| 622 | s->res.rex = TICK_ETERNITY; |
| 623 | sft->appctx = appctx; |
| 624 | |
| 625 | return 0; |
| 626 | |
| 627 | out_free_addr: |
| 628 | sockaddr_free(&addr); |
| 629 | out_error: |
| 630 | return -1; |
| 631 | } |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 632 | |
| 633 | static void sink_forward_session_release(struct appctx *appctx) |
| 634 | { |
Willy Tarreau | 42cc831 | 2022-05-04 20:42:23 +0200 | [diff] [blame] | 635 | struct sink_forward_target *sft = appctx->svcctx; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 636 | |
| 637 | if (!sft) |
| 638 | return; |
| 639 | |
| 640 | HA_SPIN_LOCK(SFT_LOCK, &sft->lock); |
| 641 | if (sft->appctx == appctx) |
| 642 | __sink_forward_session_deinit(sft); |
| 643 | HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock); |
| 644 | } |
| 645 | |
| 646 | static struct applet sink_forward_applet = { |
| 647 | .obj_type = OBJ_TYPE_APPLET, |
| 648 | .name = "<SINKFWD>", /* used for logging */ |
| 649 | .fct = sink_forward_io_handler, |
Christopher Faulet | aee57fc | 2022-05-12 15:34:48 +0200 | [diff] [blame] | 650 | .init = sink_forward_session_init, |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 651 | .release = sink_forward_session_release, |
| 652 | }; |
| 653 | |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 654 | static struct applet sink_forward_oc_applet = { |
| 655 | .obj_type = OBJ_TYPE_APPLET, |
| 656 | .name = "<SINKFWDOC>", /* used for logging */ |
| 657 | .fct = sink_forward_oc_io_handler, |
Christopher Faulet | aee57fc | 2022-05-12 15:34:48 +0200 | [diff] [blame] | 658 | .init = sink_forward_session_init, |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 659 | .release = sink_forward_session_release, |
| 660 | }; |
| 661 | |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 662 | /* |
| 663 | * Create a new peer session in assigned state (connect will start automatically) |
Willy Tarreau | 42cc831 | 2022-05-04 20:42:23 +0200 | [diff] [blame] | 664 | * It sets its context into appctx->svcctx. |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 665 | */ |
| 666 | static struct appctx *sink_forward_session_create(struct sink *sink, struct sink_forward_target *sft) |
| 667 | { |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 668 | struct appctx *appctx; |
Emeric Brun | 9755647 | 2020-05-30 01:42:45 +0200 | [diff] [blame] | 669 | struct applet *applet = &sink_forward_applet; |
| 670 | |
| 671 | if (sft->srv->log_proto == SRV_LOG_PROTO_OCTET_COUNTING) |
| 672 | applet = &sink_forward_oc_applet; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 673 | |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 674 | appctx = appctx_new_here(applet, NULL); |
Christopher Faulet | 2479e5f | 2022-01-19 14:50:11 +0100 | [diff] [blame] | 675 | if (!appctx) |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 676 | goto out_close; |
Willy Tarreau | 42cc831 | 2022-05-04 20:42:23 +0200 | [diff] [blame] | 677 | appctx->svcctx = (void *)sft; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 678 | |
Christopher Faulet | aee57fc | 2022-05-12 15:34:48 +0200 | [diff] [blame] | 679 | if (appctx_init(appctx) == -1) |
Christopher Faulet | 92202da | 2022-05-11 12:22:10 +0200 | [diff] [blame] | 680 | goto out_free_appctx; |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 681 | |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 682 | return appctx; |
| 683 | |
| 684 | /* Error unrolling */ |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 685 | out_free_appctx: |
Christopher Faulet | aee57fc | 2022-05-12 15:34:48 +0200 | [diff] [blame] | 686 | appctx_free_on_early_error(appctx); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 687 | out_close: |
| 688 | return NULL; |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * Task to handle connctions to forward servers |
| 693 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 694 | 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] | 695 | { |
| 696 | struct sink *sink = (struct sink *)context; |
| 697 | struct sink_forward_target *sft = sink->sft; |
| 698 | |
| 699 | task->expire = TICK_ETERNITY; |
| 700 | |
| 701 | if (!stopping) { |
| 702 | while (sft) { |
| 703 | HA_SPIN_LOCK(SFT_LOCK, &sft->lock); |
| 704 | /* if appctx is NULL, start a new session */ |
| 705 | if (!sft->appctx) |
| 706 | sft->appctx = sink_forward_session_create(sink, sft); |
| 707 | HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock); |
| 708 | sft = sft->next; |
| 709 | } |
| 710 | } |
| 711 | else { |
| 712 | while (sft) { |
| 713 | HA_SPIN_LOCK(SFT_LOCK, &sft->lock); |
| 714 | /* awake applet to perform a clean close */ |
| 715 | if (sft->appctx) |
| 716 | appctx_wakeup(sft->appctx); |
| 717 | HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock); |
| 718 | sft = sft->next; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | return task; |
| 723 | } |
| 724 | /* |
| 725 | * Init task to manage connctions to forward servers |
| 726 | * |
| 727 | * returns 0 in case of error. |
| 728 | */ |
| 729 | int sink_init_forward(struct sink *sink) |
| 730 | { |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 731 | sink->forward_task = task_new_anywhere(); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 732 | if (!sink->forward_task) |
| 733 | return 0; |
| 734 | |
| 735 | sink->forward_task->process = process_sink_forward; |
| 736 | sink->forward_task->context = (void *)sink; |
| 737 | sink->forward_sighandler = signal_register_task(0, sink->forward_task, 0); |
| 738 | task_wakeup(sink->forward_task, TASK_WOKEN_INIT); |
| 739 | return 1; |
| 740 | } |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 741 | /* |
| 742 | * Parse "ring" section and create corresponding sink buffer. |
| 743 | * |
| 744 | * The function returns 0 in success case, otherwise, it returns error |
| 745 | * flags. |
| 746 | */ |
| 747 | int cfg_parse_ring(const char *file, int linenum, char **args, int kwm) |
| 748 | { |
| 749 | int err_code = 0; |
| 750 | const char *inv; |
| 751 | size_t size = BUFSIZE; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 752 | struct proxy *p; |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 753 | |
| 754 | if (strcmp(args[0], "ring") == 0) { /* new peers section */ |
| 755 | if (!*args[1]) { |
| 756 | ha_alert("parsing [%s:%d] : missing ring name.\n", file, linenum); |
| 757 | err_code |= ERR_ALERT | ERR_FATAL; |
| 758 | goto err; |
| 759 | } |
| 760 | |
| 761 | inv = invalid_char(args[1]); |
| 762 | if (inv) { |
| 763 | ha_alert("parsing [%s:%d] : invalid ring name '%s' (character '%c' is not permitted).\n", file, linenum, args[1], *inv); |
| 764 | err_code |= ERR_ALERT | ERR_FATAL; |
| 765 | goto err; |
| 766 | } |
| 767 | |
| 768 | if (sink_find(args[1])) { |
| 769 | ha_alert("parsing [%s:%d] : sink named '%s' already exists.\n", file, linenum, args[1]); |
| 770 | err_code |= ERR_ALERT | ERR_FATAL; |
| 771 | goto err; |
| 772 | } |
| 773 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 774 | 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] | 775 | if (!cfg_sink || cfg_sink->type != SINK_TYPE_BUFFER) { |
| 776 | ha_alert("parsing [%s:%d] : unable to create a new sink buffer for ring '%s'.\n", file, linenum, args[1]); |
| 777 | err_code |= ERR_ALERT | ERR_FATAL; |
| 778 | goto err; |
| 779 | } |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 780 | |
| 781 | /* allocate new proxy to handle forwards */ |
| 782 | p = calloc(1, sizeof *p); |
| 783 | if (!p) { |
| 784 | ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 785 | err_code |= ERR_ALERT | ERR_FATAL; |
| 786 | goto err; |
| 787 | } |
| 788 | |
| 789 | init_new_proxy(p); |
| 790 | sink_setup_proxy(p); |
| 791 | p->parent = cfg_sink; |
| 792 | p->id = strdup(args[1]); |
| 793 | p->conf.args.file = p->conf.file = strdup(file); |
| 794 | p->conf.args.line = p->conf.line = linenum; |
| 795 | cfg_sink->forward_px = p; |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 796 | } |
| 797 | else if (strcmp(args[0], "size") == 0) { |
| 798 | size = atol(args[1]); |
| 799 | if (!size) { |
| 800 | ha_alert("parsing [%s:%d] : invalid size '%s' for new sink buffer.\n", file, linenum, args[1]); |
| 801 | err_code |= ERR_ALERT | ERR_FATAL; |
| 802 | goto err; |
| 803 | } |
| 804 | |
| 805 | if (!cfg_sink || (cfg_sink->type != SINK_TYPE_BUFFER) |
| 806 | || !ring_resize(cfg_sink->ctx.ring, size)) { |
| 807 | ha_alert("parsing [%s:%d] : fail to set sink buffer size '%s'.\n", file, linenum, args[1]); |
| 808 | err_code |= ERR_ALERT | ERR_FATAL; |
| 809 | goto err; |
| 810 | } |
| 811 | } |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 812 | else if (strcmp(args[0],"server") == 0) { |
Amaury Denoyelle | 30c0537 | 2021-03-08 16:36:46 +0100 | [diff] [blame] | 813 | err_code |= parse_server(file, linenum, args, cfg_sink->forward_px, NULL, |
| 814 | SRV_PARSE_PARSE_ADDR|SRV_PARSE_INITIAL_RESOLVE); |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 815 | } |
| 816 | else if (strcmp(args[0],"timeout") == 0) { |
| 817 | if (!cfg_sink || !cfg_sink->forward_px) { |
| 818 | ha_alert("parsing [%s:%d] : unable to set timeout '%s'.\n", file, linenum, args[1]); |
| 819 | err_code |= ERR_ALERT | ERR_FATAL; |
| 820 | goto err; |
| 821 | } |
| 822 | |
| 823 | if (strcmp(args[1], "connect") == 0 || |
| 824 | strcmp(args[1], "server") == 0) { |
| 825 | const char *res; |
| 826 | unsigned int tout; |
| 827 | |
| 828 | if (!*args[2]) { |
| 829 | ha_alert("parsing [%s:%d] : '%s %s' expects <time> as argument.\n", |
| 830 | file, linenum, args[0], args[1]); |
| 831 | err_code |= ERR_ALERT | ERR_FATAL; |
| 832 | goto err; |
| 833 | } |
| 834 | res = parse_time_err(args[2], &tout, TIME_UNIT_MS); |
| 835 | if (res == PARSE_TIME_OVER) { |
| 836 | ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n", |
| 837 | file, linenum, args[2], args[0], args[1]); |
| 838 | err_code |= ERR_ALERT | ERR_FATAL; |
| 839 | goto err; |
| 840 | } |
| 841 | else if (res == PARSE_TIME_UNDER) { |
| 842 | ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n", |
| 843 | file, linenum, args[2], args[0], args[1]); |
| 844 | err_code |= ERR_ALERT | ERR_FATAL; |
| 845 | goto err; |
| 846 | } |
| 847 | else if (res) { |
| 848 | ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s %s>.\n", |
| 849 | file, linenum, *res, args[0], args[1]); |
| 850 | err_code |= ERR_ALERT | ERR_FATAL; |
| 851 | goto err; |
| 852 | } |
| 853 | if (args[1][2] == 'c') |
| 854 | cfg_sink->forward_px->timeout.connect = tout; |
| 855 | else |
| 856 | cfg_sink->forward_px->timeout.server = tout; |
| 857 | } |
| 858 | } |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 859 | else if (strcmp(args[0],"format") == 0) { |
| 860 | if (!cfg_sink) { |
| 861 | ha_alert("parsing [%s:%d] : unable to set format '%s'.\n", file, linenum, args[1]); |
| 862 | err_code |= ERR_ALERT | ERR_FATAL; |
| 863 | goto err; |
| 864 | } |
| 865 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 866 | cfg_sink->fmt = get_log_format(args[1]); |
| 867 | if (cfg_sink->fmt == LOG_FORMAT_UNSPEC) { |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 868 | ha_alert("parsing [%s:%d] : unknown format '%s'.\n", file, linenum, args[1]); |
| 869 | err_code |= ERR_ALERT | ERR_FATAL; |
| 870 | goto err; |
| 871 | } |
| 872 | } |
| 873 | else if (strcmp(args[0],"maxlen") == 0) { |
| 874 | if (!cfg_sink) { |
| 875 | ha_alert("parsing [%s:%d] : unable to set event max length '%s'.\n", file, linenum, args[1]); |
| 876 | err_code |= ERR_ALERT | ERR_FATAL; |
| 877 | goto err; |
| 878 | } |
| 879 | |
| 880 | cfg_sink->maxlen = atol(args[1]); |
| 881 | if (!cfg_sink->maxlen) { |
| 882 | ha_alert("parsing [%s:%d] : invalid size '%s' for new sink buffer.\n", file, linenum, args[1]); |
| 883 | err_code |= ERR_ALERT | ERR_FATAL; |
| 884 | goto err; |
| 885 | } |
| 886 | } |
| 887 | else if (strcmp(args[0],"description") == 0) { |
| 888 | if (!cfg_sink) { |
| 889 | ha_alert("parsing [%s:%d] : unable to set description '%s'.\n", file, linenum, args[1]); |
| 890 | err_code |= ERR_ALERT | ERR_FATAL; |
| 891 | goto err; |
| 892 | } |
| 893 | |
| 894 | if (!*args[1]) { |
| 895 | ha_alert("parsing [%s:%d] : missing ring description text.\n", file, linenum); |
| 896 | err_code |= ERR_ALERT | ERR_FATAL; |
| 897 | goto err; |
| 898 | } |
| 899 | |
| 900 | free(cfg_sink->desc); |
| 901 | |
| 902 | cfg_sink->desc = strdup(args[1]); |
| 903 | if (!cfg_sink->desc) { |
| 904 | ha_alert("parsing [%s:%d] : fail to set description '%s'.\n", file, linenum, args[1]); |
| 905 | err_code |= ERR_ALERT | ERR_FATAL; |
| 906 | goto err; |
| 907 | } |
| 908 | } |
Emeric Brun | 9f2ff3a | 2020-05-29 15:47:52 +0200 | [diff] [blame] | 909 | else { |
| 910 | ha_alert("parsing [%s:%d] : unknown statement '%s'.\n", file, linenum, args[0]); |
| 911 | err_code |= ERR_ALERT | ERR_FATAL; |
| 912 | goto err; |
| 913 | } |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 914 | |
| 915 | err: |
| 916 | return err_code; |
| 917 | } |
| 918 | |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 919 | /* Creates an new sink buffer from a log server. |
| 920 | * |
| 921 | * It uses the logsrvaddress to declare a forward |
| 922 | * server for this buffer. And it initializes the |
| 923 | * forwarding. |
| 924 | * |
| 925 | * The function returns a pointer on the |
| 926 | * allocated struct sink if allocate |
| 927 | * and initialize succeed, else if it fails |
| 928 | * it returns NULL. |
| 929 | * |
| 930 | * Note: the sink is created using the name |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 931 | * specified into logsrv->ring_name |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 932 | */ |
| 933 | struct sink *sink_new_from_logsrv(struct logsrv *logsrv) |
| 934 | { |
| 935 | struct proxy *p = NULL; |
| 936 | struct sink *sink = NULL; |
| 937 | struct server *srv = NULL; |
| 938 | struct sink_forward_target *sft = NULL; |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 939 | |
| 940 | /* allocate new proxy to handle |
| 941 | * forward to a stream server |
| 942 | */ |
| 943 | p = calloc(1, sizeof *p); |
| 944 | if (!p) { |
| 945 | goto error; |
| 946 | } |
| 947 | |
| 948 | init_new_proxy(p); |
| 949 | sink_setup_proxy(p); |
| 950 | p->id = strdup(logsrv->ring_name); |
| 951 | p->conf.args.file = p->conf.file = strdup(logsrv->conf.file); |
| 952 | p->conf.args.line = p->conf.line = logsrv->conf.line; |
| 953 | |
| 954 | /* allocate a new server to forward messages |
| 955 | * from ring buffer |
| 956 | */ |
| 957 | srv = new_server(p); |
| 958 | if (!srv) |
| 959 | goto error; |
| 960 | |
| 961 | /* init server */ |
| 962 | srv->id = strdup(logsrv->ring_name); |
| 963 | srv->conf.file = strdup(logsrv->conf.file); |
| 964 | srv->conf.line = logsrv->conf.line; |
| 965 | srv->addr = logsrv->addr; |
| 966 | srv->svc_port = get_host_port(&logsrv->addr); |
| 967 | HA_SPIN_INIT(&srv->lock); |
| 968 | |
| 969 | /* process per thread init */ |
Miroslav Zagorac | 8a8f270 | 2021-06-15 15:33:20 +0200 | [diff] [blame] | 970 | if (srv_init_per_thr(srv) == -1) |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 971 | goto error; |
| 972 | |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 973 | /* the servers are linked backwards |
| 974 | * first into proxy |
| 975 | */ |
| 976 | p->srv = srv; |
| 977 | srv->next = p->srv; |
| 978 | |
| 979 | /* allocate sink_forward_target descriptor */ |
| 980 | sft = calloc(1, sizeof(*sft)); |
| 981 | if (!sft) |
| 982 | goto error; |
| 983 | |
| 984 | /* init sink_forward_target offset */ |
| 985 | sft->srv = srv; |
| 986 | sft->appctx = NULL; |
| 987 | sft->ofs = ~0; |
| 988 | HA_SPIN_INIT(&sft->lock); |
| 989 | |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 990 | /* prepare description for the sink */ |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 991 | chunk_reset(&trash); |
| 992 | chunk_printf(&trash, "created from logserver declared into '%s' at line %d", logsrv->conf.file, logsrv->conf.line); |
| 993 | |
| 994 | /* allocate a new sink buffer */ |
| 995 | sink = sink_new_buf(logsrv->ring_name, trash.area, logsrv->format, BUFSIZE); |
| 996 | if (!sink || sink->type != SINK_TYPE_BUFFER) { |
| 997 | goto error; |
| 998 | } |
| 999 | |
| 1000 | /* link sink_forward_target to proxy */ |
| 1001 | sink->forward_px = p; |
| 1002 | p->parent = sink; |
| 1003 | |
| 1004 | /* insert into sink_forward_targets |
| 1005 | * list into sink |
| 1006 | */ |
Christopher Faulet | 2ae25ea | 2022-05-12 14:50:09 +0200 | [diff] [blame] | 1007 | sft->sink = sink; |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 1008 | sft->next = sink->sft; |
| 1009 | sink->sft = sft; |
| 1010 | |
| 1011 | /* mark server as an attached reader to the ring */ |
| 1012 | if (!ring_attach(sink->ctx.ring)) { |
| 1013 | /* should never fail since there is |
| 1014 | * only one reader |
| 1015 | */ |
| 1016 | goto error; |
| 1017 | } |
| 1018 | |
| 1019 | /* initialize sink buffer forwarding */ |
| 1020 | if (!sink_init_forward(sink)) |
| 1021 | goto error; |
| 1022 | |
| 1023 | /* reset familyt of logsrv to consider the ring buffer target */ |
| 1024 | logsrv->addr.ss_family = AF_UNSPEC; |
| 1025 | |
| 1026 | return sink; |
| 1027 | error: |
| 1028 | if (p) { |
| 1029 | if (p->id) |
| 1030 | free(p->id); |
| 1031 | if (p->conf.file) |
| 1032 | free(p->conf.file); |
| 1033 | |
| 1034 | free(p); |
| 1035 | } |
| 1036 | |
| 1037 | if (srv) { |
| 1038 | if (srv->id) |
| 1039 | free(srv->id); |
| 1040 | if (srv->conf.file) |
| 1041 | free((void *)srv->conf.file); |
| 1042 | if (srv->per_thr) |
| 1043 | free(srv->per_thr); |
| 1044 | free(srv); |
| 1045 | } |
| 1046 | |
| 1047 | if (sft) |
| 1048 | free(sft); |
| 1049 | |
| 1050 | if (sink) { |
| 1051 | if (sink->ctx.ring) |
| 1052 | ring_free(sink->ctx.ring); |
| 1053 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1054 | LIST_DELETE(&sink->sink_list); |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 1055 | free(sink->name); |
| 1056 | free(sink->desc); |
| 1057 | free(sink); |
| 1058 | } |
| 1059 | |
| 1060 | return NULL; |
| 1061 | } |
| 1062 | |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1063 | /* |
| 1064 | * Post parsing "ring" section. |
| 1065 | * |
| 1066 | * The function returns 0 in success case, otherwise, it returns error |
| 1067 | * flags. |
| 1068 | */ |
| 1069 | int cfg_post_parse_ring() |
| 1070 | { |
| 1071 | int err_code = 0; |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 1072 | struct server *srv; |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1073 | |
| 1074 | if (cfg_sink && (cfg_sink->type == SINK_TYPE_BUFFER)) { |
| 1075 | if (cfg_sink->maxlen > b_size(&cfg_sink->ctx.ring->buf)) { |
| 1076 | 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] | 1077 | 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] | 1078 | cfg_sink->maxlen = b_size(&cfg_sink->ctx.ring->buf); |
| 1079 | err_code |= ERR_ALERT; |
| 1080 | } |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 1081 | |
| 1082 | /* prepare forward server descriptors */ |
| 1083 | if (cfg_sink->forward_px) { |
| 1084 | srv = cfg_sink->forward_px->srv; |
| 1085 | while (srv) { |
| 1086 | struct sink_forward_target *sft; |
| 1087 | /* init ssl if needed */ |
| 1088 | if (srv->use_ssl == 1 && xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) { |
| 1089 | if (xprt_get(XPRT_SSL)->prepare_srv(srv)) { |
| 1090 | ha_alert("unable to prepare SSL for server '%s' in ring '%s'.\n", srv->id, cfg_sink->name); |
| 1091 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1092 | } |
| 1093 | } |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1094 | |
Emeric Brun | 494c505 | 2020-05-28 11:13:15 +0200 | [diff] [blame] | 1095 | /* allocate sink_forward_target descriptor */ |
| 1096 | sft = calloc(1, sizeof(*sft)); |
| 1097 | if (!sft) { |
| 1098 | ha_alert("memory allocation error initializing server '%s' in ring '%s'.\n",srv->id, cfg_sink->name); |
| 1099 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1100 | break; |
| 1101 | } |
| 1102 | sft->srv = srv; |
| 1103 | sft->appctx = NULL; |
| 1104 | sft->ofs = ~0; /* init ring offset */ |
| 1105 | sft->next = cfg_sink->sft; |
| 1106 | HA_SPIN_INIT(&sft->lock); |
| 1107 | |
| 1108 | /* mark server attached to the ring */ |
| 1109 | if (!ring_attach(cfg_sink->ctx.ring)) { |
| 1110 | ha_alert("server '%s' sets too many watchers > 255 on ring '%s'.\n", srv->id, cfg_sink->name); |
| 1111 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1112 | } |
| 1113 | cfg_sink->sft = sft; |
| 1114 | srv = srv->next; |
| 1115 | } |
| 1116 | sink_init_forward(cfg_sink); |
| 1117 | } |
| 1118 | } |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1119 | cfg_sink = NULL; |
| 1120 | |
| 1121 | return err_code; |
| 1122 | } |
| 1123 | |
| 1124 | /* resolve sink names at end of config. Returns 0 on success otherwise error |
| 1125 | * flags. |
| 1126 | */ |
| 1127 | int post_sink_resolve() |
| 1128 | { |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1129 | int err_code = ERR_NONE; |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1130 | struct logsrv *logsrv, *logb; |
| 1131 | struct sink *sink; |
| 1132 | struct proxy *px; |
| 1133 | |
| 1134 | list_for_each_entry_safe(logsrv, logb, &global.logsrvs, list) { |
| 1135 | if (logsrv->type == LOG_TARGET_BUFFER) { |
| 1136 | sink = sink_find(logsrv->ring_name); |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 1137 | if (!sink) { |
| 1138 | /* LOG_TARGET_BUFFER but !AF_UNSPEC |
| 1139 | * means we must allocate a sink |
| 1140 | * buffer to send messages to this logsrv |
| 1141 | */ |
| 1142 | if (logsrv->addr.ss_family != AF_UNSPEC) { |
| 1143 | sink = sink_new_from_logsrv(logsrv); |
| 1144 | if (!sink) { |
| 1145 | ha_alert("global stream log server declared in file '%s' at line %d cannot be initialized'.\n", |
| 1146 | logsrv->conf.file, logsrv->conf.line); |
| 1147 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1148 | } |
| 1149 | } |
| 1150 | else { |
| 1151 | ha_alert("global log server declared in file '%s' at line %d uses unknown ring named '%s'.\n", |
| 1152 | logsrv->conf.file, logsrv->conf.line, logsrv->ring_name); |
| 1153 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1154 | } |
| 1155 | } |
| 1156 | else if (sink->type != SINK_TYPE_BUFFER) { |
| 1157 | ha_alert("global log server declared in file '%s' at line %d uses incompatible ring '%s'.\n", |
| 1158 | logsrv->conf.file, logsrv->conf.line, logsrv->ring_name); |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1159 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1160 | } |
| 1161 | logsrv->sink = sink; |
| 1162 | } |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 1163 | |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | for (px = proxies_list; px; px = px->next) { |
| 1167 | list_for_each_entry_safe(logsrv, logb, &px->logsrvs, list) { |
| 1168 | if (logsrv->type == LOG_TARGET_BUFFER) { |
| 1169 | sink = sink_find(logsrv->ring_name); |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 1170 | if (!sink) { |
| 1171 | /* LOG_TARGET_BUFFER but !AF_UNSPEC |
| 1172 | * means we must allocate a sink |
| 1173 | * buffer to send messages to this logsrv |
| 1174 | */ |
| 1175 | if (logsrv->addr.ss_family != AF_UNSPEC) { |
| 1176 | sink = sink_new_from_logsrv(logsrv); |
| 1177 | if (!sink) { |
| 1178 | ha_alert("log server declared in proxy section '%s' file '%s' at line %d cannot be initialized'.\n", |
| 1179 | px->id, logsrv->conf.file, logsrv->conf.line); |
| 1180 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1181 | } |
| 1182 | } |
| 1183 | else { |
| 1184 | ha_alert("log server declared in proxy section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n", |
| 1185 | px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name); |
| 1186 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1187 | } |
| 1188 | } |
| 1189 | else if (sink->type != SINK_TYPE_BUFFER) { |
| 1190 | ha_alert("log server declared in proxy section '%s' in file '%s' at line %d uses incomatible ring named '%s'.\n", |
| 1191 | px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name); |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1192 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1193 | } |
| 1194 | logsrv->sink = sink; |
| 1195 | } |
| 1196 | } |
| 1197 | } |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 1198 | |
| 1199 | for (px = cfg_log_forward; px; px = px->next) { |
| 1200 | list_for_each_entry_safe(logsrv, logb, &px->logsrvs, list) { |
| 1201 | if (logsrv->type == LOG_TARGET_BUFFER) { |
| 1202 | sink = sink_find(logsrv->ring_name); |
Emeric Brun | 94aab06 | 2021-04-02 10:41:36 +0200 | [diff] [blame] | 1203 | if (!sink) { |
| 1204 | /* LOG_TARGET_BUFFER but !AF_UNSPEC |
| 1205 | * means we must allocate a sink |
| 1206 | * buffer to send messages to this logsrv |
| 1207 | */ |
| 1208 | if (logsrv->addr.ss_family != AF_UNSPEC) { |
| 1209 | sink = sink_new_from_logsrv(logsrv); |
| 1210 | if (!sink) { |
| 1211 | ha_alert("log server declared in log-forward section '%s' file '%s' at line %d cannot be initialized'.\n", |
| 1212 | px->id, logsrv->conf.file, logsrv->conf.line); |
| 1213 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1214 | } |
| 1215 | } |
| 1216 | else { |
| 1217 | ha_alert("log server declared in log-forward section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n", |
| 1218 | px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name); |
| 1219 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1220 | } |
| 1221 | } |
| 1222 | else if (sink->type != SINK_TYPE_BUFFER) { |
| 1223 | ha_alert("log server declared in log-forward section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n", |
| 1224 | px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name); |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 1225 | err_code |= ERR_ALERT | ERR_FATAL; |
| 1226 | } |
| 1227 | logsrv->sink = sink; |
| 1228 | } |
| 1229 | } |
| 1230 | } |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1231 | return err_code; |
| 1232 | } |
| 1233 | |
| 1234 | |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 1235 | static void sink_init() |
| 1236 | { |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1237 | sink_new_fd("stdout", "standard output (fd#1)", LOG_FORMAT_RAW, 1); |
| 1238 | sink_new_fd("stderr", "standard output (fd#2)", LOG_FORMAT_RAW, 2); |
| 1239 | sink_new_buf("buf0", "in-memory ring buffer", LOG_FORMAT_TIMED, 1048576); |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | static void sink_deinit() |
| 1243 | { |
| 1244 | struct sink *sink, *sb; |
| 1245 | |
| 1246 | list_for_each_entry_safe(sink, sb, &sink_list, sink_list) { |
| 1247 | if (sink->type == SINK_TYPE_BUFFER) |
| 1248 | ring_free(sink->ctx.ring); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1249 | LIST_DELETE(&sink->sink_list); |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1250 | free(sink->name); |
| 1251 | free(sink->desc); |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 1252 | free(sink); |
| 1253 | } |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | INITCALL0(STG_REGISTER, sink_init); |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 1257 | REGISTER_POST_DEINIT(sink_deinit); |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 1258 | |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 1259 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 1260 | { { "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] | 1261 | {{},} |
| 1262 | }}; |
| 1263 | |
| 1264 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 1265 | |
Emeric Brun | 99c453d | 2020-05-25 15:01:04 +0200 | [diff] [blame] | 1266 | /* config parsers for this section */ |
| 1267 | REGISTER_CONFIG_SECTION("ring", cfg_parse_ring, cfg_post_parse_ring); |
| 1268 | REGISTER_POST_CHECK(post_sink_resolve); |
| 1269 | |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 1270 | /* |
| 1271 | * Local variables: |
| 1272 | * c-indent-level: 8 |
| 1273 | * c-basic-offset: 8 |
| 1274 | * End: |
| 1275 | */ |