2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
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.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
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.
29 #include <sys/mount.h>
30 #include <sys/resource.h>
31 #include <sys/socket.h>
33 #include <sys/types.h>
36 #include <urcu/futex.h>
37 #include <urcu/uatomic.h>
42 #include <lttng/lttng.h>
43 #include <common/common.h>
44 #include <common/compat/poll.h>
45 #include <common/compat/socket.h>
46 #include <common/defaults.h>
47 #include <common/futex.h>
48 #include <common/sessiond-comm/sessiond-comm.h>
49 #include <common/sessiond-comm/inet.h>
50 #include <common/sessiond-comm/relayd.h>
51 #include <common/uri.h>
52 #include <common/utils.h>
56 #include "lttng-relayd.h"
57 #include "lttng-viewer.h"
60 static struct lttng_uri
*live_uri
;
63 * Quit pipe for all threads. This permits a single cancellation point
64 * for all threads when receiving an event on the pipe.
66 static int live_thread_quit_pipe
[2] = { -1, -1 };
69 * This pipe is used to inform the worker thread that a command is queued and
70 * ready to be processed.
72 static int live_relay_cmd_pipe
[2] = { -1, -1 };
74 /* Shared between threads */
75 static int live_dispatch_thread_exit
;
77 static pthread_t live_listener_thread
;
78 static pthread_t live_dispatcher_thread
;
79 static pthread_t live_worker_thread
;
82 * Relay command queue.
84 * The live_thread_listener and live_thread_dispatcher communicate with this
87 static struct relay_cmd_queue viewer_cmd_queue
;
89 static uint64_t last_relay_viewer_session_id
;
99 /* Close thread quit pipes */
100 utils_close_pipe(live_thread_quit_pipe
);
105 * Write to writable pipe used to notify a thread.
108 int notify_thread_pipe(int wpipe
)
113 ret
= write(wpipe
, "!", 1);
114 } while (ret
< 0 && errno
== EINTR
);
115 if (ret
< 0 || ret
!= 1) {
116 PERROR("write poll pipe");
123 * Stop all threads by closing the thread quit pipe.
126 void stop_threads(void)
130 /* Stopping all threads */
131 DBG("Terminating all live threads");
132 ret
= notify_thread_pipe(live_thread_quit_pipe
[1]);
134 ERR("write error on thread quit pipe");
137 /* Dispatch thread */
138 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
139 futex_nto1_wake(&viewer_cmd_queue
.futex
);
143 * Init thread quit pipe.
145 * Return -1 on error or 0 if all pipes are created.
148 int init_thread_quit_pipe(void)
152 ret
= utils_create_pipe_cloexec(live_thread_quit_pipe
);
158 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
161 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
165 if (events
== NULL
|| size
== 0) {
170 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
176 ret
= lttng_poll_add(events
, live_thread_quit_pipe
[0], LPOLLIN
);
188 * Check if the thread quit pipe was triggered.
190 * Return 1 if it was triggered else 0;
193 int check_thread_quit_pipe(int fd
, uint32_t events
)
195 if (fd
== live_thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
203 * Create and init socket from uri.
206 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
209 struct lttcomm_sock
*sock
= NULL
;
211 sock
= lttcomm_alloc_sock_from_uri(uri
);
213 ERR("Allocating socket");
217 ret
= lttcomm_create_sock(sock
);
221 DBG("Listening on sock %d for live", sock
->fd
);
223 ret
= sock
->ops
->bind(sock
);
228 ret
= sock
->ops
->listen(sock
, -1);
238 lttcomm_destroy_sock(sock
);
244 * This thread manages the listening for new connections on the network
247 void *thread_listener(void *data
)
249 int i
, ret
, pollfd
, err
= -1;
251 uint32_t revents
, nb_fd
;
252 struct lttng_poll_event events
;
253 struct lttcomm_sock
*live_control_sock
;
255 DBG("[thread] Relay live listener started");
257 live_control_sock
= init_socket(live_uri
);
258 if (!live_control_sock
) {
259 goto error_sock_control
;
263 * Pass 3 as size here for the thread quit pipe, control and data socket.
265 ret
= create_thread_poll_set(&events
, 2);
267 goto error_create_poll
;
270 /* Add the control socket */
271 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
277 DBG("Listener accepting live viewers connections");
280 ret
= lttng_poll_wait(&events
, -1);
283 * Restart interrupted system call.
285 if (errno
== EINTR
) {
292 DBG("Relay new viewer connection received");
293 for (i
= 0; i
< nb_fd
; i
++) {
294 /* Fetch once the poll data */
295 revents
= LTTNG_POLL_GETEV(&events
, i
);
296 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
298 /* Thread quit pipe has been closed. Killing thread. */
299 ret
= check_thread_quit_pipe(pollfd
, revents
);
305 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
306 ERR("socket poll error");
308 } else if (revents
& LPOLLIN
) {
310 * Get allocated in this thread, enqueued to a global queue,
311 * dequeued and freed in the worker thread.
313 struct relay_command
*relay_cmd
;
314 struct lttcomm_sock
*newsock
;
316 relay_cmd
= zmalloc(sizeof(*relay_cmd
));
318 PERROR("relay command zmalloc");
322 assert(pollfd
== live_control_sock
->fd
);
323 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
325 PERROR("accepting control sock");
329 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
330 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
333 PERROR("setsockopt inet");
334 lttcomm_destroy_sock(newsock
);
338 relay_cmd
->sock
= newsock
;
341 * Lock free enqueue the request.
343 cds_wfq_enqueue(&viewer_cmd_queue
.queue
, &relay_cmd
->node
);
346 * Wake the dispatch queue futex. Implicit memory
347 * barrier with the exchange in cds_wfq_enqueue.
349 futex_nto1_wake(&viewer_cmd_queue
.futex
);
357 lttng_poll_clean(&events
);
359 if (live_control_sock
->fd
>= 0) {
360 ret
= live_control_sock
->ops
->close(live_control_sock
);
365 lttcomm_destroy_sock(live_control_sock
);
368 DBG("Live viewer listener thread exited with error");
370 DBG("Live viewer listener thread cleanup complete");
376 * This thread manages the dispatching of the requests to worker threads
379 void *thread_dispatcher(void *data
)
382 struct cds_wfq_node
*node
;
383 struct relay_command
*relay_cmd
= NULL
;
385 DBG("[thread] Live viewer relay dispatcher started");
387 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
388 /* Atomically prepare the queue futex */
389 futex_nto1_prepare(&viewer_cmd_queue
.futex
);
392 /* Dequeue commands */
393 node
= cds_wfq_dequeue_blocking(&viewer_cmd_queue
.queue
);
395 DBG("Woken up but nothing in the live-viewer "
396 "relay command queue");
397 /* Continue thread execution */
401 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
402 DBG("Dispatching viewer request waiting on sock %d",
403 relay_cmd
->sock
->fd
);
406 * Inform worker thread of the new request. This call is blocking
407 * so we can be assured that the data will be read at some point in
408 * time or wait to the end of the world :)
411 ret
= write(live_relay_cmd_pipe
[1], relay_cmd
,
413 } while (ret
< 0 && errno
== EINTR
);
415 if (ret
< 0 || ret
!= sizeof(struct relay_command
)) {
416 PERROR("write cmd pipe");
419 } while (node
!= NULL
);
421 /* Futex wait on queue. Blocking call on futex() */
422 futex_nto1_wait(&viewer_cmd_queue
.futex
);
426 DBG("Live viewer dispatch thread dying");
432 * Establish connection with the viewer and check the versions.
434 * Return 0 on success or else negative value.
437 int viewer_connect(struct relay_command
*cmd
)
440 struct lttng_viewer_connect reply
, msg
;
444 cmd
->version_check_done
= 1;
446 /* Get version from the other side. */
447 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &msg
, sizeof(msg
), 0);
448 if (ret
< 0 || ret
!= sizeof(msg
)) {
450 /* Orderly shutdown. Not necessary to print an error. */
451 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
453 ERR("Relay failed to receive the version values.");
459 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
460 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
462 /* Major versions must be the same */
463 if (reply
.major
!= be32toh(msg
.major
)) {
464 DBG("Incompatible major versions (%u vs %u)", reply
.major
,
470 cmd
->major
= reply
.major
;
471 /* We adapt to the lowest compatible version */
472 if (reply
.minor
<= be32toh(msg
.minor
)) {
473 cmd
->minor
= reply
.minor
;
475 cmd
->minor
= be32toh(msg
.minor
);
478 if (be32toh(msg
.type
) == VIEWER_CLIENT_COMMAND
) {
479 cmd
->type
= RELAY_VIEWER_COMMAND
;
480 } else if (be32toh(msg
.type
) == VIEWER_CLIENT_NOTIFICATION
) {
481 cmd
->type
= RELAY_VIEWER_NOTIFICATION
;
483 ERR("Unknown connection type : %u", be32toh(msg
.type
));
488 reply
.major
= htobe32(reply
.major
);
489 reply
.minor
= htobe32(reply
.minor
);
490 if (cmd
->type
== RELAY_VIEWER_COMMAND
) {
491 reply
.viewer_session_id
= htobe64(++last_relay_viewer_session_id
);
493 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
494 sizeof(struct lttng_viewer_connect
), 0);
496 ERR("Relay sending version");
499 DBG("Version check done using protocol %u.%u", cmd
->major
, cmd
->minor
);
507 * Send the viewer the list of current sessions.
509 * Return 0 on success or else a negative value.
512 int viewer_list_sessions(struct relay_command
*cmd
,
513 struct lttng_ht
*sessions_ht
)
516 struct lttng_viewer_list_sessions session_list
;
518 long approx_before
, approx_after
;
519 struct lttng_ht_node_ulong
*node
;
520 struct lttng_ht_iter iter
;
521 struct lttng_viewer_session send_session
;
522 struct relay_session
*session
;
524 DBG("List sessions received");
526 if (cmd
->version_check_done
== 0) {
527 ERR("Trying to list sessions before version check");
533 cds_lfht_count_nodes(sessions_ht
->ht
, &approx_before
, &count
, &approx_after
);
534 session_list
.sessions_count
= htobe32(count
);
536 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &session_list
,
537 sizeof(session_list
), 0);
539 ERR("Relay sending sessions list");
543 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, node
, node
) {
544 node
= lttng_ht_iter_get_node_ulong(&iter
);
548 session
= caa_container_of(node
, struct relay_session
, session_n
);
550 strncpy(send_session
.session_name
, session
->session_name
,
551 sizeof(send_session
.session_name
));
552 strncpy(send_session
.hostname
, session
->hostname
,
553 sizeof(send_session
.hostname
));
554 send_session
.id
= htobe64(session
->id
);
555 send_session
.live_timer
= htobe32(session
->live_timer
);
556 send_session
.clients
= htobe32(session
->viewer_attached
);
558 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &send_session
,
559 sizeof(send_session
), 0);
561 ERR("Relay sending session info");
578 * Allocate and init a new viewer_stream.
580 * Copies the values from the stream passed in parameter and insert the new
581 * stream in the viewer_streams_ht.
583 * MUST be called with rcu_read_lock held.
585 * Returns 0 on success or a negative value on error.
588 int init_viewer_stream(struct relay_stream
*stream
)
591 struct relay_viewer_stream
*viewer_stream
;
595 viewer_stream
= zmalloc(sizeof(*viewer_stream
));
596 if (!viewer_stream
) {
597 PERROR("relay viewer stream zmalloc");
602 viewer_stream
->read_fd
= -1;
603 viewer_stream
->index_read_fd
= -1;
604 viewer_stream
->session_id
= stream
->session
->id
;
605 viewer_stream
->stream_handle
= stream
->stream_handle
;
606 viewer_stream
->path_name
= strndup(stream
->path_name
,
607 LTTNG_VIEWER_PATH_MAX
);
608 viewer_stream
->channel_name
= strndup(stream
->channel_name
,
609 LTTNG_VIEWER_NAME_MAX
);
610 viewer_stream
->total_index_received
= stream
->total_index_received
;
611 viewer_stream
->tracefile_size
= stream
->tracefile_size
;
612 viewer_stream
->tracefile_count
= stream
->tracefile_count
;
613 viewer_stream
->metadata_flag
= stream
->metadata_flag
;
616 * This is to avoid a race between the initialization of this object and
617 * the close of the given stream. If the stream is unable to find this
618 * viewer stream when closing, this copy will at least take the latest
621 viewer_stream
->total_index_received
= stream
->total_index_received
;
624 * The deletion of this ctf_trace object is only done in a call RCU of the
625 * relay stream making it valid as long as we have the read side lock.
627 viewer_stream
->ctf_trace
= stream
->ctf_trace
;
628 uatomic_inc(&viewer_stream
->ctf_trace
->refcount
);
630 lttng_ht_node_init_u64(&viewer_stream
->stream_n
, stream
->stream_handle
);
631 lttng_ht_add_unique_u64(viewer_streams_ht
, &viewer_stream
->stream_n
);
640 * Send the viewer the list of current sessions.
643 int viewer_attach_session(struct relay_command
*cmd
,
644 struct lttng_ht
*sessions_ht
)
646 int ret
, send_streams
= 0, nb_streams
= 0;
647 struct lttng_viewer_attach_session_request request
;
648 struct lttng_viewer_attach_session_response response
;
649 struct lttng_viewer_stream send_stream
;
650 struct relay_stream
*stream
;
651 struct relay_viewer_stream
*viewer_stream
;
652 struct lttng_ht_node_ulong
*node
;
653 struct lttng_ht_node_u64
*node64
;
654 struct lttng_ht_iter iter
;
655 struct relay_session
*session
;
660 DBG("Attach session received");
662 if (cmd
->version_check_done
== 0) {
663 ERR("Trying to attach session before version check");
668 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request
, sizeof(request
), 0);
669 if (ret
< 0 || ret
!= sizeof(request
)) {
671 /* Orderly shutdown. Not necessary to print an error. */
672 DBG("Socket %d did an orderly shutdown", cmd
->sock
->fd
);
674 ERR("Relay failed to receive the attach parameters.");
681 lttng_ht_lookup(sessions_ht
,
682 (void *)((unsigned long) be64toh(request
.session_id
)), &iter
);
683 node
= lttng_ht_iter_get_node_ulong(&iter
);
685 DBG("Relay session %" PRIu64
" not found",
686 be64toh(request
.session_id
));
687 response
.status
= htobe32(VIEWER_ATTACH_UNK
);
691 session
= caa_container_of(node
, struct relay_session
, session_n
);
692 if (cmd
->session_id
== session
->id
) {
693 /* Same viewer already attached, just send the stream list. */
695 response
.status
= htobe32(VIEWER_ATTACH_OK
);
696 } else if (session
->viewer_attached
!= 0) {
697 DBG("Already a viewer attached");
698 response
.status
= htobe32(VIEWER_ATTACH_ALREADY
);
700 } else if (session
->live_timer
== 0) {
701 DBG("Not live session");
702 response
.status
= htobe32(VIEWER_ATTACH_NOT_LIVE
);
705 session
->viewer_attached
++;
707 response
.status
= htobe32(VIEWER_ATTACH_OK
);
708 cmd
->session_id
= session
->id
;
709 cmd
->session
= session
;
712 switch (be32toh(request
.seek
)) {
713 case VIEWER_SEEK_BEGINNING
:
714 /* Default behaviour. */
716 case VIEWER_SEEK_LAST
:
720 ERR("Wrong seek parameter");
721 response
.status
= htobe32(VIEWER_ATTACH_SEEK_ERR
);
727 /* We should only be there if we have a session to attach to. */
731 * Fill the viewer_streams_ht to count the number of streams
732 * ready to be sent and avoid concurrency issues on the
733 * relay_streams_ht and don't rely on a total session stream count.
735 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, node
, node
) {
736 struct relay_viewer_stream
*vstream
;
738 node
= lttng_ht_iter_get_node_ulong(&iter
);
742 stream
= caa_container_of(node
, struct relay_stream
, stream_n
);
743 if (stream
->session
!= cmd
->session
) {
748 * Don't send streams with no ctf_trace, they are not ready to be
751 if (!stream
->ctf_trace
) {
755 vstream
= live_find_viewer_stream_by_id(stream
->stream_handle
);
757 ret
= init_viewer_stream(stream
);
764 response
.streams_count
= htobe32(nb_streams
);
768 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &response
, sizeof(response
), 0);
770 ERR("Relay sending viewer attach response");
775 * Unknown or busy session, just return gracefully, the viewer knows what
783 /* We should only be there if we have a session to attach to. */
785 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, node
, node
) {
786 node64
= lttng_ht_iter_get_node_u64(&iter
);
790 viewer_stream
= caa_container_of(node64
, struct relay_viewer_stream
,
792 if (viewer_stream
->session_id
!= cmd
->session
->id
) {
796 send_stream
.id
= htobe64(viewer_stream
->stream_handle
);
797 send_stream
.ctf_trace_id
= htobe64(viewer_stream
->ctf_trace
->id
);
798 send_stream
.metadata_flag
= htobe32(viewer_stream
->metadata_flag
);
799 strncpy(send_stream
.path_name
, viewer_stream
->path_name
,
800 sizeof(send_stream
.path_name
));
801 strncpy(send_stream
.channel_name
, viewer_stream
->channel_name
,
802 sizeof(send_stream
.channel_name
));
804 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &send_stream
,
805 sizeof(send_stream
), 0);
807 ERR("Relay sending stream %" PRIu64
, viewer_stream
->stream_handle
);
810 DBG("Sent stream %" PRIu64
" to viewer", viewer_stream
->stream_handle
);
822 * Open index file using a given viewer stream.
824 * Return 0 on success or else a negative value.
826 static int open_index(struct relay_viewer_stream
*stream
)
829 char fullpath
[PATH_MAX
];
830 struct lttng_packet_index_file_hdr hdr
;
832 if (stream
->tracefile_size
> 0) {
833 /* For now we don't support on-disk ring buffer. */
837 ret
= snprintf(fullpath
, sizeof(fullpath
), "%s/" DEFAULT_INDEX_DIR
838 "/%s" DEFAULT_INDEX_FILE_SUFFIX
,
839 stream
->path_name
, stream
->channel_name
);
841 PERROR("snprintf index path");
846 DBG("Opening index file %s in read only", fullpath
);
847 ret
= open(fullpath
, O_RDONLY
);
849 if (errno
== ENOENT
) {
853 PERROR("opening index in read-only");
857 stream
->index_read_fd
= ret
;
858 DBG("Opening index file %s in read only, (fd: %d)", fullpath
, ret
);
861 ret
= read(stream
->index_read_fd
, &hdr
, sizeof(hdr
));
862 } while (ret
< 0 && errno
== EINTR
);
864 PERROR("Reading index header");
867 if (strncmp(hdr
.magic
, INDEX_MAGIC
, sizeof(hdr
.magic
)) != 0) {
868 ERR("Invalid header magic");
872 if (be32toh(hdr
.index_major
) != INDEX_MAJOR
||
873 be32toh(hdr
.index_minor
) != INDEX_MINOR
) {
874 ERR("Invalid header version");
886 * Get viewer stream from stream id.
888 * RCU read side lock MUST be acquired.
890 struct relay_viewer_stream
*live_find_viewer_stream_by_id(uint64_t stream_id
)
892 struct lttng_ht_node_u64
*node
;
893 struct lttng_ht_iter iter
;
894 struct relay_viewer_stream
*stream
= NULL
;
896 lttng_ht_lookup(viewer_streams_ht
, &stream_id
, &iter
);
897 node
= lttng_ht_iter_get_node_u64(&iter
);
899 DBG("Relay viewer stream %" PRIu64
" not found", stream_id
);
902 stream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
909 * Send the next index for a stream.
911 * Return 0 on success or else a negative value.
914 int viewer_get_next_index(struct relay_command
*cmd
,
915 struct lttng_ht
*sessions_ht
)
918 struct lttng_viewer_get_next_index request_index
;
919 struct lttng_viewer_index viewer_index
;
920 struct lttng_packet_index packet_index
;
921 struct relay_viewer_stream
*vstream
;
922 struct relay_stream
*rstream
;
927 DBG("Viewer get next index");
929 if (cmd
->version_check_done
== 0) {
930 ERR("Trying to request index before version check");
935 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request_index
,
936 sizeof(request_index
), 0);
937 if (ret
< 0 || ret
!= sizeof(request_index
)) {
939 ERR("Relay didn't receive the whole packet");
944 vstream
= live_find_viewer_stream_by_id(be64toh(request_index
.stream_id
));
950 memset(&viewer_index
, 0, sizeof(viewer_index
));
953 * The viewer should not ask for index on metadata stream.
955 if (vstream
->metadata_flag
) {
956 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
960 /* First time, we open the index file */
961 if (vstream
->index_read_fd
< 0) {
962 ret
= open_index(vstream
);
965 * The index is created only when the first data packet arrives, it
966 * might not be ready at the beginning of the session
968 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
970 } else if (ret
< 0) {
971 viewer_index
.status
= htobe32(VIEWER_INDEX_ERR
);
976 rstream
= relay_stream_find_by_id(vstream
->stream_handle
);
978 if (rstream
->beacon_ts_end
!= -1ULL &&
979 vstream
->last_sent_index
== rstream
->total_index_received
) {
980 viewer_index
.status
= htobe32(VIEWER_INDEX_INACTIVE
);
981 viewer_index
.timestamp_end
= htobe64(rstream
->beacon_ts_end
);
985 if (rstream
->total_index_received
<= vstream
->last_sent_index
) {
986 /* No new index to send, retry later. */
987 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
990 } else if (!rstream
&&
991 vstream
->total_index_received
== vstream
->last_sent_index
) {
992 /* Last index sent and stream closed */
993 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
997 if (!vstream
->ctf_trace
->metadata_received
||
998 vstream
->ctf_trace
->metadata_received
>
999 vstream
->ctf_trace
->metadata_sent
) {
1000 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1004 ret
= read(vstream
->index_read_fd
, &packet_index
,
1005 sizeof(packet_index
));
1006 } while (ret
< 0 && errno
== EINTR
);
1007 if (ret
< sizeof(packet_index
)) {
1008 PERROR("Relay reading index file");
1009 viewer_index
.status
= htobe32(VIEWER_INDEX_ERR
);
1011 viewer_index
.status
= htobe32(VIEWER_INDEX_OK
);
1012 vstream
->last_sent_index
++;
1016 * Indexes are stored in big endian, no need to switch before sending.
1018 viewer_index
.offset
= packet_index
.offset
;
1019 viewer_index
.packet_size
= packet_index
.packet_size
;
1020 viewer_index
.content_size
= packet_index
.content_size
;
1021 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1022 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1023 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1024 viewer_index
.stream_id
= packet_index
.stream_id
;
1027 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1028 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &viewer_index
,
1029 sizeof(viewer_index
), 0);
1031 ERR("Relay index to viewer");
1035 DBG("Index %" PRIu64
"for stream %" PRIu64
"sent",
1036 vstream
->last_sent_index
, vstream
->stream_handle
);
1047 * Send the next index for a stream
1049 * Return 0 on success or else a negative value.
1052 int viewer_get_packet(struct relay_command
*cmd
)
1054 int ret
, send_data
= 0;
1058 struct lttng_viewer_get_packet get_packet_info
;
1059 struct lttng_viewer_trace_packet reply
;
1060 struct relay_viewer_stream
*stream
;
1064 DBG2("Relay get data packet");
1066 if (cmd
->version_check_done
== 0) {
1067 ERR("Trying to get packet before version check");
1072 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &get_packet_info
,
1073 sizeof(get_packet_info
), 0);
1074 if (ret
< 0 || ret
!= sizeof(get_packet_info
)) {
1076 ERR("Relay didn't receive the whole packet");
1081 stream
= live_find_viewer_stream_by_id(be64toh(get_packet_info
.stream_id
));
1085 assert(stream
->ctf_trace
);
1088 * First time we read this stream, we need open the tracefile, we should
1089 * only arrive here if an index has already been sent to the viewer, so the
1090 * tracefile must exist, if it does not it is a fatal error.
1092 if (stream
->read_fd
< 0) {
1093 char fullpath
[PATH_MAX
];
1095 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1096 stream
->channel_name
);
1100 ret
= open(fullpath
, O_RDONLY
);
1102 PERROR("Relay opening trace file");
1105 stream
->read_fd
= ret
;
1108 memset(&reply
, 0, sizeof(reply
));
1110 if (!stream
->ctf_trace
->metadata_received
||
1111 stream
->ctf_trace
->metadata_received
>
1112 stream
->ctf_trace
->metadata_sent
) {
1113 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1114 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1119 len
= be32toh(get_packet_info
.len
);
1120 data
= zmalloc(len
);
1122 PERROR("relay data zmalloc");
1126 ret
= lseek(stream
->read_fd
, be64toh(get_packet_info
.offset
), SEEK_SET
);
1131 read_len
= read(stream
->read_fd
, data
, len
);
1132 if (read_len
< (ssize_t
) len
) {
1133 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1134 stream
->read_fd
, be64toh(get_packet_info
.offset
));
1137 reply
.status
= htobe32(VIEWER_GET_PACKET_OK
);
1138 reply
.len
= htobe32(len
);
1143 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1146 reply
.flags
= htobe32(reply
.flags
);
1147 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1149 ERR("Relay data header to viewer");
1154 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, data
, len
, 0);
1156 ERR("Relay send data to viewer");
1161 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1162 be64toh(get_packet_info
.stream_id
));
1173 * Send the session's metadata
1175 * Return 0 on success else a negative value.
1178 int viewer_get_metadata(struct relay_command
*cmd
)
1184 struct lttng_viewer_get_metadata request
;
1185 struct lttng_viewer_metadata_packet reply
;
1186 struct relay_viewer_stream
*stream
;
1190 DBG("Relay get metadata");
1192 if (cmd
->version_check_done
== 0) {
1193 ERR("Trying to get metadata before version check");
1198 ret
= cmd
->sock
->ops
->recvmsg(cmd
->sock
, &request
,
1199 sizeof(request
), 0);
1200 if (ret
< 0 || ret
!= sizeof(request
)) {
1202 ERR("Relay didn't receive the whole packet");
1207 stream
= live_find_viewer_stream_by_id(be64toh(request
.stream_id
));
1208 if (!stream
|| !stream
->metadata_flag
) {
1209 ERR("Invalid metadata stream");
1212 assert(stream
->ctf_trace
);
1213 assert(stream
->ctf_trace
->metadata_sent
<=
1214 stream
->ctf_trace
->metadata_received
);
1216 len
= stream
->ctf_trace
->metadata_received
-
1217 stream
->ctf_trace
->metadata_sent
;
1219 reply
.status
= htobe32(VIEWER_NO_NEW_METADATA
);
1223 /* first time, we open the metadata file */
1224 if (stream
->read_fd
< 0) {
1225 char fullpath
[PATH_MAX
];
1227 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1228 stream
->channel_name
);
1232 ret
= open(fullpath
, O_RDONLY
);
1234 PERROR("Relay opening metadata file");
1237 stream
->read_fd
= ret
;
1240 reply
.len
= htobe64(len
);
1241 data
= zmalloc(len
);
1243 PERROR("viewer metadata zmalloc");
1247 read_len
= read(stream
->read_fd
, data
, len
);
1248 if (read_len
< (ssize_t
) len
) {
1249 PERROR("Relay reading metadata file");
1252 stream
->ctf_trace
->metadata_sent
+= read_len
;
1253 reply
.status
= htobe32(VIEWER_METADATA_OK
);
1257 reply
.status
= htobe32(VIEWER_METADATA_ERR
);
1260 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
, sizeof(reply
), 0);
1262 ERR("Relay data header to viewer");
1267 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, data
, len
, 0);
1269 ERR("Relay send data to viewer");
1274 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1275 be64toh(request
.stream_id
));
1277 DBG("Metadata sent");
1287 * live_relay_unknown_command: send -1 if received unknown command
1290 void live_relay_unknown_command(struct relay_command
*cmd
)
1292 struct lttcomm_relayd_generic_reply reply
;
1295 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1296 ret
= cmd
->sock
->ops
->sendmsg(cmd
->sock
, &reply
,
1297 sizeof(struct lttcomm_relayd_generic_reply
), 0);
1299 ERR("Relay sending unknown command");
1304 * Process the commands received on the control socket
1307 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1308 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1312 switch (be32toh(recv_hdr
->cmd
)) {
1313 case VIEWER_CONNECT
:
1314 ret
= viewer_connect(cmd
);
1316 case VIEWER_LIST_SESSIONS
:
1317 ret
= viewer_list_sessions(cmd
, sessions_ht
);
1319 case VIEWER_ATTACH_SESSION
:
1320 ret
= viewer_attach_session(cmd
, sessions_ht
);
1322 case VIEWER_GET_NEXT_INDEX
:
1323 ret
= viewer_get_next_index(cmd
, sessions_ht
);
1325 case VIEWER_GET_PACKET
:
1326 ret
= viewer_get_packet(cmd
);
1328 case VIEWER_GET_METADATA
:
1329 ret
= viewer_get_metadata(cmd
);
1332 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr
->cmd
));
1333 live_relay_unknown_command(cmd
);
1343 void cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
1349 lttng_poll_del(events
, pollfd
);
1351 ret
= close(pollfd
);
1353 ERR("Closing pollfd %d", pollfd
);
1358 * Create and add connection to the given hash table.
1360 * Return poll add value or else -1 on error.
1363 int add_connection(int fd
, struct lttng_poll_event
*events
,
1364 struct lttng_ht
*relay_connections_ht
)
1367 struct relay_command
*relay_connection
;
1370 assert(relay_connections_ht
);
1372 relay_connection
= zmalloc(sizeof(struct relay_command
));
1373 if (relay_connection
== NULL
) {
1374 PERROR("Relay command zmalloc");
1379 ret
= read(fd
, relay_connection
, sizeof(*relay_connection
));
1380 } while (ret
< 0 && errno
== EINTR
);
1381 if (ret
< 0 || ret
< sizeof(*relay_connection
)) {
1382 PERROR("read relay cmd pipe");
1386 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1387 (unsigned long) relay_connection
->sock
->fd
);
1389 lttng_ht_add_unique_ulong(relay_connections_ht
,
1390 &relay_connection
->sock_n
);
1393 return lttng_poll_add(events
, relay_connection
->sock
->fd
,
1394 LPOLLIN
| LPOLLRDHUP
);
1397 free(relay_connection
);
1403 void deferred_free_connection(struct rcu_head
*head
)
1405 struct relay_command
*relay_connection
=
1406 caa_container_of(head
, struct relay_command
, rcu_node
);
1408 if (relay_connection
->session
&&
1409 relay_connection
->session
->viewer_attached
> 0) {
1410 relay_connection
->session
->viewer_attached
--;
1412 lttcomm_destroy_sock(relay_connection
->sock
);
1413 free(relay_connection
);
1417 void deferred_free_viewer_stream(struct rcu_head
*head
)
1419 struct relay_viewer_stream
*stream
=
1420 caa_container_of(head
, struct relay_viewer_stream
, rcu_node
);
1422 if (stream
->ctf_trace
) {
1423 uatomic_dec(&stream
->ctf_trace
->refcount
);
1424 assert(uatomic_read(&stream
->ctf_trace
->refcount
) >= 0);
1425 if (uatomic_read(&stream
->ctf_trace
->refcount
) == 0) {
1426 DBG("Freeing ctf_trace %" PRIu64
, stream
->ctf_trace
->id
);
1427 free(stream
->ctf_trace
);
1431 free(stream
->path_name
);
1432 free(stream
->channel_name
);
1437 void viewer_del_streams(uint64_t session_id
)
1440 struct relay_viewer_stream
*stream
;
1441 struct lttng_ht_node_u64
*node
;
1442 struct lttng_ht_iter iter
;
1445 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, node
, node
) {
1446 node
= lttng_ht_iter_get_node_u64(&iter
);
1451 stream
= caa_container_of(node
, struct relay_viewer_stream
, stream_n
);
1452 if (stream
->session_id
!= session_id
) {
1456 if (stream
->read_fd
> 0) {
1457 ret
= close(stream
->read_fd
);
1459 PERROR("close read_fd");
1462 if (stream
->index_read_fd
> 0) {
1463 ret
= close(stream
->index_read_fd
);
1465 PERROR("close index_read_fd");
1468 if (stream
->metadata_flag
&& stream
->ctf_trace
) {
1469 stream
->ctf_trace
->metadata_sent
= 0;
1471 ret
= lttng_ht_del(viewer_streams_ht
, &iter
);
1473 call_rcu(&stream
->rcu_node
, deferred_free_viewer_stream
);
1479 * Delete and free a connection.
1481 * RCU read side lock MUST be acquired.
1484 void del_connection(struct lttng_ht
*relay_connections_ht
,
1485 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
)
1489 assert(relay_connections_ht
);
1491 assert(relay_connection
);
1493 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1496 viewer_del_streams(relay_connection
->session_id
);
1498 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
1502 * This thread does the actual work
1505 void *thread_worker(void *data
)
1509 struct relay_command
*relay_connection
;
1510 struct lttng_poll_event events
;
1511 struct lttng_ht
*relay_connections_ht
;
1512 struct lttng_ht_node_ulong
*node
;
1513 struct lttng_ht_iter iter
;
1514 struct lttng_viewer_cmd recv_hdr
;
1515 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
1516 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
1518 DBG("[thread] Live viewer relay worker started");
1520 rcu_register_thread();
1522 /* table of connections indexed on socket */
1523 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1524 if (!relay_connections_ht
) {
1525 goto relay_connections_ht_error
;
1528 ret
= create_thread_poll_set(&events
, 2);
1530 goto error_poll_create
;
1533 ret
= lttng_poll_add(&events
, live_relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1542 /* Infinite blocking call, waiting for transmission */
1543 DBG3("Relayd live viewer worker thread polling...");
1544 ret
= lttng_poll_wait(&events
, -1);
1547 * Restart interrupted system call.
1549 if (errno
== EINTR
) {
1558 * Process control. The control connection is prioritised so we don't
1559 * starve it with high throughput tracing data on the data
1562 for (i
= 0; i
< nb_fd
; i
++) {
1563 /* Fetch once the poll data */
1564 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1565 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1567 /* Thread quit pipe has been closed. Killing thread. */
1568 ret
= check_thread_quit_pipe(pollfd
, revents
);
1574 /* Inspect the relay cmd pipe for new connection */
1575 if (pollfd
== live_relay_cmd_pipe
[0]) {
1576 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1577 ERR("Relay live pipe error");
1579 } else if (revents
& LPOLLIN
) {
1580 DBG("Relay live viewer command received");
1581 ret
= add_connection(live_relay_cmd_pipe
[0],
1582 &events
, relay_connections_ht
);
1587 } else if (revents
) {
1589 lttng_ht_lookup(relay_connections_ht
,
1590 (void *)((unsigned long) pollfd
), &iter
);
1591 node
= lttng_ht_iter_get_node_ulong(&iter
);
1593 DBG2("Relay viewer sock %d not found", pollfd
);
1597 relay_connection
= caa_container_of(node
, struct relay_command
,
1600 if (revents
& (LPOLLERR
)) {
1601 cleanup_poll_connection(&events
, pollfd
);
1602 del_connection(relay_connections_ht
, &iter
,
1604 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1605 DBG("Viewer socket %d hung up", pollfd
);
1606 cleanup_poll_connection(&events
, pollfd
);
1607 del_connection(relay_connections_ht
, &iter
,
1609 } else if (revents
& LPOLLIN
) {
1610 ret
= relay_connection
->sock
->ops
->recvmsg(
1611 relay_connection
->sock
, &recv_hdr
,
1612 sizeof(struct lttng_viewer_cmd
),
1614 /* connection closed */
1616 cleanup_poll_connection(&events
, pollfd
);
1617 del_connection( relay_connections_ht
, &iter
,
1619 DBG("Viewer control connection closed with %d",
1622 if (relay_connection
->session
) {
1623 DBG2("Relay viewer worker receiving data for "
1624 "session: %" PRIu64
,
1625 relay_connection
->session
->id
);
1627 ret
= process_control(&recv_hdr
, relay_connection
,
1630 /* Clear the session on error. */
1631 cleanup_poll_connection(&events
, pollfd
);
1632 del_connection(relay_connections_ht
, &iter
,
1634 DBG("Viewer connection closed with %d", pollfd
);
1645 lttng_poll_clean(&events
);
1647 /* empty the hash table and free the memory */
1649 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
1650 node
= lttng_ht_iter_get_node_ulong(&iter
);
1655 relay_connection
= caa_container_of(node
, struct relay_command
,
1657 del_connection(relay_connections_ht
, &iter
, relay_connection
);
1661 lttng_ht_destroy(relay_connections_ht
);
1662 relay_connections_ht_error
:
1663 /* Close relay cmd pipes */
1664 utils_close_pipe(live_relay_cmd_pipe
);
1666 DBG("Viewer worker thread exited with error");
1668 DBG("Viewer worker thread cleanup complete");
1670 rcu_unregister_thread();
1675 * Create the relay command pipe to wake thread_manage_apps.
1676 * Closed in cleanup().
1678 static int create_relay_cmd_pipe(void)
1682 ret
= utils_create_pipe_cloexec(live_relay_cmd_pipe
);
1687 void live_stop_threads()
1694 ret
= pthread_join(live_listener_thread
, &status
);
1696 PERROR("pthread_join live listener");
1697 goto error
; /* join error, exit without cleanup */
1700 ret
= pthread_join(live_worker_thread
, &status
);
1702 PERROR("pthread_join live worker");
1703 goto error
; /* join error, exit without cleanup */
1706 ret
= pthread_join(live_dispatcher_thread
, &status
);
1708 PERROR("pthread_join live dispatcher");
1709 goto error
; /* join error, exit without cleanup */
1721 int live_start_threads(struct lttng_uri
*uri
,
1722 struct relay_local_data
*relay_ctx
)
1731 /* Create thread quit pipe */
1732 if ((ret
= init_thread_quit_pipe()) < 0) {
1736 /* Check if daemon is UID = 0 */
1737 is_root
= !getuid();
1740 if (live_uri
->port
< 1024) {
1741 ERR("Need to be root to use ports < 1024");
1747 /* Setup the thread apps communication pipe. */
1748 if ((ret
= create_relay_cmd_pipe()) < 0) {
1752 /* Init relay command queue. */
1753 cds_wfq_init(&viewer_cmd_queue
.queue
);
1755 /* Set up max poll set size */
1756 lttng_poll_set_max_size();
1758 /* Setup the dispatcher thread */
1759 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
1760 thread_dispatcher
, (void *) NULL
);
1762 PERROR("pthread_create viewer dispatcher");
1763 goto exit_dispatcher
;
1766 /* Setup the worker thread */
1767 ret
= pthread_create(&live_worker_thread
, NULL
,
1768 thread_worker
, relay_ctx
);
1770 PERROR("pthread_create viewer worker");
1774 /* Setup the listener thread */
1775 ret
= pthread_create(&live_listener_thread
, NULL
,
1776 thread_listener
, (void *) NULL
);
1778 PERROR("pthread_create viewer listener");
1786 ret
= pthread_join(live_listener_thread
, &status
);
1788 PERROR("pthread_join live listener");
1789 goto error
; /* join error, exit without cleanup */
1793 ret
= pthread_join(live_worker_thread
, &status
);
1795 PERROR("pthread_join live worker");
1796 goto error
; /* join error, exit without cleanup */
1800 ret
= pthread_join(live_dispatcher_thread
, &status
);
1802 PERROR("pthread_join live dispatcher");
1803 goto error
; /* join error, exit without cleanup */