blob: ffb6cf1f97912c74527b887cfbf57cfc38e766a1 [file] [log] [blame]
Willy Tarreau67b5a162019-08-11 16:38:56 +02001/*
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 Tarreau0b8e9ce2022-08-11 16:38:20 +020021#include <sys/mman.h>
22#include <errno.h>
23#include <fcntl.h>
24
Willy Tarreau36979d92020-06-05 17:27:29 +020025#include <import/ist.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020026#include <haproxy/api.h>
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +020027#include <haproxy/applet.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020028#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020029#include <haproxy/cli.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020030#include <haproxy/errors.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020031#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020032#include <haproxy/log.h>
Willy Tarreau817538e2021-05-08 20:20:21 +020033#include <haproxy/proxy.h>
Willy Tarreaud2ad57c2020-06-03 19:43:35 +020034#include <haproxy/ring.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020035#include <haproxy/sc_strm.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020036#include <haproxy/signal.h>
Willy Tarreauba2f73d2020-06-03 20:02:28 +020037#include <haproxy/sink.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020038#include <haproxy/stconn.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/time.h>
Willy Tarreau4bad5e22021-05-08 13:05:30 +020040#include <haproxy/tools.h>
Willy Tarreau67b5a162019-08-11 16:38:56 +020041
42struct list sink_list = LIST_HEAD_INIT(sink_list);
43
Emeric Brun99c453d2020-05-25 15:01:04 +020044struct sink *cfg_sink;
45
Willy Tarreau67b5a162019-08-11 16:38:56 +020046struct 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 Brun54648852020-07-06 15:54:06 +020061static struct sink *__sink_new(const char *name, const char *desc, int fmt)
Willy Tarreau67b5a162019-08-11 16:38:56 +020062{
63 struct sink *sink;
64
65 sink = sink_find(name);
66 if (sink)
67 goto end;
68
Emeric Brun494c5052020-05-28 11:13:15 +020069 sink = calloc(1, sizeof(*sink));
Willy Tarreau67b5a162019-08-11 16:38:56 +020070 if (!sink)
71 goto end;
72
Emeric Brun99c453d2020-05-25 15:01:04 +020073 sink->name = strdup(name);
Tim Duesterhusa7ebffe2021-01-03 19:54:11 +010074 if (!sink->name)
75 goto err;
76
Emeric Brun99c453d2020-05-25 15:01:04 +020077 sink->desc = strdup(desc);
Tim Duesterhusa7ebffe2021-01-03 19:54:11 +010078 if (!sink->desc)
79 goto err;
80
Willy Tarreau67b5a162019-08-11 16:38:56 +020081 sink->fmt = fmt;
82 sink->type = SINK_TYPE_NEW;
Christopher Fauleta63a5c22019-11-15 15:10:12 +010083 sink->maxlen = BUFSIZE;
Willy Tarreau67b5a162019-08-11 16:38:56 +020084 /* address will be filled by the caller if needed */
Willy Tarreau973e6622019-08-20 11:57:52 +020085 sink->ctx.fd = -1;
Willy Tarreau67b5a162019-08-11 16:38:56 +020086 sink->ctx.dropped = 0;
87 HA_RWLOCK_INIT(&sink->ctx.lock);
Willy Tarreau2b718102021-04-21 07:32:39 +020088 LIST_APPEND(&sink_list, &sink->sink_list);
Willy Tarreau67b5a162019-08-11 16:38:56 +020089 end:
90 return sink;
Tim Duesterhusa7ebffe2021-01-03 19:54:11 +010091
92 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +010093 ha_free(&sink->name);
94 ha_free(&sink->desc);
95 ha_free(&sink);
Tim Duesterhusa7ebffe2021-01-03 19:54:11 +010096
97 return NULL;
Willy Tarreau67b5a162019-08-11 16:38:56 +020098}
99
Willy Tarreau973e6622019-08-20 11:57:52 +0200100/* 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 Brun54648852020-07-06 15:54:06 +0200104struct sink *sink_new_fd(const char *name, const char *desc, enum log_fmt fmt, int fd)
Willy Tarreau973e6622019-08-20 11:57:52 +0200105{
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 Tarreau4ed23ca2019-08-23 15:47:49 +0200123/* 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 Brun54648852020-07-06 15:54:06 +0200128struct sink *sink_new_buf(const char *name, const char *desc, enum log_fmt fmt, size_t size)
Willy Tarreau4ed23ca2019-08-23 15:47:49 +0200129{
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 Tarreau2b718102021-04-21 07:32:39 +0200150 LIST_DELETE(&sink->sink_list);
Emeric Brun99c453d2020-05-25 15:01:04 +0200151 free(sink->name);
152 free(sink->desc);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +0200153 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 Tarreau67b5a162019-08-11 16:38:56 +0200164/* tries to send <nmsg> message parts (up to 8, ignored above) from message
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500165 * array <msg> to sink <sink>. Formatting according to the sink's preference is
Willy Tarreau8f240232019-08-27 16:41:06 +0200166 * 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 Tarreau67b5a162019-08-11 16:38:56 +0200170 */
Emeric Brun54648852020-07-06 15:54:06 +0200171 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 Tarreaua1426de2019-08-27 14:21:02 +0200175 size_t npfx = 0;
Emeric Brunbd163812020-05-06 14:33:46 +0200176
Emeric Brun54648852020-07-06 15:54:06 +0200177 if (sink->fmt == LOG_FORMAT_RAW)
Emeric Brunbd163812020-05-06 14:33:46 +0200178 goto send;
Willy Tarreau67b5a162019-08-11 16:38:56 +0200179
Emeric Brun54648852020-07-06 15:54:06 +0200180 pfx = build_log_header(sink->fmt, level, facility, metadata, &npfx);
Emeric Brunbd163812020-05-06 14:33:46 +0200181
182send:
Willy Tarreau973e6622019-08-20 11:57:52 +0200183 if (sink->type == SINK_TYPE_FD) {
Willy Tarreau8f240232019-08-27 16:41:06 +0200184 return fd_write_frag_line(sink->ctx.fd, sink->maxlen, pfx, npfx, msg, nmsg, 1);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +0200185 }
186 else if (sink->type == SINK_TYPE_BUFFER) {
Willy Tarreau8f240232019-08-27 16:41:06 +0200187 return ring_write(sink->ctx.ring, sink->maxlen, pfx, npfx, msg, nmsg);
Willy Tarreau973e6622019-08-20 11:57:52 +0200188 }
Willy Tarreau8f240232019-08-27 16:41:06 +0200189 return 0;
190}
Willy Tarreau67b5a162019-08-11 16:38:56 +0200191
Willy Tarreau8f240232019-08-27 16:41:06 +0200192/* 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 Brun54648852020-07-06 15:54:06 +0200197int sink_announce_dropped(struct sink *sink, int facility)
Willy Tarreau8f240232019-08-27 16:41:06 +0200198{
Emeric Brun54648852020-07-06 15:54:06 +0200199 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 Tarreau8f240232019-08-27 16:41:06 +0200202 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 Brunbd163812020-05-06 14:33:46 +0200211
Emeric Brun54648852020-07-06 15:54:06 +0200212 if (!metadata[LOG_META_HOST].len) {
213 if (global.log_send_hostname)
Tim Duesterhus77508502022-03-15 13:11:06 +0100214 metadata[LOG_META_HOST] = ist(global.log_send_hostname);
Emeric Brun54648852020-07-06 15:54:06 +0200215 }
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 Tarreau8f240232019-08-27 16:41:06 +0200230 return 0;
231 /* success! */
232 HA_ATOMIC_SUB(&sink->ctx.dropped, dropped);
233 }
234 return 1;
Willy Tarreau67b5a162019-08-11 16:38:56 +0200235}
236
Willy Tarreau9f830d72019-08-26 18:17:04 +0200237/* parse the "show events" command, returns 1 if a message is returned, otherwise zero */
238static int cli_parse_show_events(char **args, char *payload, struct appctx *appctx, void *private)
239{
240 struct sink *sink;
Willy Tarreaucba88382022-05-05 15:18:57 +0200241 uint ring_flags;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200242 int arg;
Willy Tarreau9f830d72019-08-26 18:17:04 +0200243
244 args++; // make args[1] the 1st arg
245
246 if (!*args[1]) {
247 /* no arg => report the list of supported sink */
Willy Tarreau1d181e42019-08-30 11:17:01 +0200248 chunk_printf(&trash, "Supported events sinks are listed below. Add -w(wait), -n(new). Any key to stop\n");
Willy Tarreau9f830d72019-08-26 18:17:04 +0200249 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 Tarreaucba88382022-05-05 15:18:57 +0200272 ring_flags = 0;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200273 for (arg = 2; *args[arg]; arg++) {
274 if (strcmp(args[arg], "-w") == 0)
Willy Tarreaucba88382022-05-05 15:18:57 +0200275 ring_flags |= RING_WF_WAIT_MODE;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200276 else if (strcmp(args[arg], "-n") == 0)
Willy Tarreaucba88382022-05-05 15:18:57 +0200277 ring_flags |= RING_WF_SEEK_NEW;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200278 else if (strcmp(args[arg], "-nw") == 0 || strcmp(args[arg], "-wn") == 0)
Willy Tarreaucba88382022-05-05 15:18:57 +0200279 ring_flags |= RING_WF_WAIT_MODE | RING_WF_SEEK_NEW;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200280 else
281 return cli_err(appctx, "unknown option");
282 }
Willy Tarreaucba88382022-05-05 15:18:57 +0200283 return ring_attach_cli(sink->ctx.ring, appctx, ring_flags);
Willy Tarreau9f830d72019-08-26 18:17:04 +0200284}
285
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500286/* Pre-configures a ring proxy to emit connections */
Emeric Brun494c5052020-05-28 11:13:15 +0200287void 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 Brun494c5052020-05-28 11:13:15 +0200298}
299
300/*
Willy Tarreau42cc8312022-05-04 20:42:23 +0200301 * IO Handler to handle message push to syslog tcp server.
302 * It takes its context from appctx->svcctx.
Emeric Brun494c5052020-05-28 11:13:15 +0200303 */
304static void sink_forward_io_handler(struct appctx *appctx)
305{
Willy Tarreauc12b3212022-05-27 11:08:15 +0200306 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau0eca5392022-05-27 10:44:25 +0200307 struct stream *s = __sc_strm(sc);
Emeric Brun494c5052020-05-28 11:13:15 +0200308 struct sink *sink = strm_fe(s)->parent;
Willy Tarreau42cc8312022-05-04 20:42:23 +0200309 struct sink_forward_target *sft = appctx->svcctx;
Emeric Brun494c5052020-05-28 11:13:15 +0200310 struct ring *ring = sink->ctx.ring;
311 struct buffer *buf = &ring->buf;
312 uint64_t msg_len;
Willy Tarreau53bfab02022-08-04 17:18:54 +0200313 size_t len, cnt, ofs, last_ofs;
Emeric Brun494c5052020-05-28 11:13:15 +0200314 int ret = 0;
315
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500316 /* if stopping was requested, close immediately */
Emeric Brun494c5052020-05-28 11:13:15 +0200317 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 Tarreau0eca5392022-05-27 10:44:25 +0200324 sc_oc(sc)->rex = TICK_ETERNITY;
Emeric Brun494c5052020-05-28 11:13:15 +0200325 /* rto should not change but it seems the case */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200326 sc_oc(sc)->rto = TICK_ETERNITY;
Emeric Brun494c5052020-05-28 11:13:15 +0200327
328 /* an error was detected */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200329 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Emeric Brun494c5052020-05-28 11:13:15 +0200330 goto close;
331
332 /* con closed by server side */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200333 if ((sc_oc(sc)->flags & CF_SHUTW))
Emeric Brun494c5052020-05-28 11:13:15 +0200334 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 Tarreau0eca5392022-05-27 10:44:25 +0200339 if (sc_opposite(sc)->state < SC_ST_EST) {
Willy Tarreau90e8b452022-05-25 18:21:43 +0200340 applet_need_more_data(appctx);
Willy Tarreaub23edc82022-05-24 16:49:03 +0200341 se_need_remote_conn(appctx->sedesc);
Willy Tarreau4164eb92022-05-25 15:42:03 +0200342 applet_have_more_data(appctx);
Emeric Brun494c5052020-05-28 11:13:15 +0200343 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 Tarreau4781b152021-04-06 13:53:36 +0200370 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun494c5052020-05-28 11:13:15 +0200371 ofs += ring->ofs;
372 }
373
Emeric Brun494c5052020-05-28 11:13:15 +0200374 /* 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 Tarreau0eca5392022-05-27 10:44:25 +0200378 if (sc_opposite(sc)->state == SC_ST_EST) {
Emeric Brunfdabf492020-12-02 17:02:09 +0100379 /* 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 Tarreau4781b152021-04-06 13:53:36 +0200384 HA_ATOMIC_DEC(b_peek(buf, ofs));
Emeric Brunfdabf492020-12-02 17:02:09 +0100385
Emeric Brun494c5052020-05-28 11:13:15 +0200386 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 Tarreaud0a06d52022-05-18 15:07:19 +0200406 if (applet_putchk(appctx, &trash) == -1) {
Emeric Brun494c5052020-05-28 11:13:15 +0200407 ret = 0;
408 break;
409 }
410 ofs += cnt + msg_len;
411 }
412
Willy Tarreau4781b152021-04-06 13:53:36 +0200413 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun494c5052020-05-28 11:13:15 +0200414 ofs += ring->ofs;
415 sft->ofs = ofs;
Willy Tarreau53bfab02022-08-04 17:18:54 +0200416 last_ofs = ring->ofs;
Emeric Brun494c5052020-05-28 11:13:15 +0200417 }
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 Tarreau2b718102021-04-21 07:32:39 +0200423 LIST_APPEND(&ring->waiters, &appctx->wait_entry);
Willy Tarreau53bfab02022-08-04 17:18:54 +0200424 ofs = ring->ofs;
Emeric Brun494c5052020-05-28 11:13:15 +0200425 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau53bfab02022-08-04 17:18:54 +0200426 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 Brun494c5052020-05-28 11:13:15 +0200434 }
435 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
436
437 /* always drain data from server */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200438 co_skip(sc_oc(sc), sc_oc(sc)->output);
Emeric Brun494c5052020-05-28 11:13:15 +0200439 return;
440
441close:
Willy Tarreau0eca5392022-05-27 10:44:25 +0200442 sc_shutw(sc);
443 sc_shutr(sc);
444 sc_ic(sc)->flags |= CF_READ_NULL;
Emeric Brun494c5052020-05-28 11:13:15 +0200445}
446
Emeric Brun97556472020-05-30 01:42:45 +0200447/*
448 * IO Handler to handle message push to syslog tcp server
449 * using octet counting frames
Willy Tarreau42cc8312022-05-04 20:42:23 +0200450 * It takes its context from appctx->svcctx.
Emeric Brun97556472020-05-30 01:42:45 +0200451 */
452static void sink_forward_oc_io_handler(struct appctx *appctx)
453{
Willy Tarreauc12b3212022-05-27 11:08:15 +0200454 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau0eca5392022-05-27 10:44:25 +0200455 struct stream *s = __sc_strm(sc);
Emeric Brun97556472020-05-30 01:42:45 +0200456 struct sink *sink = strm_fe(s)->parent;
Willy Tarreau42cc8312022-05-04 20:42:23 +0200457 struct sink_forward_target *sft = appctx->svcctx;
Emeric Brun97556472020-05-30 01:42:45 +0200458 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 Shipitsin47d17182020-06-21 21:42:57 +0500465 /* if stopping was requested, close immediately */
Emeric Brun97556472020-05-30 01:42:45 +0200466 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 Tarreau0eca5392022-05-27 10:44:25 +0200473 sc_oc(sc)->rex = TICK_ETERNITY;
Emeric Brun97556472020-05-30 01:42:45 +0200474 /* rto should not change but it seems the case */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200475 sc_oc(sc)->rto = TICK_ETERNITY;
Emeric Brun97556472020-05-30 01:42:45 +0200476
477 /* an error was detected */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200478 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Emeric Brun97556472020-05-30 01:42:45 +0200479 goto close;
480
481 /* con closed by server side */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200482 if ((sc_oc(sc)->flags & CF_SHUTW))
Emeric Brun97556472020-05-30 01:42:45 +0200483 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 Tarreau0eca5392022-05-27 10:44:25 +0200488 if (sc_opposite(sc)->state < SC_ST_EST) {
Willy Tarreau90e8b452022-05-25 18:21:43 +0200489 applet_need_more_data(appctx);
Willy Tarreaub23edc82022-05-24 16:49:03 +0200490 se_need_remote_conn(appctx->sedesc);
Willy Tarreau4164eb92022-05-25 15:42:03 +0200491 applet_have_more_data(appctx);
Emeric Brun97556472020-05-30 01:42:45 +0200492 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 Tarreau4781b152021-04-06 13:53:36 +0200519 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun97556472020-05-30 01:42:45 +0200520 ofs += ring->ofs;
521 }
522
Emeric Brun97556472020-05-30 01:42:45 +0200523 /* 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 Tarreau0eca5392022-05-27 10:44:25 +0200527 if (sc_opposite(sc)->state == SC_ST_EST) {
Emeric Brunfdabf492020-12-02 17:02:09 +0100528 /* 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 Tarreau4781b152021-04-06 13:53:36 +0200533 HA_ATOMIC_DEC(b_peek(buf, ofs));
Emeric Brunfdabf492020-12-02 17:02:09 +0100534
Emeric Brun97556472020-05-30 01:42:45 +0200535 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 Tarreaud0a06d52022-05-18 15:07:19 +0200559 if (applet_putchk(appctx, &trash) == -1) {
Emeric Brun97556472020-05-30 01:42:45 +0200560 ret = 0;
561 break;
562 }
563 ofs += cnt + msg_len;
564 }
565
Willy Tarreau4781b152021-04-06 13:53:36 +0200566 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun97556472020-05-30 01:42:45 +0200567 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 Tarreau2b718102021-04-21 07:32:39 +0200575 LIST_APPEND(&ring->waiters, &appctx->wait_entry);
Emeric Brun97556472020-05-30 01:42:45 +0200576 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau4164eb92022-05-25 15:42:03 +0200577 applet_have_no_more_data(appctx);
Emeric Brun97556472020-05-30 01:42:45 +0200578 }
579 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
580
581 /* always drain data from server */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200582 co_skip(sc_oc(sc), sc_oc(sc)->output);
Emeric Brun97556472020-05-30 01:42:45 +0200583 return;
584
585close:
Willy Tarreau0eca5392022-05-27 10:44:25 +0200586 sc_shutw(sc);
587 sc_shutr(sc);
588 sc_ic(sc)->flags |= CF_READ_NULL;
Emeric Brun97556472020-05-30 01:42:45 +0200589}
590
Emeric Brun494c5052020-05-28 11:13:15 +0200591void __sink_forward_session_deinit(struct sink_forward_target *sft)
592{
Willy Tarreau0698c802022-05-11 14:09:57 +0200593 struct stream *s = appctx_strm(sft->appctx);
Emeric Brun494c5052020-05-28 11:13:15 +0200594 struct sink *sink;
595
Emeric Brun494c5052020-05-28 11:13:15 +0200596 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 Fauletaee57fc2022-05-12 15:34:48 +0200608static 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 Tarreau7cb9e6c2022-05-17 19:40:40 +0200621 s->scb->dst = addr;
Willy Tarreaucb041662022-05-17 19:44:42 +0200622 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200623
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 Brun494c5052020-05-28 11:13:15 +0200645
646static void sink_forward_session_release(struct appctx *appctx)
647{
Willy Tarreau42cc8312022-05-04 20:42:23 +0200648 struct sink_forward_target *sft = appctx->svcctx;
Emeric Brun494c5052020-05-28 11:13:15 +0200649
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
659static struct applet sink_forward_applet = {
660 .obj_type = OBJ_TYPE_APPLET,
661 .name = "<SINKFWD>", /* used for logging */
662 .fct = sink_forward_io_handler,
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200663 .init = sink_forward_session_init,
Emeric Brun494c5052020-05-28 11:13:15 +0200664 .release = sink_forward_session_release,
665};
666
Emeric Brun97556472020-05-30 01:42:45 +0200667static 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 Fauletaee57fc2022-05-12 15:34:48 +0200671 .init = sink_forward_session_init,
Emeric Brun97556472020-05-30 01:42:45 +0200672 .release = sink_forward_session_release,
673};
674
Emeric Brun494c5052020-05-28 11:13:15 +0200675/*
676 * Create a new peer session in assigned state (connect will start automatically)
Willy Tarreau42cc8312022-05-04 20:42:23 +0200677 * It sets its context into appctx->svcctx.
Emeric Brun494c5052020-05-28 11:13:15 +0200678 */
679static struct appctx *sink_forward_session_create(struct sink *sink, struct sink_forward_target *sft)
680{
Emeric Brun494c5052020-05-28 11:13:15 +0200681 struct appctx *appctx;
Emeric Brun97556472020-05-30 01:42:45 +0200682 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 Brun494c5052020-05-28 11:13:15 +0200686
Christopher Faulet6095d572022-05-16 17:09:48 +0200687 appctx = appctx_new_here(applet, NULL);
Christopher Faulet2479e5f2022-01-19 14:50:11 +0100688 if (!appctx)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100689 goto out_close;
Willy Tarreau42cc8312022-05-04 20:42:23 +0200690 appctx->svcctx = (void *)sft;
Emeric Brun494c5052020-05-28 11:13:15 +0200691
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200692 if (appctx_init(appctx) == -1)
Christopher Faulet92202da2022-05-11 12:22:10 +0200693 goto out_free_appctx;
Christopher Faulet13a35e52021-12-20 15:34:16 +0100694
Emeric Brun494c5052020-05-28 11:13:15 +0200695 return appctx;
696
697 /* Error unrolling */
Emeric Brun494c5052020-05-28 11:13:15 +0200698 out_free_appctx:
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200699 appctx_free_on_early_error(appctx);
Emeric Brun494c5052020-05-28 11:13:15 +0200700 out_close:
701 return NULL;
702}
703
704/*
705 * Task to handle connctions to forward servers
706 */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100707static struct task *process_sink_forward(struct task * task, void *context, unsigned int state)
Emeric Brun494c5052020-05-28 11:13:15 +0200708{
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 */
742int sink_init_forward(struct sink *sink)
743{
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200744 sink->forward_task = task_new_anywhere();
Emeric Brun494c5052020-05-28 11:13:15 +0200745 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 Brun99c453d2020-05-25 15:01:04 +0200754/*
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 */
760int 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 Brun494c5052020-05-28 11:13:15 +0200765 struct proxy *p;
Emeric Brun99c453d2020-05-25 15:01:04 +0200766
Willy Tarreau18d13062022-08-11 16:12:11 +0200767 if (strcmp(args[0], "ring") == 0) { /* new ring section */
Emeric Brun99c453d2020-05-25 15:01:04 +0200768 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 Brun54648852020-07-06 15:54:06 +0200787 cfg_sink = sink_new_buf(args[1], args[1], LOG_FORMAT_RAW, size);
Emeric Brun99c453d2020-05-25 15:01:04 +0200788 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 Brun494c5052020-05-28 11:13:15 +0200793
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 Brun99c453d2020-05-25 15:01:04 +0200809 }
810 else if (strcmp(args[0], "size") == 0) {
Willy Tarreau18d13062022-08-11 16:12:11 +0200811 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 Brun99c453d2020-05-25 15:01:04 +0200817 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 Tarreau0b8e9ce2022-08-11 16:38:20 +0200824 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 Tarreau18d13062022-08-11 16:12:11 +0200830 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 Brun99c453d2020-05-25 15:01:04 +0200840 err_code |= ERR_ALERT | ERR_FATAL;
841 goto err;
842 }
Willy Tarreau0b8e9ce2022-08-11 16:38:20 +0200843 }
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 Tarreau8e877052022-08-12 15:03:12 +0200865 fd = open(backing, O_RDWR | O_CREAT, 0600);
Willy Tarreau0b8e9ce2022-08-11 16:38:20 +0200866 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 Brun99c453d2020-05-25 15:01:04 +0200895 }
Emeric Brun494c5052020-05-28 11:13:15 +0200896 else if (strcmp(args[0],"server") == 0) {
Amaury Denoyelle30c05372021-03-08 16:36:46 +0100897 err_code |= parse_server(file, linenum, args, cfg_sink->forward_px, NULL,
898 SRV_PARSE_PARSE_ADDR|SRV_PARSE_INITIAL_RESOLVE);
Emeric Brun494c5052020-05-28 11:13:15 +0200899 }
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 Brun99c453d2020-05-25 15:01:04 +0200943 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 Brun54648852020-07-06 15:54:06 +0200950 cfg_sink->fmt = get_log_format(args[1]);
951 if (cfg_sink->fmt == LOG_FORMAT_UNSPEC) {
Emeric Brun99c453d2020-05-25 15:01:04 +0200952 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 Brun9f2ff3a2020-05-29 15:47:52 +0200993 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 Brun99c453d2020-05-25 15:01:04 +0200998
999err:
1000 return err_code;
1001}
1002
Emeric Brun94aab062021-04-02 10:41:36 +02001003/* 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 Shipitsinb2be9a12021-04-24 13:25:42 +05001015 * specified into logsrv->ring_name
Emeric Brun94aab062021-04-02 10:41:36 +02001016 */
1017struct 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 Brun94aab062021-04-02 10:41:36 +02001023
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 Zagorac8a8f2702021-06-15 15:33:20 +02001054 if (srv_init_per_thr(srv) == -1)
Emeric Brun94aab062021-04-02 10:41:36 +02001055 goto error;
1056
Emeric Brun94aab062021-04-02 10:41:36 +02001057 /* 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 Shipitsinb2be9a12021-04-24 13:25:42 +05001074 /* prepare description for the sink */
Emeric Brun94aab062021-04-02 10:41:36 +02001075 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 Faulet2ae25ea2022-05-12 14:50:09 +02001091 sft->sink = sink;
Emeric Brun94aab062021-04-02 10:41:36 +02001092 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;
1111error:
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 Tarreau2b718102021-04-21 07:32:39 +02001138 LIST_DELETE(&sink->sink_list);
Emeric Brun94aab062021-04-02 10:41:36 +02001139 free(sink->name);
1140 free(sink->desc);
1141 free(sink);
1142 }
1143
1144 return NULL;
1145}
1146
Emeric Brun99c453d2020-05-25 15:01:04 +02001147/*
1148 * Post parsing "ring" section.
1149 *
1150 * The function returns 0 in success case, otherwise, it returns error
1151 * flags.
1152 */
1153int cfg_post_parse_ring()
1154{
1155 int err_code = 0;
Emeric Brun494c5052020-05-28 11:13:15 +02001156 struct server *srv;
Emeric Brun99c453d2020-05-25 15:01:04 +02001157
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 Tarreau570a22b2020-06-02 12:00:46 +02001161 cfg_sink->name, cfg_sink->maxlen, (unsigned long)b_size(&cfg_sink->ctx.ring->buf));
Emeric Brun99c453d2020-05-25 15:01:04 +02001162 cfg_sink->maxlen = b_size(&cfg_sink->ctx.ring->buf);
1163 err_code |= ERR_ALERT;
1164 }
Emeric Brun494c5052020-05-28 11:13:15 +02001165
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 Brun99c453d2020-05-25 15:01:04 +02001178
Emeric Brun494c5052020-05-28 11:13:15 +02001179 /* 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 Faulet96417f32022-08-04 16:00:13 +02001189 sft->sink = cfg_sink;
Emeric Brun494c5052020-05-28 11:13:15 +02001190 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 Brun99c453d2020-05-25 15:01:04 +02001204 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*/
1212int post_sink_resolve()
1213{
Christopher Fauletfc633b62020-11-06 15:24:23 +01001214 int err_code = ERR_NONE;
Emeric Brun99c453d2020-05-25 15:01:04 +02001215 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 Brun94aab062021-04-02 10:41:36 +02001222 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 Brun99c453d2020-05-25 15:01:04 +02001244 err_code |= ERR_ALERT | ERR_FATAL;
1245 }
1246 logsrv->sink = sink;
1247 }
Emeric Brun94aab062021-04-02 10:41:36 +02001248
Emeric Brun99c453d2020-05-25 15:01:04 +02001249 }
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 Brun94aab062021-04-02 10:41:36 +02001255 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 Brun99c453d2020-05-25 15:01:04 +02001277 err_code |= ERR_ALERT | ERR_FATAL;
1278 }
1279 logsrv->sink = sink;
1280 }
1281 }
1282 }
Emeric Brun12941c82020-07-07 14:19:42 +02001283
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 Brun94aab062021-04-02 10:41:36 +02001288 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 Brun12941c82020-07-07 14:19:42 +02001310 err_code |= ERR_ALERT | ERR_FATAL;
1311 }
1312 logsrv->sink = sink;
1313 }
1314 }
1315 }
Emeric Brun99c453d2020-05-25 15:01:04 +02001316 return err_code;
1317}
1318
1319
Willy Tarreau973e6622019-08-20 11:57:52 +02001320static void sink_init()
1321{
Emeric Brun54648852020-07-06 15:54:06 +02001322 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 Tarreau4ed23ca2019-08-23 15:47:49 +02001325}
1326
1327static void sink_deinit()
1328{
1329 struct sink *sink, *sb;
1330
1331 list_for_each_entry_safe(sink, sb, &sink_list, sink_list) {
Willy Tarreau0b8e9ce2022-08-11 16:38:20 +02001332 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 Tarreau2b718102021-04-21 07:32:39 +02001338 LIST_DELETE(&sink->sink_list);
Emeric Brun99c453d2020-05-25 15:01:04 +02001339 free(sink->name);
1340 free(sink->desc);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +02001341 free(sink);
1342 }
Willy Tarreau973e6622019-08-20 11:57:52 +02001343}
1344
1345INITCALL0(STG_REGISTER, sink_init);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +02001346REGISTER_POST_DEINIT(sink_deinit);
Willy Tarreau973e6622019-08-20 11:57:52 +02001347
Willy Tarreau9f830d72019-08-26 18:17:04 +02001348static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001349 { { "show", "events", NULL }, "show events [<sink>] [-w] [-n] : show event sink state", cli_parse_show_events, NULL, NULL },
Willy Tarreau9f830d72019-08-26 18:17:04 +02001350 {{},}
1351}};
1352
1353INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
1354
Emeric Brun99c453d2020-05-25 15:01:04 +02001355/* config parsers for this section */
1356REGISTER_CONFIG_SECTION("ring", cfg_parse_ring, cfg_post_parse_ring);
1357REGISTER_POST_CHECK(post_sink_resolve);
1358
Willy Tarreau67b5a162019-08-11 16:38:56 +02001359/*
1360 * Local variables:
1361 * c-indent-level: 8
1362 * c-basic-offset: 8
1363 * End:
1364 */