blob: fff70e9c9ace46c0e3eb9122a73aa26681906851 [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>
Willy Tarreau36979d92020-06-05 17:27:29 +020026#include <haproxy/errors.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020027#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020028#include <haproxy/log.h>
Willy Tarreau817538e2021-05-08 20:20:21 +020029#include <haproxy/proxy.h>
Willy Tarreaud2ad57c2020-06-03 19:43:35 +020030#include <haproxy/ring.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020031#include <haproxy/sc_strm.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020032#include <haproxy/signal.h>
Willy Tarreauba2f73d2020-06-03 20:02:28 +020033#include <haproxy/sink.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020034#include <haproxy/stconn.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{
Willy Tarreau0eca5392022-05-27 10:44:25 +0200302 struct stconn *sc = appctx_cs(appctx);
303 struct stream *s = __sc_strm(sc);
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 */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200320 sc_oc(sc)->rex = TICK_ETERNITY;
Emeric Brun494c5052020-05-28 11:13:15 +0200321 /* rto should not change but it seems the case */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200322 sc_oc(sc)->rto = TICK_ETERNITY;
Emeric Brun494c5052020-05-28 11:13:15 +0200323
324 /* an error was detected */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200325 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Emeric Brun494c5052020-05-28 11:13:15 +0200326 goto close;
327
328 /* con closed by server side */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200329 if ((sc_oc(sc)->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 */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200335 if (sc_opposite(sc)->state < SC_ST_EST) {
Willy Tarreau90e8b452022-05-25 18:21:43 +0200336 applet_need_more_data(appctx);
Willy Tarreaub23edc82022-05-24 16:49:03 +0200337 se_need_remote_conn(appctx->sedesc);
Willy Tarreau4164eb92022-05-25 15:42:03 +0200338 applet_have_more_data(appctx);
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 */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200374 if (sc_opposite(sc)->state == SC_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
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200402 if (applet_putchk(appctx, &trash) == -1) {
Emeric Brun494c5052020-05-28 11:13:15 +0200403 ret = 0;
404 break;
405 }
406 ofs += cnt + msg_len;
407 }
408
Willy Tarreau4781b152021-04-06 13:53:36 +0200409 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun494c5052020-05-28 11:13:15 +0200410 ofs += ring->ofs;
411 sft->ofs = ofs;
412 }
413 HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
414
415 if (ret) {
416 /* let's be woken up once new data arrive */
417 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau2b718102021-04-21 07:32:39 +0200418 LIST_APPEND(&ring->waiters, &appctx->wait_entry);
Emeric Brun494c5052020-05-28 11:13:15 +0200419 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
Willy Tarreau4164eb92022-05-25 15:42:03 +0200420 applet_have_no_more_data(appctx);
Emeric Brun494c5052020-05-28 11:13:15 +0200421 }
422 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
423
424 /* always drain data from server */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200425 co_skip(sc_oc(sc), sc_oc(sc)->output);
Emeric Brun494c5052020-05-28 11:13:15 +0200426 return;
427
428close:
Willy Tarreau0eca5392022-05-27 10:44:25 +0200429 sc_shutw(sc);
430 sc_shutr(sc);
431 sc_ic(sc)->flags |= CF_READ_NULL;
Emeric Brun494c5052020-05-28 11:13:15 +0200432}
433
Emeric Brun97556472020-05-30 01:42:45 +0200434/*
435 * IO Handler to handle message push to syslog tcp server
436 * using octet counting frames
Willy Tarreau42cc8312022-05-04 20:42:23 +0200437 * It takes its context from appctx->svcctx.
Emeric Brun97556472020-05-30 01:42:45 +0200438 */
439static void sink_forward_oc_io_handler(struct appctx *appctx)
440{
Willy Tarreau0eca5392022-05-27 10:44:25 +0200441 struct stconn *sc = appctx_cs(appctx);
442 struct stream *s = __sc_strm(sc);
Emeric Brun97556472020-05-30 01:42:45 +0200443 struct sink *sink = strm_fe(s)->parent;
Willy Tarreau42cc8312022-05-04 20:42:23 +0200444 struct sink_forward_target *sft = appctx->svcctx;
Emeric Brun97556472020-05-30 01:42:45 +0200445 struct ring *ring = sink->ctx.ring;
446 struct buffer *buf = &ring->buf;
447 uint64_t msg_len;
448 size_t len, cnt, ofs;
449 int ret = 0;
450 char *p;
451
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500452 /* if stopping was requested, close immediately */
Emeric Brun97556472020-05-30 01:42:45 +0200453 if (unlikely(stopping))
454 goto close;
455
456 /* for rex because it seems reset to timeout
457 * and we don't want expire on this case
458 * with a syslog server
459 */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200460 sc_oc(sc)->rex = TICK_ETERNITY;
Emeric Brun97556472020-05-30 01:42:45 +0200461 /* rto should not change but it seems the case */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200462 sc_oc(sc)->rto = TICK_ETERNITY;
Emeric Brun97556472020-05-30 01:42:45 +0200463
464 /* an error was detected */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200465 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Emeric Brun97556472020-05-30 01:42:45 +0200466 goto close;
467
468 /* con closed by server side */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200469 if ((sc_oc(sc)->flags & CF_SHUTW))
Emeric Brun97556472020-05-30 01:42:45 +0200470 goto close;
471
472 /* if the connection is not established, inform the stream that we want
473 * to be notified whenever the connection completes.
474 */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200475 if (sc_opposite(sc)->state < SC_ST_EST) {
Willy Tarreau90e8b452022-05-25 18:21:43 +0200476 applet_need_more_data(appctx);
Willy Tarreaub23edc82022-05-24 16:49:03 +0200477 se_need_remote_conn(appctx->sedesc);
Willy Tarreau4164eb92022-05-25 15:42:03 +0200478 applet_have_more_data(appctx);
Emeric Brun97556472020-05-30 01:42:45 +0200479 return;
480 }
481
482 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
483 if (appctx != sft->appctx) {
484 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
485 goto close;
486 }
487 ofs = sft->ofs;
488
489 HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &ring->lock);
490 LIST_DEL_INIT(&appctx->wait_entry);
491 HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &ring->lock);
492
493 HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &ring->lock);
494
495 /* explanation for the initialization below: it would be better to do
496 * this in the parsing function but this would occasionally result in
497 * dropped events because we'd take a reference on the oldest message
498 * and keep it while being scheduled. Thus instead let's take it the
499 * first time we enter here so that we have a chance to pass many
500 * existing messages before grabbing a reference to a location. This
501 * value cannot be produced after initialization.
502 */
503 if (unlikely(ofs == ~0)) {
504 ofs = 0;
505
Willy Tarreau4781b152021-04-06 13:53:36 +0200506 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brun97556472020-05-30 01:42:45 +0200507 ofs += ring->ofs;
508 }
509
Emeric Brun97556472020-05-30 01:42:45 +0200510 /* in this loop, ofs always points to the counter byte that precedes
511 * the message so that we can take our reference there if we have to
512 * stop before the end (ret=0).
513 */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200514 if (sc_opposite(sc)->state == SC_ST_EST) {
Emeric Brunfdabf492020-12-02 17:02:09 +0100515 /* we were already there, adjust the offset to be relative to
516 * the buffer's head and remove us from the counter.
517 */
518 ofs -= ring->ofs;
519 BUG_ON(ofs >= buf->size);
Willy Tarreau4781b152021-04-06 13:53:36 +0200520 HA_ATOMIC_DEC(b_peek(buf, ofs));
Emeric Brunfdabf492020-12-02 17:02:09 +0100521
Emeric Brun97556472020-05-30 01:42:45 +0200522 ret = 1;
523 while (ofs + 1 < b_data(buf)) {
524 cnt = 1;
525 len = b_peek_varint(buf, ofs + cnt, &msg_len);
526 if (!len)
527 break;
528 cnt += len;
529 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
530
531 chunk_reset(&trash);
532 p = ulltoa(msg_len, trash.area, b_size(&trash));
533 if (p) {
534 trash.data = (p - trash.area) + 1;
535 *p = ' ';
536 }
537
538 if (!p || (trash.data + msg_len > b_size(&trash))) {
539 /* too large a message to ever fit, let's skip it */
540 ofs += cnt + msg_len;
541 continue;
542 }
543
544 trash.data += b_getblk(buf, p + 1, msg_len, ofs + cnt);
545
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200546 if (applet_putchk(appctx, &trash) == -1) {
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);
Willy Tarreau4164eb92022-05-25 15:42:03 +0200564 applet_have_no_more_data(appctx);
Emeric Brun97556472020-05-30 01:42:45 +0200565 }
566 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
567
568 /* always drain data from server */
Willy Tarreau0eca5392022-05-27 10:44:25 +0200569 co_skip(sc_oc(sc), sc_oc(sc)->output);
Emeric Brun97556472020-05-30 01:42:45 +0200570 return;
571
572close:
Willy Tarreau0eca5392022-05-27 10:44:25 +0200573 sc_shutw(sc);
574 sc_shutr(sc);
575 sc_ic(sc)->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{
Willy Tarreau0698c802022-05-11 14:09:57 +0200580 struct stream *s = appctx_strm(sft->appctx);
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
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200595static int sink_forward_session_init(struct appctx *appctx)
596{
597 struct sink_forward_target *sft = appctx->svcctx;
598 struct stream *s;
599 struct sockaddr_storage *addr = NULL;
600
601 if (!sockaddr_alloc(&addr, &sft->srv->addr, sizeof(sft->srv->addr)))
602 goto out_error;
603
604 if (appctx_finalize_startup(appctx, sft->sink->forward_px, &BUF_NULL) == -1)
605 goto out_free_addr;
606
607 s = appctx_strm(appctx);
Willy Tarreau7cb9e6c2022-05-17 19:40:40 +0200608 s->scb->dst = addr;
Willy Tarreaucb041662022-05-17 19:44:42 +0200609 s->scb->flags |= SC_FL_NOLINGER;
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200610
611 s->target = &sft->srv->obj_type;
612 s->flags = SF_ASSIGNED;
613
614 s->do_log = NULL;
615 s->uniq_id = 0;
616
617 s->res.flags |= CF_READ_DONTWAIT;
618 /* for rto and rex to eternity to not expire on idle recv:
619 * We are using a syslog server.
620 */
621 s->res.rto = TICK_ETERNITY;
622 s->res.rex = TICK_ETERNITY;
623 sft->appctx = appctx;
624
625 return 0;
626
627 out_free_addr:
628 sockaddr_free(&addr);
629 out_error:
630 return -1;
631}
Emeric Brun494c5052020-05-28 11:13:15 +0200632
633static void sink_forward_session_release(struct appctx *appctx)
634{
Willy Tarreau42cc8312022-05-04 20:42:23 +0200635 struct sink_forward_target *sft = appctx->svcctx;
Emeric Brun494c5052020-05-28 11:13:15 +0200636
637 if (!sft)
638 return;
639
640 HA_SPIN_LOCK(SFT_LOCK, &sft->lock);
641 if (sft->appctx == appctx)
642 __sink_forward_session_deinit(sft);
643 HA_SPIN_UNLOCK(SFT_LOCK, &sft->lock);
644}
645
646static struct applet sink_forward_applet = {
647 .obj_type = OBJ_TYPE_APPLET,
648 .name = "<SINKFWD>", /* used for logging */
649 .fct = sink_forward_io_handler,
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200650 .init = sink_forward_session_init,
Emeric Brun494c5052020-05-28 11:13:15 +0200651 .release = sink_forward_session_release,
652};
653
Emeric Brun97556472020-05-30 01:42:45 +0200654static struct applet sink_forward_oc_applet = {
655 .obj_type = OBJ_TYPE_APPLET,
656 .name = "<SINKFWDOC>", /* used for logging */
657 .fct = sink_forward_oc_io_handler,
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200658 .init = sink_forward_session_init,
Emeric Brun97556472020-05-30 01:42:45 +0200659 .release = sink_forward_session_release,
660};
661
Emeric Brun494c5052020-05-28 11:13:15 +0200662/*
663 * Create a new peer session in assigned state (connect will start automatically)
Willy Tarreau42cc8312022-05-04 20:42:23 +0200664 * It sets its context into appctx->svcctx.
Emeric Brun494c5052020-05-28 11:13:15 +0200665 */
666static struct appctx *sink_forward_session_create(struct sink *sink, struct sink_forward_target *sft)
667{
Emeric Brun494c5052020-05-28 11:13:15 +0200668 struct appctx *appctx;
Emeric Brun97556472020-05-30 01:42:45 +0200669 struct applet *applet = &sink_forward_applet;
670
671 if (sft->srv->log_proto == SRV_LOG_PROTO_OCTET_COUNTING)
672 applet = &sink_forward_oc_applet;
Emeric Brun494c5052020-05-28 11:13:15 +0200673
Christopher Faulet6095d572022-05-16 17:09:48 +0200674 appctx = appctx_new_here(applet, NULL);
Christopher Faulet2479e5f2022-01-19 14:50:11 +0100675 if (!appctx)
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100676 goto out_close;
Willy Tarreau42cc8312022-05-04 20:42:23 +0200677 appctx->svcctx = (void *)sft;
Emeric Brun494c5052020-05-28 11:13:15 +0200678
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200679 if (appctx_init(appctx) == -1)
Christopher Faulet92202da2022-05-11 12:22:10 +0200680 goto out_free_appctx;
Christopher Faulet13a35e52021-12-20 15:34:16 +0100681
Emeric Brun494c5052020-05-28 11:13:15 +0200682 return appctx;
683
684 /* Error unrolling */
Emeric Brun494c5052020-05-28 11:13:15 +0200685 out_free_appctx:
Christopher Fauletaee57fc2022-05-12 15:34:48 +0200686 appctx_free_on_early_error(appctx);
Emeric Brun494c5052020-05-28 11:13:15 +0200687 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 */
Christopher Faulet2ae25ea2022-05-12 14:50:09 +02001007 sft->sink = sink;
Emeric Brun94aab062021-04-02 10:41:36 +02001008 sft->next = sink->sft;
1009 sink->sft = sft;
1010
1011 /* mark server as an attached reader to the ring */
1012 if (!ring_attach(sink->ctx.ring)) {
1013 /* should never fail since there is
1014 * only one reader
1015 */
1016 goto error;
1017 }
1018
1019 /* initialize sink buffer forwarding */
1020 if (!sink_init_forward(sink))
1021 goto error;
1022
1023 /* reset familyt of logsrv to consider the ring buffer target */
1024 logsrv->addr.ss_family = AF_UNSPEC;
1025
1026 return sink;
1027error:
1028 if (p) {
1029 if (p->id)
1030 free(p->id);
1031 if (p->conf.file)
1032 free(p->conf.file);
1033
1034 free(p);
1035 }
1036
1037 if (srv) {
1038 if (srv->id)
1039 free(srv->id);
1040 if (srv->conf.file)
1041 free((void *)srv->conf.file);
1042 if (srv->per_thr)
1043 free(srv->per_thr);
1044 free(srv);
1045 }
1046
1047 if (sft)
1048 free(sft);
1049
1050 if (sink) {
1051 if (sink->ctx.ring)
1052 ring_free(sink->ctx.ring);
1053
Willy Tarreau2b718102021-04-21 07:32:39 +02001054 LIST_DELETE(&sink->sink_list);
Emeric Brun94aab062021-04-02 10:41:36 +02001055 free(sink->name);
1056 free(sink->desc);
1057 free(sink);
1058 }
1059
1060 return NULL;
1061}
1062
Emeric Brun99c453d2020-05-25 15:01:04 +02001063/*
1064 * Post parsing "ring" section.
1065 *
1066 * The function returns 0 in success case, otherwise, it returns error
1067 * flags.
1068 */
1069int cfg_post_parse_ring()
1070{
1071 int err_code = 0;
Emeric Brun494c5052020-05-28 11:13:15 +02001072 struct server *srv;
Emeric Brun99c453d2020-05-25 15:01:04 +02001073
1074 if (cfg_sink && (cfg_sink->type == SINK_TYPE_BUFFER)) {
1075 if (cfg_sink->maxlen > b_size(&cfg_sink->ctx.ring->buf)) {
1076 ha_warning("ring '%s' event max length '%u' exceeds size, forced to size '%lu'.\n",
Willy Tarreau570a22b2020-06-02 12:00:46 +02001077 cfg_sink->name, cfg_sink->maxlen, (unsigned long)b_size(&cfg_sink->ctx.ring->buf));
Emeric Brun99c453d2020-05-25 15:01:04 +02001078 cfg_sink->maxlen = b_size(&cfg_sink->ctx.ring->buf);
1079 err_code |= ERR_ALERT;
1080 }
Emeric Brun494c5052020-05-28 11:13:15 +02001081
1082 /* prepare forward server descriptors */
1083 if (cfg_sink->forward_px) {
1084 srv = cfg_sink->forward_px->srv;
1085 while (srv) {
1086 struct sink_forward_target *sft;
1087 /* init ssl if needed */
1088 if (srv->use_ssl == 1 && xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) {
1089 if (xprt_get(XPRT_SSL)->prepare_srv(srv)) {
1090 ha_alert("unable to prepare SSL for server '%s' in ring '%s'.\n", srv->id, cfg_sink->name);
1091 err_code |= ERR_ALERT | ERR_FATAL;
1092 }
1093 }
Emeric Brun99c453d2020-05-25 15:01:04 +02001094
Emeric Brun494c5052020-05-28 11:13:15 +02001095 /* allocate sink_forward_target descriptor */
1096 sft = calloc(1, sizeof(*sft));
1097 if (!sft) {
1098 ha_alert("memory allocation error initializing server '%s' in ring '%s'.\n",srv->id, cfg_sink->name);
1099 err_code |= ERR_ALERT | ERR_FATAL;
1100 break;
1101 }
1102 sft->srv = srv;
1103 sft->appctx = NULL;
1104 sft->ofs = ~0; /* init ring offset */
1105 sft->next = cfg_sink->sft;
1106 HA_SPIN_INIT(&sft->lock);
1107
1108 /* mark server attached to the ring */
1109 if (!ring_attach(cfg_sink->ctx.ring)) {
1110 ha_alert("server '%s' sets too many watchers > 255 on ring '%s'.\n", srv->id, cfg_sink->name);
1111 err_code |= ERR_ALERT | ERR_FATAL;
1112 }
1113 cfg_sink->sft = sft;
1114 srv = srv->next;
1115 }
1116 sink_init_forward(cfg_sink);
1117 }
1118 }
Emeric Brun99c453d2020-05-25 15:01:04 +02001119 cfg_sink = NULL;
1120
1121 return err_code;
1122}
1123
1124/* resolve sink names at end of config. Returns 0 on success otherwise error
1125 * flags.
1126*/
1127int post_sink_resolve()
1128{
Christopher Fauletfc633b62020-11-06 15:24:23 +01001129 int err_code = ERR_NONE;
Emeric Brun99c453d2020-05-25 15:01:04 +02001130 struct logsrv *logsrv, *logb;
1131 struct sink *sink;
1132 struct proxy *px;
1133
1134 list_for_each_entry_safe(logsrv, logb, &global.logsrvs, list) {
1135 if (logsrv->type == LOG_TARGET_BUFFER) {
1136 sink = sink_find(logsrv->ring_name);
Emeric Brun94aab062021-04-02 10:41:36 +02001137 if (!sink) {
1138 /* LOG_TARGET_BUFFER but !AF_UNSPEC
1139 * means we must allocate a sink
1140 * buffer to send messages to this logsrv
1141 */
1142 if (logsrv->addr.ss_family != AF_UNSPEC) {
1143 sink = sink_new_from_logsrv(logsrv);
1144 if (!sink) {
1145 ha_alert("global stream log server declared in file '%s' at line %d cannot be initialized'.\n",
1146 logsrv->conf.file, logsrv->conf.line);
1147 err_code |= ERR_ALERT | ERR_FATAL;
1148 }
1149 }
1150 else {
1151 ha_alert("global log server declared in file '%s' at line %d uses unknown ring named '%s'.\n",
1152 logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
1153 err_code |= ERR_ALERT | ERR_FATAL;
1154 }
1155 }
1156 else if (sink->type != SINK_TYPE_BUFFER) {
1157 ha_alert("global log server declared in file '%s' at line %d uses incompatible ring '%s'.\n",
1158 logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
Emeric Brun99c453d2020-05-25 15:01:04 +02001159 err_code |= ERR_ALERT | ERR_FATAL;
1160 }
1161 logsrv->sink = sink;
1162 }
Emeric Brun94aab062021-04-02 10:41:36 +02001163
Emeric Brun99c453d2020-05-25 15:01:04 +02001164 }
1165
1166 for (px = proxies_list; px; px = px->next) {
1167 list_for_each_entry_safe(logsrv, logb, &px->logsrvs, list) {
1168 if (logsrv->type == LOG_TARGET_BUFFER) {
1169 sink = sink_find(logsrv->ring_name);
Emeric Brun94aab062021-04-02 10:41:36 +02001170 if (!sink) {
1171 /* LOG_TARGET_BUFFER but !AF_UNSPEC
1172 * means we must allocate a sink
1173 * buffer to send messages to this logsrv
1174 */
1175 if (logsrv->addr.ss_family != AF_UNSPEC) {
1176 sink = sink_new_from_logsrv(logsrv);
1177 if (!sink) {
1178 ha_alert("log server declared in proxy section '%s' file '%s' at line %d cannot be initialized'.\n",
1179 px->id, logsrv->conf.file, logsrv->conf.line);
1180 err_code |= ERR_ALERT | ERR_FATAL;
1181 }
1182 }
1183 else {
1184 ha_alert("log server declared in proxy section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n",
1185 px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
1186 err_code |= ERR_ALERT | ERR_FATAL;
1187 }
1188 }
1189 else if (sink->type != SINK_TYPE_BUFFER) {
1190 ha_alert("log server declared in proxy section '%s' in file '%s' at line %d uses incomatible ring named '%s'.\n",
1191 px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
Emeric Brun99c453d2020-05-25 15:01:04 +02001192 err_code |= ERR_ALERT | ERR_FATAL;
1193 }
1194 logsrv->sink = sink;
1195 }
1196 }
1197 }
Emeric Brun12941c82020-07-07 14:19:42 +02001198
1199 for (px = cfg_log_forward; px; px = px->next) {
1200 list_for_each_entry_safe(logsrv, logb, &px->logsrvs, list) {
1201 if (logsrv->type == LOG_TARGET_BUFFER) {
1202 sink = sink_find(logsrv->ring_name);
Emeric Brun94aab062021-04-02 10:41:36 +02001203 if (!sink) {
1204 /* LOG_TARGET_BUFFER but !AF_UNSPEC
1205 * means we must allocate a sink
1206 * buffer to send messages to this logsrv
1207 */
1208 if (logsrv->addr.ss_family != AF_UNSPEC) {
1209 sink = sink_new_from_logsrv(logsrv);
1210 if (!sink) {
1211 ha_alert("log server declared in log-forward section '%s' file '%s' at line %d cannot be initialized'.\n",
1212 px->id, logsrv->conf.file, logsrv->conf.line);
1213 err_code |= ERR_ALERT | ERR_FATAL;
1214 }
1215 }
1216 else {
1217 ha_alert("log server declared in log-forward section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n",
1218 px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
1219 err_code |= ERR_ALERT | ERR_FATAL;
1220 }
1221 }
1222 else if (sink->type != SINK_TYPE_BUFFER) {
1223 ha_alert("log server declared in log-forward section '%s' in file '%s' at line %d uses unknown ring named '%s'.\n",
1224 px->id, logsrv->conf.file, logsrv->conf.line, logsrv->ring_name);
Emeric Brun12941c82020-07-07 14:19:42 +02001225 err_code |= ERR_ALERT | ERR_FATAL;
1226 }
1227 logsrv->sink = sink;
1228 }
1229 }
1230 }
Emeric Brun99c453d2020-05-25 15:01:04 +02001231 return err_code;
1232}
1233
1234
Willy Tarreau973e6622019-08-20 11:57:52 +02001235static void sink_init()
1236{
Emeric Brun54648852020-07-06 15:54:06 +02001237 sink_new_fd("stdout", "standard output (fd#1)", LOG_FORMAT_RAW, 1);
1238 sink_new_fd("stderr", "standard output (fd#2)", LOG_FORMAT_RAW, 2);
1239 sink_new_buf("buf0", "in-memory ring buffer", LOG_FORMAT_TIMED, 1048576);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +02001240}
1241
1242static void sink_deinit()
1243{
1244 struct sink *sink, *sb;
1245
1246 list_for_each_entry_safe(sink, sb, &sink_list, sink_list) {
1247 if (sink->type == SINK_TYPE_BUFFER)
1248 ring_free(sink->ctx.ring);
Willy Tarreau2b718102021-04-21 07:32:39 +02001249 LIST_DELETE(&sink->sink_list);
Emeric Brun99c453d2020-05-25 15:01:04 +02001250 free(sink->name);
1251 free(sink->desc);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +02001252 free(sink);
1253 }
Willy Tarreau973e6622019-08-20 11:57:52 +02001254}
1255
1256INITCALL0(STG_REGISTER, sink_init);
Willy Tarreau4ed23ca2019-08-23 15:47:49 +02001257REGISTER_POST_DEINIT(sink_deinit);
Willy Tarreau973e6622019-08-20 11:57:52 +02001258
Willy Tarreau9f830d72019-08-26 18:17:04 +02001259static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001260 { { "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 +02001261 {{},}
1262}};
1263
1264INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
1265
Emeric Brun99c453d2020-05-25 15:01:04 +02001266/* config parsers for this section */
1267REGISTER_CONFIG_SECTION("ring", cfg_parse_ring, cfg_post_parse_ring);
1268REGISTER_POST_CHECK(post_sink_resolve);
1269
Willy Tarreau67b5a162019-08-11 16:38:56 +02001270/*
1271 * Local variables:
1272 * c-indent-level: 8
1273 * c-basic-offset: 8
1274 * End:
1275 */