blob: 9f0bfb8bf8d1ff3dd1eee03d2b0d8d394a9eb697 [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 Tarreaucba88382022-05-05 15:18:57 +0200237 uint ring_flags;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200238 int arg;
Willy Tarreau9f830d72019-08-26 18:17:04 +0200239
240 args++; // make args[1] the 1st arg
241
242 if (!*args[1]) {
243 /* no arg => report the list of supported sink */
Willy Tarreau1d181e42019-08-30 11:17:01 +0200244 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 +0200245 list_for_each_entry(sink, &sink_list, sink_list) {
246 chunk_appendf(&trash, " %-10s : type=%s, %u dropped, %s\n",
247 sink->name,
248 sink->type == SINK_TYPE_NEW ? "init" :
249 sink->type == SINK_TYPE_FD ? "fd" :
250 sink->type == SINK_TYPE_BUFFER ? "buffer" : "?",
251 sink->ctx.dropped, sink->desc);
252 }
253
254 trash.area[trash.data] = 0;
255 return cli_msg(appctx, LOG_WARNING, trash.area);
256 }
257
258 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
259 return 1;
260
261 sink = sink_find(args[1]);
262 if (!sink)
263 return cli_err(appctx, "No such event sink");
264
265 if (sink->type != SINK_TYPE_BUFFER)
266 return cli_msg(appctx, LOG_NOTICE, "Nothing to report for this sink");
267
Willy Tarreaucba88382022-05-05 15:18:57 +0200268 ring_flags = 0;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200269 for (arg = 2; *args[arg]; arg++) {
270 if (strcmp(args[arg], "-w") == 0)
Willy Tarreaucba88382022-05-05 15:18:57 +0200271 ring_flags |= RING_WF_WAIT_MODE;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200272 else if (strcmp(args[arg], "-n") == 0)
Willy Tarreaucba88382022-05-05 15:18:57 +0200273 ring_flags |= RING_WF_SEEK_NEW;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200274 else if (strcmp(args[arg], "-nw") == 0 || strcmp(args[arg], "-wn") == 0)
Willy Tarreaucba88382022-05-05 15:18:57 +0200275 ring_flags |= RING_WF_WAIT_MODE | RING_WF_SEEK_NEW;
Willy Tarreau1d181e42019-08-30 11:17:01 +0200276 else
277 return cli_err(appctx, "unknown option");
278 }
Willy Tarreaucba88382022-05-05 15:18:57 +0200279 return ring_attach_cli(sink->ctx.ring, appctx, ring_flags);
Willy Tarreau9f830d72019-08-26 18:17:04 +0200280}
281
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500282/* Pre-configures a ring proxy to emit connections */
Emeric Brun494c5052020-05-28 11:13:15 +0200283void sink_setup_proxy(struct proxy *px)
284{
285 px->last_change = now.tv_sec;
286 px->cap = PR_CAP_FE | PR_CAP_BE;
287 px->maxconn = 0;
288 px->conn_retries = 1;
289 px->timeout.server = TICK_ETERNITY;
290 px->timeout.client = TICK_ETERNITY;
291 px->timeout.connect = TICK_ETERNITY;
292 px->accept = NULL;
293 px->options2 |= PR_O2_INDEPSTR | PR_O2_SMARTCON | PR_O2_SMARTACC;
Emeric Brun494c5052020-05-28 11:13:15 +0200294}
295
296/*
Willy Tarreau42cc8312022-05-04 20:42:23 +0200297 * IO Handler to handle message push to syslog tcp server.
298 * It takes its context from appctx->svcctx.
Emeric Brun494c5052020-05-28 11:13:15 +0200299 */
300static void sink_forward_io_handler(struct appctx *appctx)
301{
Christopher Faulet908628c2022-03-25 16:43:49 +0100302 struct conn_stream *cs = appctx->owner;
303 struct stream *s = __cs_strm(cs);
Emeric Brun494c5052020-05-28 11:13:15 +0200304 struct sink *sink = strm_fe(s)->parent;
Willy Tarreau42cc8312022-05-04 20:42:23 +0200305 struct sink_forward_target *sft = appctx->svcctx;
Emeric Brun494c5052020-05-28 11:13:15 +0200306 struct ring *ring = sink->ctx.ring;
307 struct buffer *buf = &ring->buf;
308 uint64_t msg_len;
309 size_t len, cnt, ofs;
310 int ret = 0;
311
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500312 /* if stopping was requested, close immediately */
Emeric Brun494c5052020-05-28 11:13:15 +0200313 if (unlikely(stopping))
314 goto close;
315
316 /* for rex because it seems reset to timeout
317 * and we don't want expire on this case
318 * with a syslog server
319 */
Christopher Faulet908628c2022-03-25 16:43:49 +0100320 cs_oc(cs)->rex = TICK_ETERNITY;
Emeric Brun494c5052020-05-28 11:13:15 +0200321 /* rto should not change but it seems the case */
Christopher Faulet908628c2022-03-25 16:43:49 +0100322 cs_oc(cs)->rto = TICK_ETERNITY;
Emeric Brun494c5052020-05-28 11:13:15 +0200323
324 /* an error was detected */
Christopher Faulet908628c2022-03-25 16:43:49 +0100325 if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Emeric Brun494c5052020-05-28 11:13:15 +0200326 goto close;
327
328 /* con closed by server side */
Christopher Faulet908628c2022-03-25 16:43:49 +0100329 if ((cs_oc(cs)->flags & CF_SHUTW))
Emeric Brun494c5052020-05-28 11:13:15 +0200330 goto close;
331
332 /* if the connection is not established, inform the stream that we want
333 * to be notified whenever the connection completes.
334 */
Christopher Faulet62e75742022-03-31 09:16:34 +0200335 if (cs_opposite(cs)->state < CS_ST_EST) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200336 cs_cant_get(cs);
337 cs_rx_conn_blk(cs);
338 cs_rx_endp_more(cs);
Emeric Brun494c5052020-05-28 11:13:15 +0200339 return;
340 }
341
342 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
343 if (appctx != sft->appctx) {
344 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
345 goto close;
346 }
347 ofs = sft->ofs;
348
349 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
350 LIST_DEL_INIT(&appctx->wait_entry);
351 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
352
353 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
354
355 /* explanation for the initialization below: it would be better to do
356 * this in the parsing function but this would occasionally result in
357 * dropped events because we'd take a reference on the oldest message
358 * and keep it while being scheduled. Thus instead let's take it the
359 * first time we enter here so that we have a chance to pass many
360 * existing messages before grabbing a reference to a location. This
361 * value cannot be produced after initialization.
362 */
363 if (unlikely(ofs == ~0)) {
364 ofs = 0;
365
Willy Tarreau4781b152021-04-06 13:53:36 +0200366 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun494c5052020-05-28 11:13:15 +0200367 ofs += ring->ofs;
368 }
369
Emeric Brun494c5052020-05-28 11:13:15 +0200370 /* in this loop, ofs always points to the counter byte that precedes
371 * the message so that we can take our reference there if we have to
372 * stop before the end (ret=0).
373 */
Christopher Faulet62e75742022-03-31 09:16:34 +0200374 if (cs_opposite(cs)->state == CS_ST_EST) {
Emeric Brunfdabf492020-12-02 17:02:09 +0100375 /* we were already there, adjust the offset to be relative to
376 * the buffer's head and remove us from the counter.
377 */
378 ofs -= ring->ofs;
379 BUG_ON(ofs >= buf->size);
Willy Tarreau4781b152021-04-06 13:53:36 +0200380 HA_ATOMIC_DEC(b_peek(buf, ofs));
Emeric Brunfdabf492020-12-02 17:02:09 +0100381
Emeric Brun494c5052020-05-28 11:13:15 +0200382 ret = 1;
383 while (ofs + 1 < b_data(buf)) {
384 cnt = 1;
385 len = b_peek_varint(buf, ofs + cnt, &msg_len);
386 if (!len)
387 break;
388 cnt += len;
389 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
390
391 if (unlikely(msg_len + 1 > b_size(&trash))) {
392 /* too large a message to ever fit, let's skip it */
393 ofs += cnt + msg_len;
394 continue;
395 }
396
397 chunk_reset(&trash);
398 len = b_getblk(buf, trash.area, msg_len, ofs + cnt);
399 trash.data += len;
400 trash.area[trash.data++] = '\n';
401
Christopher Faulet908628c2022-03-25 16:43:49 +0100402 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200403 cs_rx_room_blk(cs);
Emeric Brun494c5052020-05-28 11:13:15 +0200404 ret = 0;
405 break;
406 }
407 ofs += cnt + msg_len;
408 }
409
Willy Tarreau4781b152021-04-06 13:53:36 +0200410 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun494c5052020-05-28 11:13:15 +0200411 ofs += ring->ofs;
412 sft->ofs = ofs;
413 }
414 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
415
416 if (ret) {
417 /* let's be woken up once new data arrive */
418 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau2b718102021-04-21 07:32:39 +0200419 LIST_APPEND(&ring->waiters, &appctx->wait_entry);
Emeric Brun494c5052020-05-28 11:13:15 +0200420 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200421 cs_rx_endp_done(cs);
Emeric Brun494c5052020-05-28 11:13:15 +0200422 }
423 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
424
425 /* always drain data from server */
Christopher Faulet908628c2022-03-25 16:43:49 +0100426 co_skip(cs_oc(cs), cs_oc(cs)->output);
Emeric Brun494c5052020-05-28 11:13:15 +0200427 return;
428
429close:
Christopher Fauletda098e62022-03-31 17:44:45 +0200430 cs_shutw(cs);
431 cs_shutr(cs);
Christopher Faulet908628c2022-03-25 16:43:49 +0100432 cs_ic(cs)->flags |= CF_READ_NULL;
Emeric Brun494c5052020-05-28 11:13:15 +0200433}
434
Emeric Brun97556472020-05-30 01:42:45 +0200435/*
436 * IO Handler to handle message push to syslog tcp server
437 * using octet counting frames
Willy Tarreau42cc8312022-05-04 20:42:23 +0200438 * It takes its context from appctx->svcctx.
Emeric Brun97556472020-05-30 01:42:45 +0200439 */
440static void sink_forward_oc_io_handler(struct appctx *appctx)
441{
Christopher Faulet908628c2022-03-25 16:43:49 +0100442 struct conn_stream *cs = appctx->owner;
443 struct stream *s = __cs_strm(cs);
Emeric Brun97556472020-05-30 01:42:45 +0200444 struct sink *sink = strm_fe(s)->parent;
Willy Tarreau42cc8312022-05-04 20:42:23 +0200445 struct sink_forward_target *sft = appctx->svcctx;
Emeric Brun97556472020-05-30 01:42:45 +0200446 struct ring *ring = sink->ctx.ring;
447 struct buffer *buf = &ring->buf;
448 uint64_t msg_len;
449 size_t len, cnt, ofs;
450 int ret = 0;
451 char *p;
452
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500453 /* if stopping was requested, close immediately */
Emeric Brun97556472020-05-30 01:42:45 +0200454 if (unlikely(stopping))
455 goto close;
456
457 /* for rex because it seems reset to timeout
458 * and we don't want expire on this case
459 * with a syslog server
460 */
Christopher Faulet908628c2022-03-25 16:43:49 +0100461 cs_oc(cs)->rex = TICK_ETERNITY;
Emeric Brun97556472020-05-30 01:42:45 +0200462 /* rto should not change but it seems the case */
Christopher Faulet908628c2022-03-25 16:43:49 +0100463 cs_oc(cs)->rto = TICK_ETERNITY;
Emeric Brun97556472020-05-30 01:42:45 +0200464
465 /* an error was detected */
Christopher Faulet908628c2022-03-25 16:43:49 +0100466 if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Emeric Brun97556472020-05-30 01:42:45 +0200467 goto close;
468
469 /* con closed by server side */
Christopher Faulet908628c2022-03-25 16:43:49 +0100470 if ((cs_oc(cs)->flags & CF_SHUTW))
Emeric Brun97556472020-05-30 01:42:45 +0200471 goto close;
472
473 /* if the connection is not established, inform the stream that we want
474 * to be notified whenever the connection completes.
475 */
Christopher Faulet62e75742022-03-31 09:16:34 +0200476 if (cs_opposite(cs)->state < CS_ST_EST) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200477 cs_cant_get(cs);
478 cs_rx_conn_blk(cs);
479 cs_rx_endp_more(cs);
Emeric Brun97556472020-05-30 01:42:45 +0200480 return;
481 }
482
483 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
484 if (appctx != sft->appctx) {
485 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
486 goto close;
487 }
488 ofs = sft->ofs;
489
490 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
491 LIST_DEL_INIT(&appctx->wait_entry);
492 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
493
494 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
495
496 /* explanation for the initialization below: it would be better to do
497 * this in the parsing function but this would occasionally result in
498 * dropped events because we'd take a reference on the oldest message
499 * and keep it while being scheduled. Thus instead let's take it the
500 * first time we enter here so that we have a chance to pass many
501 * existing messages before grabbing a reference to a location. This
502 * value cannot be produced after initialization.
503 */
504 if (unlikely(ofs == ~0)) {
505 ofs = 0;
506
Willy Tarreau4781b152021-04-06 13:53:36 +0200507 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun97556472020-05-30 01:42:45 +0200508 ofs += ring->ofs;
509 }
510
Emeric Brun97556472020-05-30 01:42:45 +0200511 /* in this loop, ofs always points to the counter byte that precedes
512 * the message so that we can take our reference there if we have to
513 * stop before the end (ret=0).
514 */
Christopher Faulet62e75742022-03-31 09:16:34 +0200515 if (cs_opposite(cs)->state == CS_ST_EST) {
Emeric Brunfdabf492020-12-02 17:02:09 +0100516 /* we were already there, adjust the offset to be relative to
517 * the buffer's head and remove us from the counter.
518 */
519 ofs -= ring->ofs;
520 BUG_ON(ofs >= buf->size);
Willy Tarreau4781b152021-04-06 13:53:36 +0200521 HA_ATOMIC_DEC(b_peek(buf, ofs));
Emeric Brunfdabf492020-12-02 17:02:09 +0100522
Emeric Brun97556472020-05-30 01:42:45 +0200523 ret = 1;
524 while (ofs + 1 < b_data(buf)) {
525 cnt = 1;
526 len = b_peek_varint(buf, ofs + cnt, &msg_len);
527 if (!len)
528 break;
529 cnt += len;
530 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
531
532 chunk_reset(&trash);
533 p = ulltoa(msg_len, trash.area, b_size(&trash));
534 if (p) {
535 trash.data = (p - trash.area) + 1;
536 *p = ' ';
537 }
538
539 if (!p || (trash.data + msg_len > b_size(&trash))) {
540 /* too large a message to ever fit, let's skip it */
541 ofs += cnt + msg_len;
542 continue;
543 }
544
545 trash.data += b_getblk(buf, p + 1, msg_len, ofs + cnt);
546
Christopher Faulet908628c2022-03-25 16:43:49 +0100547 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200548 cs_rx_room_blk(cs);
Emeric Brun97556472020-05-30 01:42:45 +0200549 ret = 0;
550 break;
551 }
552 ofs += cnt + msg_len;
553 }
554
Willy Tarreau4781b152021-04-06 13:53:36 +0200555 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun97556472020-05-30 01:42:45 +0200556 ofs += ring->ofs;
557 sft->ofs = ofs;
558 }
559 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
560
561 if (ret) {
562 /* let's be woken up once new data arrive */
563 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau2b718102021-04-21 07:32:39 +0200564 LIST_APPEND(&ring->waiters, &appctx->wait_entry);
Emeric Brun97556472020-05-30 01:42:45 +0200565 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
Christopher Fauleta0bdec32022-04-04 07:51:21 +0200566 cs_rx_endp_done(cs);
Emeric Brun97556472020-05-30 01:42:45 +0200567 }
568 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
569
570 /* always drain data from server */
Christopher Faulet908628c2022-03-25 16:43:49 +0100571 co_skip(cs_oc(cs), cs_oc(cs)->output);
Emeric Brun97556472020-05-30 01:42:45 +0200572 return;
573
574close:
Christopher Fauletda098e62022-03-31 17:44:45 +0200575 cs_shutw(cs);
576 cs_shutr(cs);
Christopher Faulet908628c2022-03-25 16:43:49 +0100577 cs_ic(cs)->flags |= CF_READ_NULL;
Emeric Brun97556472020-05-30 01:42:45 +0200578}
579
Emeric Brun494c5052020-05-28 11:13:15 +0200580void __sink_forward_session_deinit(struct sink_forward_target *sft)
581{
Christopher Faulet908628c2022-03-25 16:43:49 +0100582 struct stream *s = __cs_strm(sft->appctx->owner);
Emeric Brun494c5052020-05-28 11:13:15 +0200583 struct sink *sink;
584
Emeric Brun494c5052020-05-28 11:13:15 +0200585 sink = strm_fe(s)->parent;
586 if (!sink)
587 return;
588
589 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &sink->ctx.ring->lock);
590 LIST_DEL_INIT(&sft->appctx->wait_entry);
591 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &sink->ctx.ring->lock);
592
593 sft->appctx = NULL;
594 task_wakeup(sink->forward_task, TASK_WOKEN_MSG);
595}
596
597
598static void sink_forward_session_release(struct appctx *appctx)
599{
Willy Tarreau42cc8312022-05-04 20:42:23 +0200600 struct sink_forward_target *sft = appctx->svcctx;
Emeric Brun494c5052020-05-28 11:13:15 +0200601
602 if (!sft)
603 return;
604
605 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
606 if (sft->appctx == appctx)
607 __sink_forward_session_deinit(sft);
608 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
609}
610
611static struct applet sink_forward_applet = {
612 .obj_type = OBJ_TYPE_APPLET,
613 .name = "<SINKFWD>", /* used for logging */
614 .fct = sink_forward_io_handler,
615 .release = sink_forward_session_release,
616};
617
Emeric Brun97556472020-05-30 01:42:45 +0200618static struct applet sink_forward_oc_applet = {
619 .obj_type = OBJ_TYPE_APPLET,
620 .name = "<SINKFWDOC>", /* used for logging */
621 .fct = sink_forward_oc_io_handler,
622 .release = sink_forward_session_release,
623};
624
Emeric Brun494c5052020-05-28 11:13:15 +0200625/*
626 * Create a new peer session in assigned state (connect will start automatically)
Willy Tarreau42cc8312022-05-04 20:42:23 +0200627 * It sets its context into appctx->svcctx.
Emeric Brun494c5052020-05-28 11:13:15 +0200628 */
629static struct appctx *sink_forward_session_create(struct sink *sink, struct sink_forward_target *sft)
630{
631 struct proxy *p = sink->forward_px;
632 struct appctx *appctx;
633 struct session *sess;
Christopher Faulet13a35e52021-12-20 15:34:16 +0100634 struct conn_stream *cs;
Emeric Brun494c5052020-05-28 11:13:15 +0200635 struct stream *s;
Emeric Brun97556472020-05-30 01:42:45 +0200636 struct applet *applet = &sink_forward_applet;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100637 struct sockaddr_storage *addr = NULL;
Emeric Brun97556472020-05-30 01:42:45 +0200638
639 if (sft->srv->log_proto == SRV_LOG_PROTO_OCTET_COUNTING)
640 applet = &sink_forward_oc_applet;
Emeric Brun494c5052020-05-28 11:13:15 +0200641
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100642 appctx = appctx_new(applet, NULL);
Christopher Faulet2479e5f2022-01-19 14:50:11 +0100643 if (!appctx)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100644 goto out_close;
Emeric Brun494c5052020-05-28 11:13:15 +0200645
Willy Tarreau42cc8312022-05-04 20:42:23 +0200646 appctx->svcctx = (void *)sft;
Emeric Brun494c5052020-05-28 11:13:15 +0200647
648 sess = session_new(p, NULL, &appctx->obj_type);
649 if (!sess) {
Christopher Faulet13a35e52021-12-20 15:34:16 +0100650 ha_alert("out of memory in sink_forward_session_create().\n");
Emeric Brun494c5052020-05-28 11:13:15 +0200651 goto out_free_appctx;
652 }
653
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100654 if (!sockaddr_alloc(&addr, &sft->srv->addr, sizeof(sft->srv->addr)))
Christopher Faulet2479e5f2022-01-19 14:50:11 +0100655 goto out_free_sess;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100656
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100657 cs = cs_new_from_applet(appctx->endp, sess, &BUF_NULL);
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100658 if (!cs) {
659 ha_alert("Failed to initialize stream in sink_forward_session_create().\n");
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100660 goto out_free_addr;
Christopher Faulet13a35e52021-12-20 15:34:16 +0100661 }
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100662 s = DISGUISE(cs_strm(cs));
Christopher Faulet13a35e52021-12-20 15:34:16 +0100663
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200664 s->csb->dst = addr;
Christopher Faulet8abe7122022-03-30 15:10:18 +0200665 s->csb->flags |= CS_FL_NOLINGER;
Emeric Brun494c5052020-05-28 11:13:15 +0200666
667 s->target = &sft->srv->obj_type;
Willy Tarreau03bd3952022-05-02 16:36:47 +0200668 s->flags = SF_ASSIGNED;
Emeric Brun494c5052020-05-28 11:13:15 +0200669
670 s->do_log = NULL;
671 s->uniq_id = 0;
672
673 s->res.flags |= CF_READ_DONTWAIT;
674 /* for rto and rex to eternity to not expire on idle recv:
675 * We are using a syslog server.
676 */
677 s->res.rto = TICK_ETERNITY;
678 s->res.rex = TICK_ETERNITY;
679 sft->appctx = appctx;
Emeric Brun494c5052020-05-28 11:13:15 +0200680 return appctx;
681
682 /* Error unrolling */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100683 out_free_addr:
684 sockaddr_free(&addr);
Emeric Brun494c5052020-05-28 11:13:15 +0200685 out_free_sess:
686 session_free(sess);
687 out_free_appctx:
688 appctx_free(appctx);
689 out_close:
690 return NULL;
691}
692
693/*
694 * Task to handle connctions to forward servers
695 */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100696static struct task *process_sink_forward(struct task * task, void *context, unsigned int state)
Emeric Brun494c5052020-05-28 11:13:15 +0200697{
698 struct sink *sink = (struct sink *)context;
699 struct sink_forward_target *sft = sink->sft;
700
701 task->expire = TICK_ETERNITY;
702
703 if (!stopping) {
704 while (sft) {
705 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
706 /* if appctx is NULL, start a new session */
707 if (!sft->appctx)
708 sft->appctx = sink_forward_session_create(sink, sft);
709 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
710 sft = sft->next;
711 }
712 }
713 else {
714 while (sft) {
715 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
716 /* awake applet to perform a clean close */
717 if (sft->appctx)
718 appctx_wakeup(sft->appctx);
719 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
720 sft = sft->next;
721 }
722 }
723
724 return task;
725}
726/*
727 * Init task to manage connctions to forward servers
728 *
729 * returns 0 in case of error.
730 */
731int sink_init_forward(struct sink *sink)
732{
Willy Tarreaubeeabf52021-10-01 18:23:30 +0200733 sink->forward_task = task_new_anywhere();
Emeric Brun494c5052020-05-28 11:13:15 +0200734 if (!sink->forward_task)
735 return 0;
736
737 sink->forward_task->process = process_sink_forward;
738 sink->forward_task->context = (void *)sink;
739 sink->forward_sighandler = signal_register_task(0, sink->forward_task, 0);
740 task_wakeup(sink->forward_task, TASK_WOKEN_INIT);
741 return 1;
742}
Emeric Brun99c453d2020-05-25 15:01:04 +0200743/*
744 * Parse "ring" section and create corresponding sink buffer.
745 *
746 * The function returns 0 in success case, otherwise, it returns error
747 * flags.
748 */
749int cfg_parse_ring(const char *file, int linenum, char **args, int kwm)
750{
751 int err_code = 0;
752 const char *inv;
753 size_t size = BUFSIZE;
Emeric Brun494c5052020-05-28 11:13:15 +0200754 struct proxy *p;
Emeric Brun99c453d2020-05-25 15:01:04 +0200755
756 if (strcmp(args[0], "ring") == 0) { /* new peers section */
757 if (!*args[1]) {
758 ha_alert("parsing [%s:%d] : missing ring name.\n", file, linenum);
759 err_code |= ERR_ALERT | ERR_FATAL;
760 goto err;
761 }
762
763 inv = invalid_char(args[1]);
764 if (inv) {
765 ha_alert("parsing [%s:%d] : invalid ring name '%s' (character '%c' is not permitted).\n", file, linenum, args[1], *inv);
766 err_code |= ERR_ALERT | ERR_FATAL;
767 goto err;
768 }
769
770 if (sink_find(args[1])) {
771 ha_alert("parsing [%s:%d] : sink named '%s' already exists.\n", file, linenum, args[1]);
772 err_code |= ERR_ALERT | ERR_FATAL;
773 goto err;
774 }
775
Emeric Brun54648852020-07-06 15:54:06 +0200776 cfg_sink = sink_new_buf(args[1], args[1], LOG_FORMAT_RAW, size);
Emeric Brun99c453d2020-05-25 15:01:04 +0200777 if (!cfg_sink || cfg_sink->type != SINK_TYPE_BUFFER) {
778 ha_alert("parsing [%s:%d] : unable to create a new sink buffer for ring '%s'.\n", file, linenum, args[1]);
779 err_code |= ERR_ALERT | ERR_FATAL;
780 goto err;
781 }
Emeric Brun494c5052020-05-28 11:13:15 +0200782
783 /* allocate new proxy to handle forwards */
784 p = calloc(1, sizeof *p);
785 if (!p) {
786 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
787 err_code |= ERR_ALERT | ERR_FATAL;
788 goto err;
789 }
790
791 init_new_proxy(p);
792 sink_setup_proxy(p);
793 p->parent = cfg_sink;
794 p->id = strdup(args[1]);
795 p->conf.args.file = p->conf.file = strdup(file);
796 p->conf.args.line = p->conf.line = linenum;
797 cfg_sink->forward_px = p;
Emeric Brun99c453d2020-05-25 15:01:04 +0200798 }
799 else if (strcmp(args[0], "size") == 0) {
800 size = atol(args[1]);
801 if (!size) {
802 ha_alert("parsing [%s:%d] : invalid size '%s' for new sink buffer.\n", file, linenum, args[1]);
803 err_code |= ERR_ALERT | ERR_FATAL;
804 goto err;
805 }
806
807 if (!cfg_sink || (cfg_sink->type != SINK_TYPE_BUFFER)
808 || !ring_resize(cfg_sink->ctx.ring, size)) {
809 ha_alert("parsing [%s:%d] : fail to set sink buffer size '%s'.\n", file, linenum, args[1]);
810 err_code |= ERR_ALERT | ERR_FATAL;
811 goto err;
812 }
813 }
Emeric Brun494c5052020-05-28 11:13:15 +0200814 else if (strcmp(args[0],"server") == 0) {
Amaury Denoyelle30c05372021-03-08 16:36:46 +0100815 err_code |= parse_server(file, linenum, args, cfg_sink->forward_px, NULL,
816 SRV_PARSE_PARSE_ADDR|SRV_PARSE_INITIAL_RESOLVE);
Emeric Brun494c5052020-05-28 11:13:15 +0200817 }
818 else if (strcmp(args[0],"timeout") == 0) {
819 if (!cfg_sink || !cfg_sink->forward_px) {
820 ha_alert("parsing [%s:%d] : unable to set timeout '%s'.\n", file, linenum, args[1]);
821 err_code |= ERR_ALERT | ERR_FATAL;
822 goto err;
823 }
824
825 if (strcmp(args[1], "connect") == 0 ||
826 strcmp(args[1], "server") == 0) {
827 const char *res;
828 unsigned int tout;
829
830 if (!*args[2]) {
831 ha_alert("parsing [%s:%d] : '%s %s' expects <time> as argument.\n",
832 file, linenum, args[0], args[1]);
833 err_code |= ERR_ALERT | ERR_FATAL;
834 goto err;
835 }
836 res = parse_time_err(args[2], &tout, TIME_UNIT_MS);
837 if (res == PARSE_TIME_OVER) {
838 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
839 file, linenum, args[2], args[0], args[1]);
840 err_code |= ERR_ALERT | ERR_FATAL;
841 goto err;
842 }
843 else if (res == PARSE_TIME_UNDER) {
844 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
845 file, linenum, args[2], args[0], args[1]);
846 err_code |= ERR_ALERT | ERR_FATAL;
847 goto err;
848 }
849 else if (res) {
850 ha_alert("parsing [%s:%d]: unexpected character '%c' in argument to <%s %s>.\n",
851 file, linenum, *res, args[0], args[1]);
852 err_code |= ERR_ALERT | ERR_FATAL;
853 goto err;
854 }
855 if (args[1][2] == 'c')
856 cfg_sink->forward_px->timeout.connect = tout;
857 else
858 cfg_sink->forward_px->timeout.server = tout;
859 }
860 }
Emeric Brun99c453d2020-05-25 15:01:04 +0200861 else if (strcmp(args[0],"format") == 0) {
862 if (!cfg_sink) {
863 ha_alert("parsing [%s:%d] : unable to set format '%s'.\n", file, linenum, args[1]);
864 err_code |= ERR_ALERT | ERR_FATAL;
865 goto err;
866 }
867
Emeric Brun54648852020-07-06 15:54:06 +0200868 cfg_sink->fmt = get_log_format(args[1]);
869 if (cfg_sink->fmt == LOG_FORMAT_UNSPEC) {
Emeric Brun99c453d2020-05-25 15:01:04 +0200870 ha_alert("parsing [%s:%d] : unknown format '%s'.\n", file, linenum, args[1]);
871 err_code |= ERR_ALERT | ERR_FATAL;
872 goto err;
873 }
874 }
875 else if (strcmp(args[0],"maxlen") == 0) {
876 if (!cfg_sink) {
877 ha_alert("parsing [%s:%d] : unable to set event max length '%s'.\n", file, linenum, args[1]);
878 err_code |= ERR_ALERT | ERR_FATAL;
879 goto err;
880 }
881
882 cfg_sink->maxlen = atol(args[1]);
883 if (!cfg_sink->maxlen) {
884 ha_alert("parsing [%s:%d] : invalid size '%s' for new sink buffer.\n", file, linenum, args[1]);
885 err_code |= ERR_ALERT | ERR_FATAL;
886 goto err;
887 }
888 }
889 else if (strcmp(args[0],"description") == 0) {
890 if (!cfg_sink) {
891 ha_alert("parsing [%s:%d] : unable to set description '%s'.\n", file, linenum, args[1]);
892 err_code |= ERR_ALERT | ERR_FATAL;
893 goto err;
894 }
895
896 if (!*args[1]) {
897 ha_alert("parsing [%s:%d] : missing ring description text.\n", file, linenum);
898 err_code |= ERR_ALERT | ERR_FATAL;
899 goto err;
900 }
901
902 free(cfg_sink->desc);
903
904 cfg_sink->desc = strdup(args[1]);
905 if (!cfg_sink->desc) {
906 ha_alert("parsing [%s:%d] : fail to set description '%s'.\n", file, linenum, args[1]);
907 err_code |= ERR_ALERT | ERR_FATAL;
908 goto err;
909 }
910 }
Emeric Brun9f2ff3a2020-05-29 15:47:52 +0200911 else {
912 ha_alert("parsing [%s:%d] : unknown statement '%s'.\n", file, linenum, args[0]);
913 err_code |= ERR_ALERT | ERR_FATAL;
914 goto err;
915 }
Emeric Brun99c453d2020-05-25 15:01:04 +0200916
917err:
918 return err_code;
919}
920
Emeric Brun94aab062021-04-02 10:41:36 +0200921/* Creates an new sink buffer from a log server.
922 *
923 * It uses the logsrvaddress to declare a forward
924 * server for this buffer. And it initializes the
925 * forwarding.
926 *
927 * The function returns a pointer on the
928 * allocated struct sink if allocate
929 * and initialize succeed, else if it fails
930 * it returns NULL.
931 *
932 * Note: the sink is created using the name
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +0500933 * specified into logsrv->ring_name
Emeric Brun94aab062021-04-02 10:41:36 +0200934 */
935struct sink *sink_new_from_logsrv(struct logsrv *logsrv)
936{
937 struct proxy *p = NULL;
938 struct sink *sink = NULL;
939 struct server *srv = NULL;
940 struct sink_forward_target *sft = NULL;
Emeric Brun94aab062021-04-02 10:41:36 +0200941
942 /* allocate new proxy to handle
943 * forward to a stream server
944 */
945 p = calloc(1, sizeof *p);
946 if (!p) {
947 goto error;
948 }
949
950 init_new_proxy(p);
951 sink_setup_proxy(p);
952 p->id = strdup(logsrv->ring_name);
953 p->conf.args.file = p->conf.file = strdup(logsrv->conf.file);
954 p->conf.args.line = p->conf.line = logsrv->conf.line;
955
956 /* allocate a new server to forward messages
957 * from ring buffer
958 */
959 srv = new_server(p);
960 if (!srv)
961 goto error;
962
963 /* init server */
964 srv->id = strdup(logsrv->ring_name);
965 srv->conf.file = strdup(logsrv->conf.file);
966 srv->conf.line = logsrv->conf.line;
967 srv->addr = logsrv->addr;
968 srv->svc_port = get_host_port(&logsrv->addr);
969 HA_SPIN_INIT(&srv->lock);
970
971 /* process per thread init */
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +0200972 if (srv_init_per_thr(srv) == -1)
Emeric Brun94aab062021-04-02 10:41:36 +0200973 goto error;
974
Emeric Brun94aab062021-04-02 10:41:36 +0200975 /* the servers are linked backwards
976 * first into proxy
977 */
978 p->srv = srv;
979 srv->next = p->srv;
980
981 /* allocate sink_forward_target descriptor */
982 sft = calloc(1, sizeof(*sft));
983 if (!sft)
984 goto error;
985
986 /* init sink_forward_target offset */
987 sft->srv = srv;
988 sft->appctx = NULL;
989 sft->ofs = ~0;
990 HA_SPIN_INIT(&sft->lock);
991
Ilya Shipitsinb2be9a12021-04-24 13:25:42 +0500992 /* prepare description for the sink */
Emeric Brun94aab062021-04-02 10:41:36 +0200993 chunk_reset(&trash);
994 chunk_printf(&trash, "created from logserver declared into '%s' at line %d", logsrv->conf.file, logsrv->conf.line);
995
996 /* allocate a new sink buffer */
997 sink = sink_new_buf(logsrv->ring_name, trash.area, logsrv->format, BUFSIZE);
998 if (!sink || sink->type != SINK_TYPE_BUFFER) {
999 goto error;
1000 }
1001
1002 /* link sink_forward_target to proxy */
1003 sink->forward_px = p;
1004 p->parent = sink;
1005
1006 /* insert into sink_forward_targets
1007 * list into sink
1008 */
1009 sft->next = sink->sft;
1010 sink->sft = sft;
1011
1012 /* mark server as an attached reader to the ring */
1013 if (!ring_attach(sink->ctx.ring)) {
1014 /* should never fail since there is
1015 * only one reader
1016 */
1017 goto error;
1018 }
1019
1020 /* initialize sink buffer forwarding */
1021 if (!sink_init_forward(sink))
1022 goto error;
1023
1024 /* reset familyt of logsrv to consider the ring buffer target */
1025 logsrv->addr.ss_family = AF_UNSPEC;
1026
1027 return sink;
1028error:
1029 if (p) {
1030 if (p->id)
1031 free(p->id);
1032 if (p->conf.file)
1033 free(p->conf.file);
1034
1035 free(p);
1036 }
1037
1038 if (srv) {
1039 if (srv->id)
1040 free(srv->id);
1041 if (srv->conf.file)
1042 free((void *)srv->conf.file);
1043 if (srv->per_thr)
1044 free(srv->per_thr);
1045 free(srv);
1046 }
1047
1048 if (sft)
1049 free(sft);
1050
1051 if (sink) {
1052 if (sink->ctx.ring)
1053 ring_free(sink->ctx.ring);
1054
Willy Tarreau2b718102021-04-21 07:32:39 +02001055 LIST_DELETE(&sink->sink_list);
Emeric Brun94aab062021-04-02 10:41:36 +02001056 free(sink->name);
1057 free(sink->desc);
1058 free(sink);
1059 }
1060
1061 return NULL;
1062}
1063
Emeric Brun99c453d2020-05-25 15:01:04 +02001064/*
1065 * Post parsing "ring" section.
1066 *
1067 * The function returns 0 in success case, otherwise, it returns error
1068 * flags.
1069 */
1070int cfg_post_parse_ring()
1071{
1072 int err_code = 0;
Emeric Brun494c5052020-05-28 11:13:15 +02001073 struct server *srv;
Emeric Brun99c453d2020-05-25 15:01:04 +02001074
1075 if (cfg_sink && (cfg_sink->type == SINK_TYPE_BUFFER)) {
1076 if (cfg_sink->maxlen > b_size(&cfg_sink->ctx.ring->buf)) {
1077 ha_warning("ring '%s' event max length '%u' exceeds size, forced to size '%lu'.\n",
Willy Tarreau570a22b2020-06-02 12:00:46 +02001078 cfg_sink->name, cfg_sink->maxlen, (unsigned long)b_size(&cfg_sink->ctx.ring->buf));
Emeric Brun99c453d2020-05-25 15:01:04 +02001079 cfg_sink->maxlen = b_size(&cfg_sink->ctx.ring->buf);
1080 err_code |= ERR_ALERT;
1081 }
Emeric Brun494c5052020-05-28 11:13:15 +02001082
1083 /* prepare forward server descriptors */
1084 if (cfg_sink->forward_px) {
1085 srv = cfg_sink->forward_px->srv;
1086 while (srv) {
1087 struct sink_forward_target *sft;
1088 /* init ssl if needed */
1089 if (srv->use_ssl == 1 && xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) {
1090 if (xprt_get(XPRT_SSL)->prepare_srv(srv)) {
1091 ha_alert("unable to prepare SSL for server '%s' in ring '%s'.\n", srv->id, cfg_sink->name);
1092 err_code |= ERR_ALERT | ERR_FATAL;
1093 }
1094 }
Emeric Brun99c453d2020-05-25 15:01:04 +02001095
Emeric Brun494c5052020-05-28 11:13:15 +02001096 /* allocate sink_forward_target descriptor */
1097 sft = calloc(1, sizeof(*sft));
1098 if (!sft) {
1099 ha_alert("memory allocation error initializing server '%s' in ring '%s'.\n",srv->id, cfg_sink->name);
1100 err_code |= ERR_ALERT | ERR_FATAL;
1101 break;
1102 }
1103 sft->srv = srv;
1104 sft->appctx = NULL;
1105 sft->ofs = ~0; /* init ring offset */
1106 sft->next = cfg_sink->sft;
1107 HA_SPIN_INIT(&sft->lock);
1108
1109 /* mark server attached to the ring */
1110 if (!ring_attach(cfg_sink->ctx.ring)) {
1111 ha_alert("server '%s' sets too many watchers > 255 on ring '%s'.\n", srv->id, cfg_sink->name);
1112 err_code |= ERR_ALERT | ERR_FATAL;
1113 }
1114 cfg_sink->sft = sft;
1115 srv = srv->next;
1116 }
1117 sink_init_forward(cfg_sink);
1118 }
1119 }
Emeric Brun99c453d2020-05-25 15:01:04 +02001120 cfg_sink = NULL;
1121
1122 return err_code;
1123}
1124
1125/* resolve sink names at end of config. Returns 0 on success otherwise error
1126 * flags.
1127*/
1128int post_sink_resolve()
1129{
Christopher Fauletfc633b62020-11-06 15:24:23 +01001130 int err_code = ERR_NONE;
Emeric Brun99c453d2020-05-25 15:01:04 +02001131 struct logsrv *logsrv, *logb;
1132 struct sink *sink;
1133 struct proxy *px;
1134
1135 list_for_each_entry_safe(logsrv, logb, &global.logsrvs, list) {
1136 if (logsrv->type == LOG_TARGET_BUFFER) {
1137 sink = sink_find(logsrv->ring_name);
Emeric Brun94aab062021-04-02 10:41:36 +02001138 if (!sink) {
1139 /* LOG_TARGET_BUFFER but !AF_UNSPEC
1140 * means we must allocate a sink
1141 * buffer to send messages to this logsrv
1142 */
1143 if (logsrv->addr.ss_family != AF_UNSPEC) {
1144 sink = sink_new_from_logsrv(logsrv);
1145 if (!sink) {
1146 ha_alert("global stream log server declared in file '%s' at line %d cannot be initialized'.\n",
1147 logsrv->conf.file, logsrv->conf.line);
1148 err_code |= ERR_ALERT | ERR_FATAL;
1149 }
1150 }
1151 else {
1152 ha_alert("global log server declared in file '%s' at line %d uses unknown ring named '%s'.\n",
1153 logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
1154 err_code |= ERR_ALERT | ERR_FATAL;
1155 }
1156 }
1157 else if (sink->type != SINK_TYPE_BUFFER) {
1158 ha_alert("global log server declared in file '%s' at line %d uses incompatible ring '%s'.\n",
1159 logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
Emeric Brun99c453d2020-05-25 15:01:04 +02001160 err_code |= ERR_ALERT | ERR_FATAL;
1161 }
1162 logsrv->sink = sink;
1163 }
Emeric Brun94aab062021-04-02 10:41:36 +02001164
Emeric Brun99c453d2020-05-25 15:01:04 +02001165 }
1166
1167 for (px = proxies_list; px; px = px->next) {
1168 list_for_each_entry_safe(logsrv, logb, &px->logsrvs, list) {
1169 if (logsrv->type == LOG_TARGET_BUFFER) {
1170 sink = sink_find(logsrv->ring_name);
Emeric Brun94aab062021-04-02 10:41:36 +02001171 if (!sink) {
1172 /* LOG_TARGET_BUFFER but !AF_UNSPEC
1173 * means we must allocate a sink
1174 * buffer to send messages to this logsrv
1175 */
1176 if (logsrv->addr.ss_family != AF_UNSPEC) {
1177 sink = sink_new_from_logsrv(logsrv);
1178 if (!sink) {
1179 ha_alert("log server declared in proxy section '%s' file '%s' at line %d cannot be initialized'.\n",
1180 px->id, logsrv->conf.file, logsrv->conf.line);
1181 err_code |= ERR_ALERT | ERR_FATAL;
1182 }
1183 }
1184 else {
1185 ha_alert("log server declared in proxy section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n",
1186 px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
1187 err_code |= ERR_ALERT | ERR_FATAL;
1188 }
1189 }
1190 else if (sink->type != SINK_TYPE_BUFFER) {
1191 ha_alert("log server declared in proxy section '%s' in file '%s' at line %d uses incomatible ring named '%s'.\n",
1192 px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
Emeric Brun99c453d2020-05-25 15:01:04 +02001193 err_code |= ERR_ALERT | ERR_FATAL;
1194 }
1195 logsrv->sink = sink;
1196 }
1197 }
1198 }
Emeric Brun12941c82020-07-07 14:19:42 +02001199
1200 for (px = cfg_log_forward; px; px = px->next) {
1201 list_for_each_entry_safe(logsrv, logb, &px->logsrvs, list) {
1202 if (logsrv->type == LOG_TARGET_BUFFER) {
1203 sink = sink_find(logsrv->ring_name);
Emeric Brun94aab062021-04-02 10:41:36 +02001204 if (!sink) {
1205 /* LOG_TARGET_BUFFER but !AF_UNSPEC
1206 * means we must allocate a sink
1207 * buffer to send messages to this logsrv
1208 */
1209 if (logsrv->addr.ss_family != AF_UNSPEC) {
1210 sink = sink_new_from_logsrv(logsrv);
1211 if (!sink) {
1212 ha_alert("log server declared in log-forward section '%s' file '%s' at line %d cannot be initialized'.\n",
1213 px->id, logsrv->conf.file, logsrv->conf.line);
1214 err_code |= ERR_ALERT | ERR_FATAL;
1215 }
1216 }
1217 else {
1218 ha_alert("log server declared in log-forward section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n",
1219 px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
1220 err_code |= ERR_ALERT | ERR_FATAL;
1221 }
1222 }
1223 else if (sink->type != SINK_TYPE_BUFFER) {
1224 ha_alert("log server declared in log-forward section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n",
1225 px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
Emeric Brun12941c82020-07-07 14:19:42 +02001226 err_code |= ERR_ALERT | ERR_FATAL;
1227 }
1228 logsrv->sink = sink;
1229 }
1230 }
1231 }
Emeric Brun99c453d2020-05-25 15:01:04 +02001232 return err_code;
1233}
1234
1235
Willy Tarreau973e6622019-08-20 11:57:52 +02001236static void sink_init()
1237{
Emeric Brun54648852020-07-06 15:54:06 +02001238 sink_new_fd("stdout", "standard output (fd#1)", LOG_FORMAT_RAW, 1);
1239 sink_new_fd("stderr", "standard output (fd#2)", LOG_FORMAT_RAW, 2);
1240 sink_new_buf("buf0", "in-memory ring buffer", LOG_FORMAT_TIMED, 1048576);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +02001241}
1242
1243static void sink_deinit()
1244{
1245 struct sink *sink, *sb;
1246
1247 list_for_each_entry_safe(sink, sb, &sink_list, sink_list) {
1248 if (sink->type == SINK_TYPE_BUFFER)
1249 ring_free(sink->ctx.ring);
Willy Tarreau2b718102021-04-21 07:32:39 +02001250 LIST_DELETE(&sink->sink_list);
Emeric Brun99c453d2020-05-25 15:01:04 +02001251 free(sink->name);
1252 free(sink->desc);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +02001253 free(sink);
1254 }
Willy Tarreau973e6622019-08-20 11:57:52 +02001255}
1256
1257INITCALL0(STG_REGISTER, sink_init);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +02001258REGISTER_POST_DEINIT(sink_deinit);
Willy Tarreau973e6622019-08-20 11:57:52 +02001259
Willy Tarreau9f830d72019-08-26 18:17:04 +02001260static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001261 { { "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 +02001262 {{},}
1263}};
1264
1265INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
1266
Emeric Brun99c453d2020-05-25 15:01:04 +02001267/* config parsers for this section */
1268REGISTER_CONFIG_SECTION("ring", cfg_parse_ring, cfg_post_parse_ring);
1269REGISTER_POST_CHECK(post_sink_resolve);
1270
Willy Tarreau67b5a162019-08-11 16:38:56 +02001271/*
1272 * Local variables:
1273 * c-indent-level: 8
1274 * c-basic-offset: 8
1275 * End:
1276 */