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