blob: 65a218d635808a801caf619a58efa806f59507bd [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 Tarreau36979d92020-06-05 17:27:29 +020021#include <import/ist.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020022#include <haproxy/api.h>
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +020023#include <haproxy/applet.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020025#include <haproxy/cli.h>
Christopher Faulet908628c2022-03-25 16:43:49 +010026#include <haproxy/conn_stream.h>
27#include <haproxy/cs_utils.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020028#include <haproxy/errors.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020029#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020030#include <haproxy/log.h>
Willy Tarreau817538e2021-05-08 20:20:21 +020031#include <haproxy/proxy.h>
Willy Tarreaud2ad57c2020-06-03 19:43:35 +020032#include <haproxy/ring.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020033#include <haproxy/signal.h>
Willy Tarreauba2f73d2020-06-03 20:02:28 +020034#include <haproxy/sink.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020035#include <haproxy/time.h>
Willy Tarreau4bad5e22021-05-08 13:05:30 +020036#include <haproxy/tools.h>
Willy Tarreau67b5a162019-08-11 16:38:56 +020037
38struct list sink_list = LIST_HEAD_INIT(sink_list);
39
Emeric Brun99c453d2020-05-25 15:01:04 +020040struct sink *cfg_sink;
41
Willy Tarreau67b5a162019-08-11 16:38:56 +020042struct sink *sink_find(const char *name)
43{
44 struct sink *sink;
45
46 list_for_each_entry(sink, &sink_list, sink_list)
47 if (strcmp(sink->name, name) == 0)
48 return sink;
49 return NULL;
50}
51
52/* creates a new sink and adds it to the list, it's still generic and not fully
53 * initialized. Returns NULL on allocation failure. If another one already
54 * exists with the same name, it will be returned. The caller can detect it as
55 * a newly created one has type SINK_TYPE_NEW.
56 */
Emeric Brun54648852020-07-06 15:54:06 +020057static struct sink *__sink_new(const char *name, const char *desc, int fmt)
Willy Tarreau67b5a162019-08-11 16:38:56 +020058{
59 struct sink *sink;
60
61 sink = sink_find(name);
62 if (sink)
63 goto end;
64
Emeric Brun494c5052020-05-28 11:13:15 +020065 sink = calloc(1, sizeof(*sink));
Willy Tarreau67b5a162019-08-11 16:38:56 +020066 if (!sink)
67 goto end;
68
Emeric Brun99c453d2020-05-25 15:01:04 +020069 sink->name = strdup(name);
Tim Duesterhusa7ebffe2021-01-03 19:54:11 +010070 if (!sink->name)
71 goto err;
72
Emeric Brun99c453d2020-05-25 15:01:04 +020073 sink->desc = strdup(desc);
Tim Duesterhusa7ebffe2021-01-03 19:54:11 +010074 if (!sink->desc)
75 goto err;
76
Willy Tarreau67b5a162019-08-11 16:38:56 +020077 sink->fmt = fmt;
78 sink->type = SINK_TYPE_NEW;
Christopher Fauleta63a5c22019-11-15 15:10:12 +010079 sink->maxlen = BUFSIZE;
Willy Tarreau67b5a162019-08-11 16:38:56 +020080 /* address will be filled by the caller if needed */
Willy Tarreau973e6622019-08-20 11:57:52 +020081 sink->ctx.fd = -1;
Willy Tarreau67b5a162019-08-11 16:38:56 +020082 sink->ctx.dropped = 0;
83 HA_RWLOCK_INIT(&sink->ctx.lock);
Willy Tarreau2b718102021-04-21 07:32:39 +020084 LIST_APPEND(&sink_list, &sink->sink_list);
Willy Tarreau67b5a162019-08-11 16:38:56 +020085 end:
86 return sink;
Tim Duesterhusa7ebffe2021-01-03 19:54:11 +010087
88 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +010089 ha_free(&sink->name);
90 ha_free(&sink->desc);
91 ha_free(&sink);
Tim Duesterhusa7ebffe2021-01-03 19:54:11 +010092
93 return NULL;
Willy Tarreau67b5a162019-08-11 16:38:56 +020094}
95
Willy Tarreau973e6622019-08-20 11:57:52 +020096/* creates a sink called <name> of type FD associated to fd <fd>, format <fmt>,
97 * and description <desc>. Returns NULL on allocation failure or conflict.
98 * Perfect duplicates are merged (same type, fd, and name).
99 */
Emeric Brun54648852020-07-06 15:54:06 +0200100struct sink *sink_new_fd(const char *name, const char *desc, enum log_fmt fmt, int fd)
Willy Tarreau973e6622019-08-20 11:57:52 +0200101{
102 struct sink *sink;
103
104 sink = __sink_new(name, desc, fmt);
105 if (!sink || (sink->type == SINK_TYPE_FD && sink->ctx.fd == fd))
106 goto end;
107
108 if (sink->type != SINK_TYPE_NEW) {
109 sink = NULL;
110 goto end;
111 }
112
113 sink->type = SINK_TYPE_FD;
114 sink->ctx.fd = fd;
115 end:
116 return sink;
117}
118
Willy Tarreau4ed23ca2019-08-23 15:47:49 +0200119/* creates a sink called <name> of type BUF of size <size>, format <fmt>,
120 * and description <desc>. Returns NULL on allocation failure or conflict.
121 * Perfect duplicates are merged (same type and name). If sizes differ, the
122 * largest one is kept.
123 */
Emeric Brun54648852020-07-06 15:54:06 +0200124struct sink *sink_new_buf(const char *name, const char *desc, enum log_fmt fmt, size_t size)
Willy Tarreau4ed23ca2019-08-23 15:47:49 +0200125{
126 struct sink *sink;
127
128 sink = __sink_new(name, desc, fmt);
129 if (!sink)
130 goto fail;
131
132 if (sink->type == SINK_TYPE_BUFFER) {
133 /* such a buffer already exists, we may have to resize it */
134 if (!ring_resize(sink->ctx.ring, size))
135 goto fail;
136 goto end;
137 }
138
139 if (sink->type != SINK_TYPE_NEW) {
140 /* already exists of another type */
141 goto fail;
142 }
143
144 sink->ctx.ring = ring_new(size);
145 if (!sink->ctx.ring) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200146 LIST_DELETE(&sink->sink_list);
Emeric Brun99c453d2020-05-25 15:01:04 +0200147 free(sink->name);
148 free(sink->desc);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +0200149 free(sink);
150 goto fail;
151 }
152
153 sink->type = SINK_TYPE_BUFFER;
154 end:
155 return sink;
156 fail:
157 return NULL;
158}
159
Willy Tarreau67b5a162019-08-11 16:38:56 +0200160/* tries to send <nmsg> message parts (up to 8, ignored above) from message
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500161 * array <msg> to sink <sink>. Formatting according to the sink's preference is
Willy Tarreau8f240232019-08-27 16:41:06 +0200162 * done here. Lost messages are NOT accounted for. It is preferable to call
163 * sink_write() instead which will also try to emit the number of dropped
164 * messages when there are any. It returns >0 if it could write anything,
165 * <=0 otherwise.
Willy Tarreau67b5a162019-08-11 16:38:56 +0200166 */
Emeric Brun54648852020-07-06 15:54:06 +0200167 ssize_t __sink_write(struct sink *sink, const struct ist msg[], size_t nmsg,
168 int level, int facility, struct ist *metadata)
169 {
170 struct ist *pfx = NULL;
Willy Tarreaua1426de2019-08-27 14:21:02 +0200171 size_t npfx = 0;
Emeric Brunbd163812020-05-06 14:33:46 +0200172
Emeric Brun54648852020-07-06 15:54:06 +0200173 if (sink->fmt == LOG_FORMAT_RAW)
Emeric Brunbd163812020-05-06 14:33:46 +0200174 goto send;
Willy Tarreau67b5a162019-08-11 16:38:56 +0200175
Emeric Brun54648852020-07-06 15:54:06 +0200176 pfx = build_log_header(sink->fmt, level, facility, metadata, &npfx);
Emeric Brunbd163812020-05-06 14:33:46 +0200177
178send:
Willy Tarreau973e6622019-08-20 11:57:52 +0200179 if (sink->type == SINK_TYPE_FD) {
Willy Tarreau8f240232019-08-27 16:41:06 +0200180 return fd_write_frag_line(sink->ctx.fd, sink->maxlen, pfx, npfx, msg, nmsg, 1);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +0200181 }
182 else if (sink->type == SINK_TYPE_BUFFER) {
Willy Tarreau8f240232019-08-27 16:41:06 +0200183 return ring_write(sink->ctx.ring, sink->maxlen, pfx, npfx, msg, nmsg);
Willy Tarreau973e6622019-08-20 11:57:52 +0200184 }
Willy Tarreau8f240232019-08-27 16:41:06 +0200185 return 0;
186}
Willy Tarreau67b5a162019-08-11 16:38:56 +0200187
Willy Tarreau8f240232019-08-27 16:41:06 +0200188/* Tries to emit a message indicating the number of dropped events. In case of
189 * success, the amount of drops is reduced by as much. It's supposed to be
190 * called under an exclusive lock on the sink to avoid multiple produces doing
191 * the same. On success, >0 is returned, otherwise <=0 on failure.
192 */
Emeric Brun54648852020-07-06 15:54:06 +0200193int sink_announce_dropped(struct sink *sink, int facility)
Willy Tarreau8f240232019-08-27 16:41:06 +0200194{
Emeric Brun54648852020-07-06 15:54:06 +0200195 static THREAD_LOCAL struct ist metadata[LOG_META_FIELDS];
196 static THREAD_LOCAL pid_t curr_pid;
197 static THREAD_LOCAL char pidstr[16];
Willy Tarreau8f240232019-08-27 16:41:06 +0200198 unsigned int dropped;
199 struct buffer msg;
200 struct ist msgvec[1];
201 char logbuf[64];
202
203 while (unlikely((dropped = sink->ctx.dropped) > 0)) {
204 chunk_init(&msg, logbuf, sizeof(logbuf));
205 chunk_printf(&msg, "%u event%s dropped", dropped, dropped > 1 ? "s" : "");
206 msgvec[0] = ist2(msg.area, msg.data);
Emeric Brunbd163812020-05-06 14:33:46 +0200207
Emeric Brun54648852020-07-06 15:54:06 +0200208 if (!metadata[LOG_META_HOST].len) {
209 if (global.log_send_hostname)
Tim Duesterhus77508502022-03-15 13:11:06 +0100210 metadata[LOG_META_HOST] = ist(global.log_send_hostname);
Emeric Brun54648852020-07-06 15:54:06 +0200211 }
212
213 if (!metadata[LOG_META_TAG].len)
214 metadata[LOG_META_TAG] = ist2(global.log_tag.area, global.log_tag.data);
215
216 if (unlikely(curr_pid != getpid()))
217 metadata[LOG_META_PID].len = 0;
218
219 if (!metadata[LOG_META_PID].len) {
220 curr_pid = getpid();
221 ltoa_o(curr_pid, pidstr, sizeof(pidstr));
222 metadata[LOG_META_PID] = ist2(pidstr, strlen(pidstr));
223 }
224
225 if (__sink_write(sink, msgvec, 1, LOG_NOTICE, facility, metadata) <= 0)
Willy Tarreau8f240232019-08-27 16:41:06 +0200226 return 0;
227 /* success! */
228 HA_ATOMIC_SUB(&sink->ctx.dropped, dropped);
229 }
230 return 1;
Willy Tarreau67b5a162019-08-11 16:38:56 +0200231}
232
Willy Tarreau9f830d72019-08-26 18:17:04 +0200233/* parse the "show events" command, returns 1 if a message is returned, otherwise zero */
234static int cli_parse_show_events(char **args, char *payload, struct appctx *appctx, void *private)
235{
236 struct sink *sink;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200237 int arg;
Willy Tarreau9f830d72019-08-26 18:17:04 +0200238
239 args++; // make args[1] the 1st arg
240
241 if (!*args[1]) {
242 /* no arg => report the list of supported sink */
Willy Tarreau1d181e42019-08-30 11:17:01 +0200243 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 +0200244 list_for_each_entry(sink, &sink_list, sink_list) {
245 chunk_appendf(&trash, " %-10s : type=%s, %u dropped, %s\n",
246 sink->name,
247 sink->type == SINK_TYPE_NEW ? "init" :
248 sink->type == SINK_TYPE_FD ? "fd" :
249 sink->type == SINK_TYPE_BUFFER ? "buffer" : "?",
250 sink->ctx.dropped, sink->desc);
251 }
252
253 trash.area[trash.data] = 0;
254 return cli_msg(appctx, LOG_WARNING, trash.area);
255 }
256
257 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
258 return 1;
259
260 sink = sink_find(args[1]);
261 if (!sink)
262 return cli_err(appctx, "No such event sink");
263
264 if (sink->type != SINK_TYPE_BUFFER)
265 return cli_msg(appctx, LOG_NOTICE, "Nothing to report for this sink");
266
Willy Tarreau1d181e42019-08-30 11:17:01 +0200267 for (arg = 2; *args[arg]; arg++) {
268 if (strcmp(args[arg], "-w") == 0)
269 appctx->ctx.cli.i0 |= 1; // wait mode
270 else if (strcmp(args[arg], "-n") == 0)
271 appctx->ctx.cli.i0 |= 2; // seek to new
272 else if (strcmp(args[arg], "-nw") == 0 || strcmp(args[arg], "-wn") == 0)
273 appctx->ctx.cli.i0 |= 3; // seek to new + wait
274 else
275 return cli_err(appctx, "unknown option");
276 }
Willy Tarreau9f830d72019-08-26 18:17:04 +0200277 return ring_attach_cli(sink->ctx.ring, appctx);
278}
279
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500280/* Pre-configures a ring proxy to emit connections */
Emeric Brun494c5052020-05-28 11:13:15 +0200281void sink_setup_proxy(struct proxy *px)
282{
283 px->last_change = now.tv_sec;
284 px->cap = PR_CAP_FE | PR_CAP_BE;
285 px->maxconn = 0;
286 px->conn_retries = 1;
287 px->timeout.server = TICK_ETERNITY;
288 px->timeout.client = TICK_ETERNITY;
289 px->timeout.connect = TICK_ETERNITY;
290 px->accept = NULL;
291 px->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Emeric Brun494c5052020-05-28 11:13:15 +0200292}
293
294/*
Willy Tarreau42cc8312022-05-04 20:42:23 +0200295 * IO Handler to handle message push to syslog tcp server.
296 * It takes its context from appctx->svcctx.
Emeric Brun494c5052020-05-28 11:13:15 +0200297 */
298static void sink_forward_io_handler(struct appctx *appctx)
299{
Christopher Faulet908628c2022-03-25 16:43:49 +0100300 struct conn_stream *cs = appctx->owner;
301 struct stream *s = __cs_strm(cs);
Emeric Brun494c5052020-05-28 11:13:15 +0200302 struct sink *sink = strm_fe(s)->parent;
Willy Tarreau42cc8312022-05-04 20:42:23 +0200303 struct sink_forward_target *sft = appctx->svcctx;
Emeric Brun494c5052020-05-28 11:13:15 +0200304 struct ring *ring = sink->ctx.ring;
305 struct buffer *buf = &ring->buf;
306 uint64_t msg_len;
307 size_t len, cnt, ofs;
308 int ret = 0;
309
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500310 /* if stopping was requested, close immediately */
Emeric Brun494c5052020-05-28 11:13:15 +0200311 if (unlikely(stopping))
312 goto close;
313
314 /* for rex because it seems reset to timeout
315 * and we don't want expire on this case
316 * with a syslog server
317 */
Christopher Faulet908628c2022-03-25 16:43:49 +0100318 cs_oc(cs)->rex = TICK_ETERNITY;
Emeric Brun494c5052020-05-28 11:13:15 +0200319 /* rto should not change but it seems the case */
Christopher Faulet908628c2022-03-25 16:43:49 +0100320 cs_oc(cs)->rto = TICK_ETERNITY;
Emeric Brun494c5052020-05-28 11:13:15 +0200321
322 /* an error was detected */
Christopher Faulet908628c2022-03-25 16:43:49 +0100323 if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Emeric Brun494c5052020-05-28 11:13:15 +0200324 goto close;
325
326 /* con closed by server side */
Christopher Faulet908628c2022-03-25 16:43:49 +0100327 if ((cs_oc(cs)->flags & CF_SHUTW))
Emeric Brun494c5052020-05-28 11:13:15 +0200328 goto close;
329
330 /* if the connection is not established, inform the stream that we want
331 * to be notified whenever the connection completes.
332 */
Christopher Faulet62e75742022-03-31 09:16:34 +0200333 if (cs_opposite(cs)->state < CS_ST_EST) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200334 cs_cant_get(cs);
335 cs_rx_conn_blk(cs);
336 cs_rx_endp_more(cs);
Emeric Brun494c5052020-05-28 11:13:15 +0200337 return;
338 }
339
340 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
341 if (appctx != sft->appctx) {
342 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
343 goto close;
344 }
345 ofs = sft->ofs;
346
347 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
348 LIST_DEL_INIT(&appctx->wait_entry);
349 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
350
351 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
352
353 /* explanation for the initialization below: it would be better to do
354 * this in the parsing function but this would occasionally result in
355 * dropped events because we'd take a reference on the oldest message
356 * and keep it while being scheduled. Thus instead let's take it the
357 * first time we enter here so that we have a chance to pass many
358 * existing messages before grabbing a reference to a location. This
359 * value cannot be produced after initialization.
360 */
361 if (unlikely(ofs == ~0)) {
362 ofs = 0;
363
Willy Tarreau4781b152021-04-06 13:53:36 +0200364 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun494c5052020-05-28 11:13:15 +0200365 ofs += ring->ofs;
366 }
367
Emeric Brun494c5052020-05-28 11:13:15 +0200368 /* in this loop, ofs always points to the counter byte that precedes
369 * the message so that we can take our reference there if we have to
370 * stop before the end (ret=0).
371 */
Christopher Faulet62e75742022-03-31 09:16:34 +0200372 if (cs_opposite(cs)->state == CS_ST_EST) {
Emeric Brunfdabf492020-12-02 17:02:09 +0100373 /* we were already there, adjust the offset to be relative to
374 * the buffer's head and remove us from the counter.
375 */
376 ofs -= ring->ofs;
377 BUG_ON(ofs >= buf->size);
Willy Tarreau4781b152021-04-06 13:53:36 +0200378 HA_ATOMIC_DEC(b_peek(buf, ofs));
Emeric Brunfdabf492020-12-02 17:02:09 +0100379
Emeric Brun494c5052020-05-28 11:13:15 +0200380 ret = 1;
381 while (ofs + 1 < b_data(buf)) {
382 cnt = 1;
383 len = b_peek_varint(buf, ofs + cnt, &msg_len);
384 if (!len)
385 break;
386 cnt += len;
387 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
388
389 if (unlikely(msg_len + 1 > b_size(&trash))) {
390 /* too large a message to ever fit, let's skip it */
391 ofs += cnt + msg_len;
392 continue;
393 }
394
395 chunk_reset(&trash);
396 len = b_getblk(buf, trash.area, msg_len, ofs + cnt);
397 trash.data += len;
398 trash.area[trash.data++] = '\n';
399
Christopher Faulet908628c2022-03-25 16:43:49 +0100400 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200401 cs_rx_room_blk(cs);
Emeric Brun494c5052020-05-28 11:13:15 +0200402 ret = 0;
403 break;
404 }
405 ofs += cnt + msg_len;
406 }
407
Willy Tarreau4781b152021-04-06 13:53:36 +0200408 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun494c5052020-05-28 11:13:15 +0200409 ofs += ring->ofs;
410 sft->ofs = ofs;
411 }
412 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
413
414 if (ret) {
415 /* let's be woken up once new data arrive */
416 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau2b718102021-04-21 07:32:39 +0200417 LIST_APPEND(&ring->waiters, &appctx->wait_entry);
Emeric Brun494c5052020-05-28 11:13:15 +0200418 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200419 cs_rx_endp_done(cs);
Emeric Brun494c5052020-05-28 11:13:15 +0200420 }
421 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
422
423 /* always drain data from server */
Christopher Faulet908628c2022-03-25 16:43:49 +0100424 co_skip(cs_oc(cs), cs_oc(cs)->output);
Emeric Brun494c5052020-05-28 11:13:15 +0200425 return;
426
427close:
Christopher Fauletda098e62022-03-31 17:44:45 +0200428 cs_shutw(cs);
429 cs_shutr(cs);
Christopher Faulet908628c2022-03-25 16:43:49 +0100430 cs_ic(cs)->flags |= CF_READ_NULL;
Emeric Brun494c5052020-05-28 11:13:15 +0200431}
432
Emeric Brun97556472020-05-30 01:42:45 +0200433/*
434 * IO Handler to handle message push to syslog tcp server
435 * using octet counting frames
Willy Tarreau42cc8312022-05-04 20:42:23 +0200436 * It takes its context from appctx->svcctx.
Emeric Brun97556472020-05-30 01:42:45 +0200437 */
438static void sink_forward_oc_io_handler(struct appctx *appctx)
439{
Christopher Faulet908628c2022-03-25 16:43:49 +0100440 struct conn_stream *cs = appctx->owner;
441 struct stream *s = __cs_strm(cs);
Emeric Brun97556472020-05-30 01:42:45 +0200442 struct sink *sink = strm_fe(s)->parent;
Willy Tarreau42cc8312022-05-04 20:42:23 +0200443 struct sink_forward_target *sft = appctx->svcctx;
Emeric Brun97556472020-05-30 01:42:45 +0200444 struct ring *ring = sink->ctx.ring;
445 struct buffer *buf = &ring->buf;
446 uint64_t msg_len;
447 size_t len, cnt, ofs;
448 int ret = 0;
449 char *p;
450
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500451 /* if stopping was requested, close immediately */
Emeric Brun97556472020-05-30 01:42:45 +0200452 if (unlikely(stopping))
453 goto close;
454
455 /* for rex because it seems reset to timeout
456 * and we don't want expire on this case
457 * with a syslog server
458 */
Christopher Faulet908628c2022-03-25 16:43:49 +0100459 cs_oc(cs)->rex = TICK_ETERNITY;
Emeric Brun97556472020-05-30 01:42:45 +0200460 /* rto should not change but it seems the case */
Christopher Faulet908628c2022-03-25 16:43:49 +0100461 cs_oc(cs)->rto = TICK_ETERNITY;
Emeric Brun97556472020-05-30 01:42:45 +0200462
463 /* an error was detected */
Christopher Faulet908628c2022-03-25 16:43:49 +0100464 if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Emeric Brun97556472020-05-30 01:42:45 +0200465 goto close;
466
467 /* con closed by server side */
Christopher Faulet908628c2022-03-25 16:43:49 +0100468 if ((cs_oc(cs)->flags & CF_SHUTW))
Emeric Brun97556472020-05-30 01:42:45 +0200469 goto close;
470
471 /* if the connection is not established, inform the stream that we want
472 * to be notified whenever the connection completes.
473 */
Christopher Faulet62e75742022-03-31 09:16:34 +0200474 if (cs_opposite(cs)->state < CS_ST_EST) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200475 cs_cant_get(cs);
476 cs_rx_conn_blk(cs);
477 cs_rx_endp_more(cs);
Emeric Brun97556472020-05-30 01:42:45 +0200478 return;
479 }
480
481 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
482 if (appctx != sft->appctx) {
483 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
484 goto close;
485 }
486 ofs = sft->ofs;
487
488 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
489 LIST_DEL_INIT(&appctx->wait_entry);
490 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
491
492 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
493
494 /* explanation for the initialization below: it would be better to do
495 * this in the parsing function but this would occasionally result in
496 * dropped events because we'd take a reference on the oldest message
497 * and keep it while being scheduled. Thus instead let's take it the
498 * first time we enter here so that we have a chance to pass many
499 * existing messages before grabbing a reference to a location. This
500 * value cannot be produced after initialization.
501 */
502 if (unlikely(ofs == ~0)) {
503 ofs = 0;
504
Willy Tarreau4781b152021-04-06 13:53:36 +0200505 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun97556472020-05-30 01:42:45 +0200506 ofs += ring->ofs;
507 }
508
Emeric Brun97556472020-05-30 01:42:45 +0200509 /* in this loop, ofs always points to the counter byte that precedes
510 * the message so that we can take our reference there if we have to
511 * stop before the end (ret=0).
512 */
Christopher Faulet62e75742022-03-31 09:16:34 +0200513 if (cs_opposite(cs)->state == CS_ST_EST) {
Emeric Brunfdabf492020-12-02 17:02:09 +0100514 /* we were already there, adjust the offset to be relative to
515 * the buffer's head and remove us from the counter.
516 */
517 ofs -= ring->ofs;
518 BUG_ON(ofs >= buf->size);
Willy Tarreau4781b152021-04-06 13:53:36 +0200519 HA_ATOMIC_DEC(b_peek(buf, ofs));
Emeric Brunfdabf492020-12-02 17:02:09 +0100520
Emeric Brun97556472020-05-30 01:42:45 +0200521 ret = 1;
522 while (ofs + 1 < b_data(buf)) {
523 cnt = 1;
524 len = b_peek_varint(buf, ofs + cnt, &msg_len);
525 if (!len)
526 break;
527 cnt += len;
528 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
529
530 chunk_reset(&trash);
531 p = ulltoa(msg_len, trash.area, b_size(&trash));
532 if (p) {
533 trash.data = (p - trash.area) + 1;
534 *p = ' ';
535 }
536
537 if (!p || (trash.data + msg_len > b_size(&trash))) {
538 /* too large a message to ever fit, let's skip it */
539 ofs += cnt + msg_len;
540 continue;
541 }
542
543 trash.data += b_getblk(buf, p + 1, msg_len, ofs + cnt);
544
Christopher Faulet908628c2022-03-25 16:43:49 +0100545 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200546 cs_rx_room_blk(cs);
Emeric Brun97556472020-05-30 01:42:45 +0200547 ret = 0;
548 break;
549 }
550 ofs += cnt + msg_len;
551 }
552
Willy Tarreau4781b152021-04-06 13:53:36 +0200553 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun97556472020-05-30 01:42:45 +0200554 ofs += ring->ofs;
555 sft->ofs = ofs;
556 }
557 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
558
559 if (ret) {
560 /* let's be woken up once new data arrive */
561 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau2b718102021-04-21 07:32:39 +0200562 LIST_APPEND(&ring->waiters, &appctx->wait_entry);
Emeric Brun97556472020-05-30 01:42:45 +0200563 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200564 cs_rx_endp_done(cs);
Emeric Brun97556472020-05-30 01:42:45 +0200565 }
566 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
567
568 /* always drain data from server */
Christopher Faulet908628c2022-03-25 16:43:49 +0100569 co_skip(cs_oc(cs), cs_oc(cs)->output);
Emeric Brun97556472020-05-30 01:42:45 +0200570 return;
571
572close:
Christopher Fauletda098e62022-03-31 17:44:45 +0200573 cs_shutw(cs);
574 cs_shutr(cs);
Christopher Faulet908628c2022-03-25 16:43:49 +0100575 cs_ic(cs)->flags |= CF_READ_NULL;
Emeric Brun97556472020-05-30 01:42:45 +0200576}
577
Emeric Brun494c5052020-05-28 11:13:15 +0200578void __sink_forward_session_deinit(struct sink_forward_target *sft)
579{
Christopher Faulet908628c2022-03-25 16:43:49 +0100580 struct stream *s = __cs_strm(sft->appctx->owner);
Emeric Brun494c5052020-05-28 11:13:15 +0200581 struct sink *sink;
582
Emeric Brun494c5052020-05-28 11:13:15 +0200583 sink = strm_fe(s)->parent;
584 if (!sink)
585 return;
586
587 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &sink->ctx.ring->lock);
588 LIST_DEL_INIT(&sft->appctx->wait_entry);
589 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &sink->ctx.ring->lock);
590
591 sft->appctx = NULL;
592 task_wakeup(sink->forward_task, TASK_WOKEN_MSG);
593}
594
595
596static void sink_forward_session_release(struct appctx *appctx)
597{
Willy Tarreau42cc8312022-05-04 20:42:23 +0200598 struct sink_forward_target *sft = appctx->svcctx;
Emeric Brun494c5052020-05-28 11:13:15 +0200599
600 if (!sft)
601 return;
602
603 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
604 if (sft->appctx == appctx)
605 __sink_forward_session_deinit(sft);
606 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
607}
608
609static struct applet sink_forward_applet = {
610 .obj_type = OBJ_TYPE_APPLET,
611 .name = "<SINKFWD>", /* used for logging */
612 .fct = sink_forward_io_handler,
613 .release = sink_forward_session_release,
614};
615
Emeric Brun97556472020-05-30 01:42:45 +0200616static struct applet sink_forward_oc_applet = {
617 .obj_type = OBJ_TYPE_APPLET,
618 .name = "<SINKFWDOC>", /* used for logging */
619 .fct = sink_forward_oc_io_handler,
620 .release = sink_forward_session_release,
621};
622
Emeric Brun494c5052020-05-28 11:13:15 +0200623/*
624 * Create a new peer session in assigned state (connect will start automatically)
Willy Tarreau42cc8312022-05-04 20:42:23 +0200625 * It sets its context into appctx->svcctx.
Emeric Brun494c5052020-05-28 11:13:15 +0200626 */
627static struct appctx *sink_forward_session_create(struct sink *sink, struct sink_forward_target *sft)
628{
629 struct proxy *p = sink->forward_px;
630 struct appctx *appctx;
631 struct session *sess;
Christopher Faulet13a35e52021-12-20 15:34:16 +0100632 struct conn_stream *cs;
Emeric Brun494c5052020-05-28 11:13:15 +0200633 struct stream *s;
Emeric Brun97556472020-05-30 01:42:45 +0200634 struct applet *applet = &sink_forward_applet;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100635 struct sockaddr_storage *addr = NULL;
Emeric Brun97556472020-05-30 01:42:45 +0200636
637 if (sft->srv->log_proto == SRV_LOG_PROTO_OCTET_COUNTING)
638 applet = &sink_forward_oc_applet;
Emeric Brun494c5052020-05-28 11:13:15 +0200639
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100640 appctx = appctx_new(applet, NULL);
Christopher Faulet2479e5f2022-01-19 14:50:11 +0100641 if (!appctx)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100642 goto out_close;
Emeric Brun494c5052020-05-28 11:13:15 +0200643
Willy Tarreau42cc8312022-05-04 20:42:23 +0200644 appctx->svcctx = (void *)sft;
Emeric Brun494c5052020-05-28 11:13:15 +0200645
646 sess = session_new(p, NULL, &appctx->obj_type);
647 if (!sess) {
Christopher Faulet13a35e52021-12-20 15:34:16 +0100648 ha_alert("out of memory in sink_forward_session_create().\n");
Emeric Brun494c5052020-05-28 11:13:15 +0200649 goto out_free_appctx;
650 }
651
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100652 if (!sockaddr_alloc(&addr, &sft->srv->addr, sizeof(sft->srv->addr)))
Christopher Faulet2479e5f2022-01-19 14:50:11 +0100653 goto out_free_sess;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100654
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100655 cs = cs_new_from_applet(appctx->endp, sess, &BUF_NULL);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100656 if (!cs) {
657 ha_alert("Failed to initialize stream in sink_forward_session_create().\n");
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100658 goto out_free_addr;
Christopher Faulet13a35e52021-12-20 15:34:16 +0100659 }
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100660 s = DISGUISE(cs_strm(cs));
Christopher Faulet13a35e52021-12-20 15:34:16 +0100661
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200662 s->csb->dst = addr;
Christopher Faulet8abe7122022-03-30 15:10:18 +0200663 s->csb->flags |= CS_FL_NOLINGER;
Emeric Brun494c5052020-05-28 11:13:15 +0200664
665 s->target = &sft->srv->obj_type;
Willy Tarreau03bd3952022-05-02 16:36:47 +0200666 s->flags = SF_ASSIGNED;
Emeric Brun494c5052020-05-28 11:13:15 +0200667
668 s->do_log = NULL;
669 s->uniq_id = 0;
670
671 s->res.flags |= CF_READ_DONTWAIT;
672 /* for rto and rex to eternity to not expire on idle recv:
673 * We are using a syslog server.
674 */
675 s->res.rto = TICK_ETERNITY;
676 s->res.rex = TICK_ETERNITY;
677 sft->appctx = appctx;
Emeric Brun494c5052020-05-28 11:13:15 +0200678 return appctx;
679
680 /* Error unrolling */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100681 out_free_addr:
682 sockaddr_free(&addr);
Emeric Brun494c5052020-05-28 11:13:15 +0200683 out_free_sess:
684 session_free(sess);
685 out_free_appctx:
686 appctx_free(appctx);
687 out_close:
688 return NULL;
689}
690
691/*
692 * Task to handle connctions to forward servers
693 */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100694static struct task *process_sink_forward(struct task * task, void *context, unsigned int state)
Emeric Brun494c5052020-05-28 11:13:15 +0200695{
696 struct sink *sink = (struct sink *)context;
697 struct sink_forward_target *sft = sink->sft;
698
699 task->expire = TICK_ETERNITY;
700
701 if (!stopping) {
702 while (sft) {
703 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
704 /* if appctx is NULL, start a new session */
705 if (!sft->appctx)
706 sft->appctx = sink_forward_session_create(sink, sft);
707 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
708 sft = sft->next;
709 }
710 }
711 else {
712 while (sft) {
713 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
714 /* awake applet to perform a clean close */
715 if (sft->appctx)
716 appctx_wakeup(sft->appctx);
717 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
718 sft = sft->next;
719 }
720 }
721
722 return task;
723}
724/*
725 * Init task to manage connctions to forward servers
726 *
727 * returns 0 in case of error.
728 */
729int sink_init_forward(struct sink *sink)
730{
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200731 sink->forward_task = task_new_anywhere();
Emeric Brun494c5052020-05-28 11:13:15 +0200732 if (!sink->forward_task)
733 return 0;
734
735 sink->forward_task->process = process_sink_forward;
736 sink->forward_task->context = (void *)sink;
737 sink->forward_sighandler = signal_register_task(0, sink->forward_task, 0);
738 task_wakeup(sink->forward_task, TASK_WOKEN_INIT);
739 return 1;
740}
Emeric Brun99c453d2020-05-25 15:01:04 +0200741/*
742 * Parse "ring" section and create corresponding sink buffer.
743 *
744 * The function returns 0 in success case, otherwise, it returns error
745 * flags.
746 */
747int cfg_parse_ring(const char *file, int linenum, char **args, int kwm)
748{
749 int err_code = 0;
750 const char *inv;
751 size_t size = BUFSIZE;
Emeric Brun494c5052020-05-28 11:13:15 +0200752 struct proxy *p;
Emeric Brun99c453d2020-05-25 15:01:04 +0200753
754 if (strcmp(args[0], "ring") == 0) { /* new peers section */
755 if (!*args[1]) {
756 ha_alert("parsing [%s:%d] : missing ring name.\n", file, linenum);
757 err_code |= ERR_ALERT | ERR_FATAL;
758 goto err;
759 }
760
761 inv = invalid_char(args[1]);
762 if (inv) {
763 ha_alert("parsing [%s:%d] : invalid ring name '%s' (character '%c' is not permitted).\n", file, linenum, args[1], *inv);
764 err_code |= ERR_ALERT | ERR_FATAL;
765 goto err;
766 }
767
768 if (sink_find(args[1])) {
769 ha_alert("parsing [%s:%d] : sink named '%s' already exists.\n", file, linenum, args[1]);
770 err_code |= ERR_ALERT | ERR_FATAL;
771 goto err;
772 }
773
Emeric Brun54648852020-07-06 15:54:06 +0200774 cfg_sink = sink_new_buf(args[1], args[1], LOG_FORMAT_RAW, size);
Emeric Brun99c453d2020-05-25 15:01:04 +0200775 if (!cfg_sink || cfg_sink->type != SINK_TYPE_BUFFER) {
776 ha_alert("parsing [%s:%d] : unable to create a new sink buffer for ring '%s'.\n", file, linenum, args[1]);
777 err_code |= ERR_ALERT | ERR_FATAL;
778 goto err;
779 }
Emeric Brun494c5052020-05-28 11:13:15 +0200780
781 /* allocate new proxy to handle forwards */
782 p = calloc(1, sizeof *p);
783 if (!p) {
784 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
785 err_code |= ERR_ALERT | ERR_FATAL;
786 goto err;
787 }
788
789 init_new_proxy(p);
790 sink_setup_proxy(p);
791 p->parent = cfg_sink;
792 p->id = strdup(args[1]);
793 p->conf.args.file = p->conf.file = strdup(file);
794 p->conf.args.line = p->conf.line = linenum;
795 cfg_sink->forward_px = p;
Emeric Brun99c453d2020-05-25 15:01:04 +0200796 }
797 else if (strcmp(args[0], "size") == 0) {
798 size = atol(args[1]);
799 if (!size) {
800 ha_alert("parsing [%s:%d] : invalid size '%s' for new sink buffer.\n", file, linenum, args[1]);
801 err_code |= ERR_ALERT | ERR_FATAL;
802 goto err;
803 }
804
805 if (!cfg_sink || (cfg_sink->type != SINK_TYPE_BUFFER)
806 || !ring_resize(cfg_sink->ctx.ring, size)) {
807 ha_alert("parsing [%s:%d] : fail to set sink buffer size '%s'.\n", file, linenum, args[1]);
808 err_code |= ERR_ALERT | ERR_FATAL;
809 goto err;
810 }
811 }
Emeric Brun494c5052020-05-28 11:13:15 +0200812 else if (strcmp(args[0],"server") == 0) {
Amaury Denoyelle30c05372021-03-08 16:36:46 +0100813 err_code |= parse_server(file, linenum, args, cfg_sink->forward_px, NULL,
814 SRV_PARSE_PARSE_ADDR|SRV_PARSE_INITIAL_RESOLVE);
Emeric Brun494c5052020-05-28 11:13:15 +0200815 }
816 else if (strcmp(args[0],"timeout") == 0) {
817 if (!cfg_sink || !cfg_sink->forward_px) {
818 ha_alert("parsing [%s:%d] : unable to set timeout '%s'.\n", file, linenum, args[1]);
819 err_code |= ERR_ALERT | ERR_FATAL;
820 goto err;
821 }
822
823 if (strcmp(args[1], "connect") == 0 ||
824 strcmp(args[1], "server") == 0) {
825 const char *res;
826 unsigned int tout;
827
828 if (!*args[2]) {
829 ha_alert("parsing [%s:%d] : '%s %s' expects <time> as argument.\n",
830 file, linenum, args[0], args[1]);
831 err_code |= ERR_ALERT | ERR_FATAL;
832 goto err;
833 }
834 res = parse_time_err(args[2], &tout, TIME_UNIT_MS);
835 if (res == PARSE_TIME_OVER) {
836 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
837 file, linenum, args[2], args[0], args[1]);
838 err_code |= ERR_ALERT | ERR_FATAL;
839 goto err;
840 }
841 else if (res == PARSE_TIME_UNDER) {
842 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
843 file, linenum, args[2], args[0], args[1]);
844 err_code |= ERR_ALERT | ERR_FATAL;
845 goto err;
846 }
847 else if (res) {
848 ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s %s>.\n",
849 file, linenum, *res, args[0], args[1]);
850 err_code |= ERR_ALERT | ERR_FATAL;
851 goto err;
852 }
853 if (args[1][2] == 'c')
854 cfg_sink->forward_px->timeout.connect = tout;
855 else
856 cfg_sink->forward_px->timeout.server = tout;
857 }
858 }
Emeric Brun99c453d2020-05-25 15:01:04 +0200859 else if (strcmp(args[0],"format") == 0) {
860 if (!cfg_sink) {
861 ha_alert("parsing [%s:%d] : unable to set format '%s'.\n", file, linenum, args[1]);
862 err_code |= ERR_ALERT | ERR_FATAL;
863 goto err;
864 }
865
Emeric Brun54648852020-07-06 15:54:06 +0200866 cfg_sink->fmt = get_log_format(args[1]);
867 if (cfg_sink->fmt == LOG_FORMAT_UNSPEC) {
Emeric Brun99c453d2020-05-25 15:01:04 +0200868 ha_alert("parsing [%s:%d] : unknown format '%s'.\n", file, linenum, args[1]);
869 err_code |= ERR_ALERT | ERR_FATAL;
870 goto err;
871 }
872 }
873 else if (strcmp(args[0],"maxlen") == 0) {
874 if (!cfg_sink) {
875 ha_alert("parsing [%s:%d] : unable to set event max length '%s'.\n", file, linenum, args[1]);
876 err_code |= ERR_ALERT | ERR_FATAL;
877 goto err;
878 }
879
880 cfg_sink->maxlen = atol(args[1]);
881 if (!cfg_sink->maxlen) {
882 ha_alert("parsing [%s:%d] : invalid size '%s' for new sink buffer.\n", file, linenum, args[1]);
883 err_code |= ERR_ALERT | ERR_FATAL;
884 goto err;
885 }
886 }
887 else if (strcmp(args[0],"description") == 0) {
888 if (!cfg_sink) {
889 ha_alert("parsing [%s:%d] : unable to set description '%s'.\n", file, linenum, args[1]);
890 err_code |= ERR_ALERT | ERR_FATAL;
891 goto err;
892 }
893
894 if (!*args[1]) {
895 ha_alert("parsing [%s:%d] : missing ring description text.\n", file, linenum);
896 err_code |= ERR_ALERT | ERR_FATAL;
897 goto err;
898 }
899
900 free(cfg_sink->desc);
901
902 cfg_sink->desc = strdup(args[1]);
903 if (!cfg_sink->desc) {
904 ha_alert("parsing [%s:%d] : fail to set description '%s'.\n", file, linenum, args[1]);
905 err_code |= ERR_ALERT | ERR_FATAL;
906 goto err;
907 }
908 }
Emeric Brun9f2ff3a2020-05-29 15:47:52 +0200909 else {
910 ha_alert("parsing [%s:%d] : unknown statement '%s'.\n", file, linenum, args[0]);
911 err_code |= ERR_ALERT | ERR_FATAL;
912 goto err;
913 }
Emeric Brun99c453d2020-05-25 15:01:04 +0200914
915err:
916 return err_code;
917}
918
Emeric Brun94aab062021-04-02 10:41:36 +0200919/* Creates an new sink buffer from a log server.
920 *
921 * It uses the logsrvaddress to declare a forward
922 * server for this buffer. And it initializes the
923 * forwarding.
924 *
925 * The function returns a pointer on the
926 * allocated struct sink if allocate
927 * and initialize succeed, else if it fails
928 * it returns NULL.
929 *
930 * Note: the sink is created using the name
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +0500931 * specified into logsrv->ring_name
Emeric Brun94aab062021-04-02 10:41:36 +0200932 */
933struct sink *sink_new_from_logsrv(struct logsrv *logsrv)
934{
935 struct proxy *p = NULL;
936 struct sink *sink = NULL;
937 struct server *srv = NULL;
938 struct sink_forward_target *sft = NULL;
Emeric Brun94aab062021-04-02 10:41:36 +0200939
940 /* allocate new proxy to handle
941 * forward to a stream server
942 */
943 p = calloc(1, sizeof *p);
944 if (!p) {
945 goto error;
946 }
947
948 init_new_proxy(p);
949 sink_setup_proxy(p);
950 p->id = strdup(logsrv->ring_name);
951 p->conf.args.file = p->conf.file = strdup(logsrv->conf.file);
952 p->conf.args.line = p->conf.line = logsrv->conf.line;
953
954 /* allocate a new server to forward messages
955 * from ring buffer
956 */
957 srv = new_server(p);
958 if (!srv)
959 goto error;
960
961 /* init server */
962 srv->id = strdup(logsrv->ring_name);
963 srv->conf.file = strdup(logsrv->conf.file);
964 srv->conf.line = logsrv->conf.line;
965 srv->addr = logsrv->addr;
966 srv->svc_port = get_host_port(&logsrv->addr);
967 HA_SPIN_INIT(&srv->lock);
968
969 /* process per thread init */
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +0200970 if (srv_init_per_thr(srv) == -1)
Emeric Brun94aab062021-04-02 10:41:36 +0200971 goto error;
972
Emeric Brun94aab062021-04-02 10:41:36 +0200973 /* the servers are linked backwards
974 * first into proxy
975 */
976 p->srv = srv;
977 srv->next = p->srv;
978
979 /* allocate sink_forward_target descriptor */
980 sft = calloc(1, sizeof(*sft));
981 if (!sft)
982 goto error;
983
984 /* init sink_forward_target offset */
985 sft->srv = srv;
986 sft->appctx = NULL;
987 sft->ofs = ~0;
988 HA_SPIN_INIT(&sft->lock);
989
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +0500990 /* prepare description for the sink */
Emeric Brun94aab062021-04-02 10:41:36 +0200991 chunk_reset(&trash);
992 chunk_printf(&trash, "created from logserver declared into '%s' at line %d", logsrv->conf.file, logsrv->conf.line);
993
994 /* allocate a new sink buffer */
995 sink = sink_new_buf(logsrv->ring_name, trash.area, logsrv->format, BUFSIZE);
996 if (!sink || sink->type != SINK_TYPE_BUFFER) {
997 goto error;
998 }
999
1000 /* link sink_forward_target to proxy */
1001 sink->forward_px = p;
1002 p->parent = sink;
1003
1004 /* insert into sink_forward_targets
1005 * list into sink
1006 */
1007 sft->next = sink->sft;
1008 sink->sft = sft;
1009
1010 /* mark server as an attached reader to the ring */
1011 if (!ring_attach(sink->ctx.ring)) {
1012 /* should never fail since there is
1013 * only one reader
1014 */
1015 goto error;
1016 }
1017
1018 /* initialize sink buffer forwarding */
1019 if (!sink_init_forward(sink))
1020 goto error;
1021
1022 /* reset familyt of logsrv to consider the ring buffer target */
1023 logsrv->addr.ss_family = AF_UNSPEC;
1024
1025 return sink;
1026error:
1027 if (p) {
1028 if (p->id)
1029 free(p->id);
1030 if (p->conf.file)
1031 free(p->conf.file);
1032
1033 free(p);
1034 }
1035
1036 if (srv) {
1037 if (srv->id)
1038 free(srv->id);
1039 if (srv->conf.file)
1040 free((void *)srv->conf.file);
1041 if (srv->per_thr)
1042 free(srv->per_thr);
1043 free(srv);
1044 }
1045
1046 if (sft)
1047 free(sft);
1048
1049 if (sink) {
1050 if (sink->ctx.ring)
1051 ring_free(sink->ctx.ring);
1052
Willy Tarreau2b718102021-04-21 07:32:39 +02001053 LIST_DELETE(&sink->sink_list);
Emeric Brun94aab062021-04-02 10:41:36 +02001054 free(sink->name);
1055 free(sink->desc);
1056 free(sink);
1057 }
1058
1059 return NULL;
1060}
1061
Emeric Brun99c453d2020-05-25 15:01:04 +02001062/*
1063 * Post parsing "ring" section.
1064 *
1065 * The function returns 0 in success case, otherwise, it returns error
1066 * flags.
1067 */
1068int cfg_post_parse_ring()
1069{
1070 int err_code = 0;
Emeric Brun494c5052020-05-28 11:13:15 +02001071 struct server *srv;
Emeric Brun99c453d2020-05-25 15:01:04 +02001072
1073 if (cfg_sink && (cfg_sink->type == SINK_TYPE_BUFFER)) {
1074 if (cfg_sink->maxlen > b_size(&cfg_sink->ctx.ring->buf)) {
1075 ha_warning("ring '%s' event max length '%u' exceeds size, forced to size '%lu'.\n",
Willy Tarreau570a22b2020-06-02 12:00:46 +02001076 cfg_sink->name, cfg_sink->maxlen, (unsigned long)b_size(&cfg_sink->ctx.ring->buf));
Emeric Brun99c453d2020-05-25 15:01:04 +02001077 cfg_sink->maxlen = b_size(&cfg_sink->ctx.ring->buf);
1078 err_code |= ERR_ALERT;
1079 }
Emeric Brun494c5052020-05-28 11:13:15 +02001080
1081 /* prepare forward server descriptors */
1082 if (cfg_sink->forward_px) {
1083 srv = cfg_sink->forward_px->srv;
1084 while (srv) {
1085 struct sink_forward_target *sft;
1086 /* init ssl if needed */
1087 if (srv->use_ssl == 1 && xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) {
1088 if (xprt_get(XPRT_SSL)->prepare_srv(srv)) {
1089 ha_alert("unable to prepare SSL for server '%s' in ring '%s'.\n", srv->id, cfg_sink->name);
1090 err_code |= ERR_ALERT | ERR_FATAL;
1091 }
1092 }
Emeric Brun99c453d2020-05-25 15:01:04 +02001093
Emeric Brun494c5052020-05-28 11:13:15 +02001094 /* allocate sink_forward_target descriptor */
1095 sft = calloc(1, sizeof(*sft));
1096 if (!sft) {
1097 ha_alert("memory allocation error initializing server '%s' in ring '%s'.\n",srv->id, cfg_sink->name);
1098 err_code |= ERR_ALERT | ERR_FATAL;
1099 break;
1100 }
1101 sft->srv = srv;
1102 sft->appctx = NULL;
1103 sft->ofs = ~0; /* init ring offset */
1104 sft->next = cfg_sink->sft;
1105 HA_SPIN_INIT(&sft->lock);
1106
1107 /* mark server attached to the ring */
1108 if (!ring_attach(cfg_sink->ctx.ring)) {
1109 ha_alert("server '%s' sets too many watchers > 255 on ring '%s'.\n", srv->id, cfg_sink->name);
1110 err_code |= ERR_ALERT | ERR_FATAL;
1111 }
1112 cfg_sink->sft = sft;
1113 srv = srv->next;
1114 }
1115 sink_init_forward(cfg_sink);
1116 }
1117 }
Emeric Brun99c453d2020-05-25 15:01:04 +02001118 cfg_sink = NULL;
1119
1120 return err_code;
1121}
1122
1123/* resolve sink names at end of config. Returns 0 on success otherwise error
1124 * flags.
1125*/
1126int post_sink_resolve()
1127{
Christopher Fauletfc633b62020-11-06 15:24:23 +01001128 int err_code = ERR_NONE;
Emeric Brun99c453d2020-05-25 15:01:04 +02001129 struct logsrv *logsrv, *logb;
1130 struct sink *sink;
1131 struct proxy *px;
1132
1133 list_for_each_entry_safe(logsrv, logb, &global.logsrvs, list) {
1134 if (logsrv->type == LOG_TARGET_BUFFER) {
1135 sink = sink_find(logsrv->ring_name);
Emeric Brun94aab062021-04-02 10:41:36 +02001136 if (!sink) {
1137 /* LOG_TARGET_BUFFER but !AF_UNSPEC
1138 * means we must allocate a sink
1139 * buffer to send messages to this logsrv
1140 */
1141 if (logsrv->addr.ss_family != AF_UNSPEC) {
1142 sink = sink_new_from_logsrv(logsrv);
1143 if (!sink) {
1144 ha_alert("global stream log server declared in file '%s' at line %d cannot be initialized'.\n",
1145 logsrv->conf.file, logsrv->conf.line);
1146 err_code |= ERR_ALERT | ERR_FATAL;
1147 }
1148 }
1149 else {
1150 ha_alert("global log server declared in file '%s' at line %d uses unknown ring named '%s'.\n",
1151 logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
1152 err_code |= ERR_ALERT | ERR_FATAL;
1153 }
1154 }
1155 else if (sink->type != SINK_TYPE_BUFFER) {
1156 ha_alert("global log server declared in file '%s' at line %d uses incompatible ring '%s'.\n",
1157 logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
Emeric Brun99c453d2020-05-25 15:01:04 +02001158 err_code |= ERR_ALERT | ERR_FATAL;
1159 }
1160 logsrv->sink = sink;
1161 }
Emeric Brun94aab062021-04-02 10:41:36 +02001162
Emeric Brun99c453d2020-05-25 15:01:04 +02001163 }
1164
1165 for (px = proxies_list; px; px = px->next) {
1166 list_for_each_entry_safe(logsrv, logb, &px->logsrvs, list) {
1167 if (logsrv->type == LOG_TARGET_BUFFER) {
1168 sink = sink_find(logsrv->ring_name);
Emeric Brun94aab062021-04-02 10:41:36 +02001169 if (!sink) {
1170 /* LOG_TARGET_BUFFER but !AF_UNSPEC
1171 * means we must allocate a sink
1172 * buffer to send messages to this logsrv
1173 */
1174 if (logsrv->addr.ss_family != AF_UNSPEC) {
1175 sink = sink_new_from_logsrv(logsrv);
1176 if (!sink) {
1177 ha_alert("log server declared in proxy section '%s' file '%s' at line %d cannot be initialized'.\n",
1178 px->id, logsrv->conf.file, logsrv->conf.line);
1179 err_code |= ERR_ALERT | ERR_FATAL;
1180 }
1181 }
1182 else {
1183 ha_alert("log server declared in proxy section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n",
1184 px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
1185 err_code |= ERR_ALERT | ERR_FATAL;
1186 }
1187 }
1188 else if (sink->type != SINK_TYPE_BUFFER) {
1189 ha_alert("log server declared in proxy section '%s' in file '%s' at line %d uses incomatible ring named '%s'.\n",
1190 px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
Emeric Brun99c453d2020-05-25 15:01:04 +02001191 err_code |= ERR_ALERT | ERR_FATAL;
1192 }
1193 logsrv->sink = sink;
1194 }
1195 }
1196 }
Emeric Brun12941c82020-07-07 14:19:42 +02001197
1198 for (px = cfg_log_forward; px; px = px->next) {
1199 list_for_each_entry_safe(logsrv, logb, &px->logsrvs, list) {
1200 if (logsrv->type == LOG_TARGET_BUFFER) {
1201 sink = sink_find(logsrv->ring_name);
Emeric Brun94aab062021-04-02 10:41:36 +02001202 if (!sink) {
1203 /* LOG_TARGET_BUFFER but !AF_UNSPEC
1204 * means we must allocate a sink
1205 * buffer to send messages to this logsrv
1206 */
1207 if (logsrv->addr.ss_family != AF_UNSPEC) {
1208 sink = sink_new_from_logsrv(logsrv);
1209 if (!sink) {
1210 ha_alert("log server declared in log-forward section '%s' file '%s' at line %d cannot be initialized'.\n",
1211 px->id, logsrv->conf.file, logsrv->conf.line);
1212 err_code |= ERR_ALERT | ERR_FATAL;
1213 }
1214 }
1215 else {
1216 ha_alert("log server declared in log-forward section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n",
1217 px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
1218 err_code |= ERR_ALERT | ERR_FATAL;
1219 }
1220 }
1221 else if (sink->type != SINK_TYPE_BUFFER) {
1222 ha_alert("log server declared in log-forward section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n",
1223 px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
Emeric Brun12941c82020-07-07 14:19:42 +02001224 err_code |= ERR_ALERT | ERR_FATAL;
1225 }
1226 logsrv->sink = sink;
1227 }
1228 }
1229 }
Emeric Brun99c453d2020-05-25 15:01:04 +02001230 return err_code;
1231}
1232
1233
Willy Tarreau973e6622019-08-20 11:57:52 +02001234static void sink_init()
1235{
Emeric Brun54648852020-07-06 15:54:06 +02001236 sink_new_fd("stdout", "standard output (fd#1)", LOG_FORMAT_RAW, 1);
1237 sink_new_fd("stderr", "standard output (fd#2)", LOG_FORMAT_RAW, 2);
1238 sink_new_buf("buf0", "in-memory ring buffer", LOG_FORMAT_TIMED, 1048576);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +02001239}
1240
1241static void sink_deinit()
1242{
1243 struct sink *sink, *sb;
1244
1245 list_for_each_entry_safe(sink, sb, &sink_list, sink_list) {
1246 if (sink->type == SINK_TYPE_BUFFER)
1247 ring_free(sink->ctx.ring);
Willy Tarreau2b718102021-04-21 07:32:39 +02001248 LIST_DELETE(&sink->sink_list);
Emeric Brun99c453d2020-05-25 15:01:04 +02001249 free(sink->name);
1250 free(sink->desc);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +02001251 free(sink);
1252 }
Willy Tarreau973e6622019-08-20 11:57:52 +02001253}
1254
1255INITCALL0(STG_REGISTER, sink_init);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +02001256REGISTER_POST_DEINIT(sink_deinit);
Willy Tarreau973e6622019-08-20 11:57:52 +02001257
Willy Tarreau9f830d72019-08-26 18:17:04 +02001258static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001259 { { "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 +02001260 {{},}
1261}};
1262
1263INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
1264
Emeric Brun99c453d2020-05-25 15:01:04 +02001265/* config parsers for this section */
1266REGISTER_CONFIG_SECTION("ring", cfg_parse_ring, cfg_post_parse_ring);
1267REGISTER_POST_CHECK(post_sink_resolve);
1268
Willy Tarreau67b5a162019-08-11 16:38:56 +02001269/*
1270 * Local variables:
1271 * c-indent-level: 8
1272 * c-basic-offset: 8
1273 * End:
1274 */