Introduce the relayd socket object
[lttng-tools.git] / src / common / kernel-consumer / kernel-consumer.c
CommitLineData
3bd1e081
MD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
3bd1e081
MD
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3bd1e081
MD
17 */
18
19#define _GNU_SOURCE
20#include <assert.h>
3bd1e081
MD
21#include <poll.h>
22#include <pthread.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/mman.h>
26#include <sys/socket.h>
27#include <sys/types.h>
77c7c900 28#include <inttypes.h>
3bd1e081 29#include <unistd.h>
dbb5dfe6 30#include <sys/stat.h>
3bd1e081 31
990570ed 32#include <common/common.h>
10a8a223 33#include <common/kernel-ctl/kernel-ctl.h>
10a8a223 34#include <common/sessiond-comm/sessiond-comm.h>
00e2e675 35#include <common/sessiond-comm/relayd.h>
dbb5dfe6 36#include <common/compat/fcntl.h>
00e2e675 37#include <common/relayd/relayd.h>
0857097f 38
10a8a223 39#include "kernel-consumer.h"
3bd1e081
MD
40
41extern struct lttng_consumer_global_data consumer_data;
42extern int consumer_poll_timeout;
43extern volatile int consumer_quit;
44
3bd1e081
MD
45/*
46 * Take a snapshot for a specific fd
47 *
48 * Returns 0 on success, < 0 on error
49 */
ffe60014 50int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
3bd1e081
MD
51{
52 int ret = 0;
53 int infd = stream->wait_fd;
54
55 ret = kernctl_snapshot(infd);
56 if (ret != 0) {
87dc6a9c 57 errno = -ret;
3bd1e081
MD
58 perror("Getting sub-buffer snapshot.");
59 }
60
61 return ret;
62}
63
64/*
65 * Get the produced position
66 *
67 * Returns 0 on success, < 0 on error
68 */
ffe60014 69int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
3bd1e081
MD
70 unsigned long *pos)
71{
72 int ret;
73 int infd = stream->wait_fd;
74
75 ret = kernctl_snapshot_get_produced(infd, pos);
76 if (ret != 0) {
87dc6a9c 77 errno = -ret;
3bd1e081
MD
78 perror("kernctl_snapshot_get_produced");
79 }
80
81 return ret;
82}
83
84int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
85 int sock, struct pollfd *consumer_sockpoll)
86{
87 ssize_t ret;
f50f23d9 88 enum lttng_error_code ret_code = LTTNG_OK;
3bd1e081
MD
89 struct lttcomm_consumer_msg msg;
90
91 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
92 if (ret != sizeof(msg)) {
f73fabfd 93 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
3bd1e081
MD
94 return ret;
95 }
96 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
f50f23d9
DG
97 /*
98 * Notify the session daemon that the command is completed.
99 *
100 * On transport layer error, the function call will print an error
101 * message so handling the returned code is a bit useless since we
102 * return an error code anyway.
103 */
104 (void) consumer_send_status_msg(sock, ret_code);
3bd1e081
MD
105 return -ENOENT;
106 }
107
b0b335c8
MD
108 /* relayd needs RCU read-side protection */
109 rcu_read_lock();
110
3bd1e081 111 switch (msg.cmd_type) {
00e2e675
DG
112 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
113 {
f50f23d9 114 /* Session daemon status message are handled in the following call. */
7735ef9e
DG
115 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
116 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
46e6455f 117 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id);
00e2e675
DG
118 goto end_nosignal;
119 }
3bd1e081
MD
120 case LTTNG_CONSUMER_ADD_CHANNEL:
121 {
122 struct lttng_consumer_channel *new_channel;
123
f50f23d9
DG
124 /* First send a status message before receiving the fds. */
125 ret = consumer_send_status_msg(sock, ret_code);
126 if (ret < 0) {
127 /* Somehow, the session daemon is not responding anymore. */
128 goto end_nosignal;
129 }
130
d88aee68 131 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
3bd1e081 132 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
ffe60014
DG
133 msg.u.channel.session_id, msg.u.channel.pathname,
134 msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid,
1624d5b7
JD
135 msg.u.channel.relayd_id, msg.u.channel.output,
136 msg.u.channel.tracefile_size,
137 msg.u.channel.tracefile_count);
3bd1e081 138 if (new_channel == NULL) {
f73fabfd 139 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
3bd1e081
MD
140 goto end_nosignal;
141 }
ffe60014
DG
142 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
143
144 /* Translate and save channel type. */
145 switch (msg.u.channel.type) {
146 case CONSUMER_CHANNEL_TYPE_DATA:
147 case CONSUMER_CHANNEL_TYPE_METADATA:
148 new_channel->type = msg.u.channel.type;
149 break;
150 default:
151 assert(0);
152 goto end_nosignal;
153 };
154
3bd1e081
MD
155 if (ctx->on_recv_channel != NULL) {
156 ret = ctx->on_recv_channel(new_channel);
157 if (ret == 0) {
d8ef542d 158 consumer_add_channel(new_channel, ctx);
3bd1e081
MD
159 } else if (ret < 0) {
160 goto end_nosignal;
161 }
162 } else {
d8ef542d 163 consumer_add_channel(new_channel, ctx);
3bd1e081
MD
164 }
165 goto end_nosignal;
166 }
167 case LTTNG_CONSUMER_ADD_STREAM:
168 {
50f8ae69 169 int fd, stream_pipe;
00e2e675
DG
170 struct consumer_relayd_sock_pair *relayd = NULL;
171 struct lttng_consumer_stream *new_stream;
ffe60014 172 struct lttng_consumer_channel *channel;
c80048c6 173 int alloc_ret = 0;
3bd1e081 174
ffe60014
DG
175 /*
176 * Get stream's channel reference. Needed when adding the stream to the
177 * global hash table.
178 */
179 channel = consumer_find_channel(msg.u.stream.channel_key);
180 if (!channel) {
181 /*
182 * We could not find the channel. Can happen if cpu hotplug
183 * happens while tearing down.
184 */
d88aee68 185 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
ffe60014
DG
186 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
187 }
188
f50f23d9
DG
189 /* First send a status message before receiving the fds. */
190 ret = consumer_send_status_msg(sock, ret_code);
ffe60014
DG
191 if (ret < 0 || ret_code != LTTNG_OK) {
192 /*
193 * Somehow, the session daemon is not responding anymore or the
194 * channel was not found.
195 */
f50f23d9
DG
196 goto end_nosignal;
197 }
198
3bd1e081
MD
199 /* block */
200 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3f8e211f 201 rcu_read_unlock();
3bd1e081
MD
202 return -EINTR;
203 }
00e2e675
DG
204
205 /* Get stream file descriptor from socket */
f2fc6720
MD
206 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
207 if (ret != sizeof(fd)) {
f73fabfd 208 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3f8e211f 209 rcu_read_unlock();
3bd1e081
MD
210 return ret;
211 }
3bd1e081 212
f50f23d9
DG
213 /*
214 * Send status code to session daemon only if the recv works. If the
215 * above recv() failed, the session daemon is notified through the
216 * error socket and the teardown is eventually done.
217 */
218 ret = consumer_send_status_msg(sock, ret_code);
219 if (ret < 0) {
220 /* Somehow, the session daemon is not responding anymore. */
221 goto end_nosignal;
222 }
223
ffe60014
DG
224 new_stream = consumer_allocate_stream(channel->key,
225 fd,
226 LTTNG_CONSUMER_ACTIVE_STREAM,
227 channel->name,
228 channel->uid,
229 channel->gid,
230 channel->relayd_id,
231 channel->session_id,
232 msg.u.stream.cpu,
233 &alloc_ret,
234 channel->type);
3bd1e081 235 if (new_stream == NULL) {
c80048c6
MD
236 switch (alloc_ret) {
237 case -ENOMEM:
238 case -EINVAL:
239 default:
240 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
241 break;
c80048c6 242 }
3f8e211f 243 goto end_nosignal;
3bd1e081 244 }
ffe60014
DG
245 new_stream->chan = channel;
246 new_stream->wait_fd = fd;
00e2e675 247
fb3a43a9
DG
248 /*
249 * The buffer flush is done on the session daemon side for the kernel
250 * so no need for the stream "hangup_flush_done" variable to be
251 * tracked. This is important for a kernel stream since we don't rely
252 * on the flush state of the stream to read data. It's not the case for
253 * user space tracing.
254 */
255 new_stream->hangup_flush_done = 0;
256
00e2e675 257 /* The stream is not metadata. Get relayd reference if exists. */
ffe60014 258 relayd = consumer_find_relayd(new_stream->net_seq_idx);
00e2e675
DG
259 if (relayd != NULL) {
260 /* Add stream on the relayd */
261 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
262 ret = relayd_add_stream(&relayd->control_sock,
ffe60014 263 new_stream->name, new_stream->chan->pathname,
00e2e675
DG
264 &new_stream->relayd_stream_id);
265 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
266 if (ret < 0) {
e316aad5 267 consumer_del_stream(new_stream, NULL);
3f8e211f 268 goto end_nosignal;
00e2e675 269 }
d88aee68
DG
270 } else if (new_stream->net_seq_idx != (uint64_t) -1ULL) {
271 ERR("Network sequence index %" PRIu64 " unknown. Not adding stream.",
ffe60014 272 new_stream->net_seq_idx);
e316aad5 273 consumer_del_stream(new_stream, NULL);
3f8e211f 274 goto end_nosignal;
00e2e675
DG
275 }
276
633d0084
DG
277 if (ctx->on_recv_stream) {
278 ret = ctx->on_recv_stream(new_stream);
279 if (ret < 0) {
e316aad5 280 consumer_del_stream(new_stream, NULL);
633d0084 281 goto end_nosignal;
fb3a43a9 282 }
633d0084 283 }
fb3a43a9 284
50f8ae69 285 /* Get the right pipe where the stream will be sent. */
633d0084 286 if (new_stream->metadata_flag) {
50f8ae69 287 stream_pipe = ctx->consumer_metadata_pipe[1];
3bd1e081 288 } else {
50f8ae69
DG
289 stream_pipe = ctx->consumer_data_pipe[1];
290 }
291
292 do {
293 ret = write(stream_pipe, &new_stream, sizeof(new_stream));
294 } while (ret < 0 && errno == EINTR);
295 if (ret < 0) {
296 PERROR("Consumer write %s stream to pipe %d",
297 new_stream->metadata_flag ? "metadata" : "data",
298 stream_pipe);
299 consumer_del_stream(new_stream, NULL);
300 goto end_nosignal;
3bd1e081 301 }
00e2e675 302
50f8ae69 303 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64,
ffe60014 304 new_stream->name, fd, new_stream->relayd_stream_id);
3bd1e081
MD
305 break;
306 }
307 case LTTNG_CONSUMER_UPDATE_STREAM:
308 {
3f8e211f
DG
309 rcu_read_unlock();
310 return -ENOSYS;
311 }
312 case LTTNG_CONSUMER_DESTROY_RELAYD:
313 {
a6ba4fe1 314 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
3f8e211f
DG
315 struct consumer_relayd_sock_pair *relayd;
316
a6ba4fe1 317 DBG("Kernel consumer destroying relayd %" PRIu64, index);
3f8e211f
DG
318
319 /* Get relayd reference if exists. */
a6ba4fe1 320 relayd = consumer_find_relayd(index);
3f8e211f 321 if (relayd == NULL) {
3448e266 322 DBG("Unable to find relayd %" PRIu64, index);
f50f23d9 323 ret_code = LTTNG_ERR_NO_CONSUMER;
3bd1e081 324 }
3f8e211f 325
a6ba4fe1
DG
326 /*
327 * Each relayd socket pair has a refcount of stream attached to it
328 * which tells if the relayd is still active or not depending on the
329 * refcount value.
330 *
331 * This will set the destroy flag of the relayd object and destroy it
332 * if the refcount reaches zero when called.
333 *
334 * The destroy can happen either here or when a stream fd hangs up.
335 */
f50f23d9
DG
336 if (relayd) {
337 consumer_flag_relayd_for_destroy(relayd);
338 }
339
340 ret = consumer_send_status_msg(sock, ret_code);
341 if (ret < 0) {
342 /* Somehow, the session daemon is not responding anymore. */
343 goto end_nosignal;
344 }
3f8e211f 345
3f8e211f 346 goto end_nosignal;
3bd1e081 347 }
6d805429 348 case LTTNG_CONSUMER_DATA_PENDING:
53632229 349 {
c8f59ee5 350 int32_t ret;
6d805429 351 uint64_t id = msg.u.data_pending.session_id;
c8f59ee5 352
6d805429 353 DBG("Kernel consumer data pending command for id %" PRIu64, id);
c8f59ee5 354
6d805429 355 ret = consumer_data_pending(id);
c8f59ee5
DG
356
357 /* Send back returned value to session daemon */
358 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
359 if (ret < 0) {
6d805429 360 PERROR("send data pending ret code");
c8f59ee5 361 }
f50f23d9
DG
362
363 /*
364 * No need to send back a status message since the data pending
365 * returned value is the response.
366 */
c8f59ee5 367 break;
53632229 368 }
3bd1e081 369 default:
3f8e211f 370 goto end_nosignal;
3bd1e081 371 }
3f8e211f 372
3bd1e081 373end_nosignal:
b0b335c8 374 rcu_read_unlock();
4cbc1a04
DG
375
376 /*
377 * Return 1 to indicate success since the 0 value can be a socket
378 * shutdown during the recv() or send() call.
379 */
380 return 1;
3bd1e081 381}
d41f73b7
MD
382
383/*
384 * Consume data on a file descriptor and write it on a trace file.
385 */
4078b776 386ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
387 struct lttng_consumer_local_data *ctx)
388{
1d4dfdef 389 unsigned long len, subbuf_size, padding;
d41f73b7 390 int err;
4078b776 391 ssize_t ret = 0;
d41f73b7
MD
392 int infd = stream->wait_fd;
393
394 DBG("In read_subbuffer (infd : %d)", infd);
395 /* Get the next subbuffer */
396 err = kernctl_get_next_subbuf(infd);
397 if (err != 0) {
1d4dfdef 398 ret = err;
d41f73b7
MD
399 /*
400 * This is a debug message even for single-threaded consumer,
401 * because poll() have more relaxed criterions than get subbuf,
402 * so get_subbuf may fail for short race windows where poll()
403 * would issue wakeups.
404 */
405 DBG("Reserving sub buffer failed (everything is normal, "
406 "it is due to concurrency)");
407 goto end;
408 }
409
1d4dfdef
DG
410 /* Get the full subbuffer size including padding */
411 err = kernctl_get_padded_subbuf_size(infd, &len);
412 if (err != 0) {
413 errno = -err;
414 perror("Getting sub-buffer len failed.");
415 ret = err;
416 goto end;
417 }
418
ffe60014 419 switch (stream->chan->output) {
1d4dfdef 420 case LTTNG_EVENT_SPLICE:
d41f73b7 421
1d4dfdef
DG
422 /*
423 * XXX: The lttng-modules splice "actor" does not handle copying
424 * partial pages hence only using the subbuffer size without the
425 * padding makes the splice fail.
426 */
427 subbuf_size = len;
428 padding = 0;
429
430 /* splice the subbuffer to the tracefile */
91dfef6e
DG
431 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
432 padding);
433 /*
434 * XXX: Splice does not support network streaming so the return value
435 * is simply checked against subbuf_size and not like the mmap() op.
436 */
1d4dfdef
DG
437 if (ret != subbuf_size) {
438 /*
439 * display the error but continue processing to try
440 * to release the subbuffer
441 */
442 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
443 ret, subbuf_size);
444 }
445 break;
446 case LTTNG_EVENT_MMAP:
447 /* Get subbuffer size without padding */
448 err = kernctl_get_subbuf_size(infd, &subbuf_size);
449 if (err != 0) {
450 errno = -err;
451 perror("Getting sub-buffer len failed.");
452 ret = err;
453 goto end;
454 }
47e81c02 455
1d4dfdef
DG
456 /* Make sure the tracer is not gone mad on us! */
457 assert(len >= subbuf_size);
458
459 padding = len - subbuf_size;
460
461 /* write the subbuffer to the tracefile */
91dfef6e
DG
462 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
463 padding);
464 /*
465 * The mmap operation should write subbuf_size amount of data when
466 * network streaming or the full padding (len) size when we are _not_
467 * streaming.
468 */
d88aee68
DG
469 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
470 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
1d4dfdef 471 /*
91dfef6e
DG
472 * Display the error but continue processing to try to release the
473 * subbuffer
1d4dfdef 474 */
91dfef6e
DG
475 ERR("Error writing to tracefile "
476 "(ret: %zd != len: %lu != subbuf_size: %lu)",
477 ret, len, subbuf_size);
1d4dfdef
DG
478 }
479 break;
480 default:
481 ERR("Unknown output method");
482 ret = -1;
d41f73b7
MD
483 }
484
485 err = kernctl_put_next_subbuf(infd);
486 if (err != 0) {
21073eaa 487 errno = -err;
d41f73b7
MD
488 if (errno == EFAULT) {
489 perror("Error in unreserving sub buffer\n");
490 } else if (errno == EIO) {
491 /* Should never happen with newer LTTng versions */
492 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
493 }
21073eaa
DG
494
495 ret = -err;
d41f73b7
MD
496 goto end;
497 }
498
499end:
500 return ret;
501}
502
503int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
504{
505 int ret;
ffe60014
DG
506
507 assert(stream);
508
1624d5b7 509 ret = lttng_create_output_file(stream);
ffe60014 510 if (ret < 0) {
1624d5b7 511 ERR("Creating output file");
ffe60014
DG
512 goto error;
513 }
d41f73b7 514
d41f73b7
MD
515 if (stream->output == LTTNG_EVENT_MMAP) {
516 /* get the len of the mmap region */
517 unsigned long mmap_len;
518
519 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
520 if (ret != 0) {
87dc6a9c 521 errno = -ret;
ffe60014 522 PERROR("kernctl_get_mmap_len");
d41f73b7
MD
523 goto error_close_fd;
524 }
525 stream->mmap_len = (size_t) mmap_len;
526
ffe60014
DG
527 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
528 MAP_PRIVATE, stream->wait_fd, 0);
d41f73b7 529 if (stream->mmap_base == MAP_FAILED) {
ffe60014 530 PERROR("Error mmaping");
d41f73b7
MD
531 ret = -1;
532 goto error_close_fd;
533 }
534 }
535
536 /* we return 0 to let the library handle the FD internally */
537 return 0;
538
539error_close_fd:
540 {
541 int err;
542
543 err = close(stream->out_fd);
544 assert(!err);
545 }
546error:
547 return ret;
548}
549
ca22feea
DG
550/*
551 * Check if data is still being extracted from the buffers for a specific
4e9a4686
DG
552 * stream. Consumer data lock MUST be acquired before calling this function
553 * and the stream lock.
ca22feea 554 *
6d805429 555 * Return 1 if the traced data are still getting read else 0 meaning that the
ca22feea
DG
556 * data is available for trace viewer reading.
557 */
6d805429 558int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
ca22feea
DG
559{
560 int ret;
561
562 assert(stream);
563
ca22feea
DG
564 ret = kernctl_get_next_subbuf(stream->wait_fd);
565 if (ret == 0) {
566 /* There is still data so let's put back this subbuffer. */
567 ret = kernctl_put_subbuf(stream->wait_fd);
568 assert(ret == 0);
6d805429 569 ret = 1; /* Data is pending */
4e9a4686 570 goto end;
ca22feea
DG
571 }
572
6d805429
DG
573 /* Data is NOT pending and ready to be read. */
574 ret = 0;
ca22feea 575
6efae65e
DG
576end:
577 return ret;
ca22feea 578}
This page took 0.100937 seconds and 4 git commands to generate.