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/index/index.h>
49 #include <common/sessiond-comm/sessiond-comm.h>
50 #include <common/sessiond-comm/inet.h>
51 #include <common/sessiond-comm/relayd.h>
52 #include <common/uri.h>
53 #include <common/utils.h>
57 #include "lttng-relayd.h"
59 #include "health-relayd.h"
60 #include "testpoint.h"
61 #include "viewer-stream.h"
63 static struct lttng_uri
*live_uri
;
66 * This pipe is used to inform the worker thread that a command is queued and
67 * ready to be processed.
69 static int live_relay_cmd_pipe
[2] = { -1, -1 };
71 /* Shared between threads */
72 static int live_dispatch_thread_exit
;
74 static pthread_t live_listener_thread
;
75 static pthread_t live_dispatcher_thread
;
76 static pthread_t live_worker_thread
;
79 * Relay command queue.
81 * The live_thread_listener and live_thread_dispatcher communicate with this
84 static struct relay_cmd_queue viewer_cmd_queue
;
86 static uint64_t last_relay_viewer_session_id
;
100 * Receive a request buffer using a given socket, destination allocated buffer
103 * Return the size of the received message or else a negative value on error
104 * with errno being set by recvmsg() syscall.
107 ssize_t
recv_request(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
114 ret
= sock
->ops
->recvmsg(sock
, buf
, size
, 0);
115 if (ret
< 0 || ret
!= size
) {
117 /* Orderly shutdown. Not necessary to print an error. */
118 DBG("Socket %d did an orderly shutdown", sock
->fd
);
120 ERR("Relay failed to receive request.");
129 * Send a response buffer using a given socket, source allocated buffer of
132 * Return the size of the sent message or else a negative value on error with
133 * errno being set by sendmsg() syscall.
136 ssize_t
send_response(struct lttcomm_sock
*sock
, void *buf
, size_t size
)
143 ret
= sock
->ops
->sendmsg(sock
, buf
, size
, 0);
145 ERR("Relayd failed to send response.");
152 * Atomically check if new streams got added in the session since the last
153 * check and reset the flag to 0.
155 * Returns 1 if new streams got added, 0 if nothing changed, a negative value
159 int check_new_streams(uint64_t session_id
, struct lttng_ht
*sessions_ht
)
162 unsigned long current_val
;
163 struct relay_session
*session
;
167 session
= session_find_by_id(sessions_ht
, session_id
);
169 DBG("Relay session %" PRIu64
" not found", session_id
);
174 current_val
= uatomic_cmpxchg(&session
->new_streams
, 1, 0);
182 * Send viewer streams to the given socket. The ignore_sent_flag indicates if
183 * this function should ignore the sent flag or not.
185 * Return 0 on success or else a negative value.
188 ssize_t
send_viewer_streams(struct lttcomm_sock
*sock
,
189 struct relay_session
*session
, unsigned int ignore_sent_flag
)
192 struct lttng_viewer_stream send_stream
;
193 struct lttng_ht_iter iter
;
194 struct relay_viewer_stream
*vstream
;
200 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, vstream
,
202 health_code_update();
204 /* Ignore if not the same session. */
205 if (vstream
->session_id
!= session
->id
||
206 (!ignore_sent_flag
&& vstream
->sent_flag
)) {
210 send_stream
.id
= htobe64(vstream
->stream_handle
);
211 send_stream
.ctf_trace_id
= htobe64(vstream
->ctf_trace
->id
);
212 send_stream
.metadata_flag
= htobe32(vstream
->metadata_flag
);
213 strncpy(send_stream
.path_name
, vstream
->path_name
,
214 sizeof(send_stream
.path_name
));
215 strncpy(send_stream
.channel_name
, vstream
->channel_name
,
216 sizeof(send_stream
.channel_name
));
218 DBG("Sending stream %" PRIu64
" to viewer", vstream
->stream_handle
);
219 ret
= send_response(sock
, &send_stream
, sizeof(send_stream
));
223 vstream
->sent_flag
= 1;
234 * Create every viewer stream possible for the given session with the seek
235 * type. Three counters *can* be return which are in order the total amount of
236 * viewer stream of the session, the number of unsent stream and the number of
237 * stream created. Those counters can be NULL and thus will be ignored.
239 * Return 0 on success or else a negative value.
242 int make_viewer_streams(struct relay_session
*session
,
243 enum lttng_viewer_seek seek_t
, uint32_t *nb_total
, uint32_t *nb_unsent
,
244 uint32_t *nb_created
)
247 struct relay_stream
*stream
;
248 struct lttng_ht_iter iter
;
253 * This is to make sure we create viewer streams for a full received
254 * channel. For instance, if we have 8 streams for a channel that are
255 * concurrently being flagged ready, we can end up creating just a subset
256 * of the 8 streams (the ones that are flagged). This lock avoids this
259 pthread_mutex_lock(&session
->viewer_ready_lock
);
262 * Create viewer streams for relay streams that are ready to be used for a
263 * the given session id only.
265 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
267 struct relay_viewer_stream
*vstream
;
269 health_code_update();
271 if (stream
->session
->id
!= session
->id
||
272 !stream
->ctf_trace
|| !stream
->viewer_ready
) {
274 * Ignore stream from a different session. Don't create streams
275 * with no ctf_trace or not ready for the viewer.
280 vstream
= viewer_stream_find_by_id(stream
->stream_handle
);
282 vstream
= viewer_stream_create(stream
, seek_t
);
288 /* Update number of created stream counter. */
291 } else if (!vstream
->sent_flag
&& nb_unsent
) {
292 /* Update number of unsent stream counter. */
295 /* Update number of total stream counter. */
304 pthread_mutex_unlock(&session
->viewer_ready_lock
);
309 * Write to writable pipe used to notify a thread.
312 int notify_thread_pipe(int wpipe
)
316 ret
= lttng_write(wpipe
, "!", 1);
318 PERROR("write poll pipe");
325 * Stop all threads by closing the thread quit pipe.
328 void stop_threads(void)
332 /* Stopping all threads */
333 DBG("Terminating all live threads");
334 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
336 ERR("write error on thread quit pipe");
339 /* Dispatch thread */
340 CMM_STORE_SHARED(live_dispatch_thread_exit
, 1);
341 futex_nto1_wake(&viewer_cmd_queue
.futex
);
345 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
348 int create_thread_poll_set(struct lttng_poll_event
*events
, int size
)
352 if (events
== NULL
|| size
== 0) {
357 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
363 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
375 * Check if the thread quit pipe was triggered.
377 * Return 1 if it was triggered else 0;
380 int check_thread_quit_pipe(int fd
, uint32_t events
)
382 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
390 * Create and init socket from uri.
393 struct lttcomm_sock
*init_socket(struct lttng_uri
*uri
)
396 struct lttcomm_sock
*sock
= NULL
;
398 sock
= lttcomm_alloc_sock_from_uri(uri
);
400 ERR("Allocating socket");
404 ret
= lttcomm_create_sock(sock
);
408 DBG("Listening on sock %d for live", sock
->fd
);
410 ret
= sock
->ops
->bind(sock
);
415 ret
= sock
->ops
->listen(sock
, -1);
425 lttcomm_destroy_sock(sock
);
431 * This thread manages the listening for new connections on the network
434 void *thread_listener(void *data
)
436 int i
, ret
, pollfd
, err
= -1;
438 uint32_t revents
, nb_fd
;
439 struct lttng_poll_event events
;
440 struct lttcomm_sock
*live_control_sock
;
442 DBG("[thread] Relay live listener started");
444 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_LISTENER
);
446 health_code_update();
448 live_control_sock
= init_socket(live_uri
);
449 if (!live_control_sock
) {
450 goto error_sock_control
;
453 /* Pass 2 as size here for the thread quit pipe and control sockets. */
454 ret
= create_thread_poll_set(&events
, 2);
456 goto error_create_poll
;
459 /* Add the control socket */
460 ret
= lttng_poll_add(&events
, live_control_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
465 lttng_relay_notify_ready();
467 if (testpoint(relayd_thread_live_listener
)) {
468 goto error_testpoint
;
472 health_code_update();
474 DBG("Listener accepting live viewers connections");
478 ret
= lttng_poll_wait(&events
, -1);
482 * Restart interrupted system call.
484 if (errno
== EINTR
) {
491 DBG("Relay new viewer connection received");
492 for (i
= 0; i
< nb_fd
; i
++) {
493 health_code_update();
495 /* Fetch once the poll data */
496 revents
= LTTNG_POLL_GETEV(&events
, i
);
497 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
499 /* Thread quit pipe has been closed. Killing thread. */
500 ret
= check_thread_quit_pipe(pollfd
, revents
);
506 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
507 ERR("socket poll error");
509 } else if (revents
& LPOLLIN
) {
511 * Get allocated in this thread, enqueued to a global queue,
512 * dequeued and freed in the worker thread.
514 struct relay_command
*relay_cmd
;
515 struct lttcomm_sock
*newsock
;
517 relay_cmd
= zmalloc(sizeof(*relay_cmd
));
519 PERROR("relay command zmalloc");
523 assert(pollfd
== live_control_sock
->fd
);
524 newsock
= live_control_sock
->ops
->accept(live_control_sock
);
526 PERROR("accepting control sock");
530 DBG("Relay viewer connection accepted socket %d", newsock
->fd
);
531 ret
= setsockopt(newsock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
,
534 PERROR("setsockopt inet");
535 lttcomm_destroy_sock(newsock
);
539 relay_cmd
->sock
= newsock
;
542 * Lock free enqueue the request.
544 cds_wfq_enqueue(&viewer_cmd_queue
.queue
, &relay_cmd
->node
);
547 * Wake the dispatch queue futex. Implicit memory
548 * barrier with the exchange in cds_wfq_enqueue.
550 futex_nto1_wake(&viewer_cmd_queue
.futex
);
559 lttng_poll_clean(&events
);
561 if (live_control_sock
->fd
>= 0) {
562 ret
= live_control_sock
->ops
->close(live_control_sock
);
567 lttcomm_destroy_sock(live_control_sock
);
571 DBG("Live viewer listener thread exited with error");
573 health_unregister(health_relayd
);
574 DBG("Live viewer listener thread cleanup complete");
580 * This thread manages the dispatching of the requests to worker threads
583 void *thread_dispatcher(void *data
)
587 struct cds_wfq_node
*node
;
588 struct relay_command
*relay_cmd
= NULL
;
590 DBG("[thread] Live viewer relay dispatcher started");
592 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_DISPATCHER
);
594 if (testpoint(relayd_thread_live_dispatcher
)) {
595 goto error_testpoint
;
598 health_code_update();
600 while (!CMM_LOAD_SHARED(live_dispatch_thread_exit
)) {
601 health_code_update();
603 /* Atomically prepare the queue futex */
604 futex_nto1_prepare(&viewer_cmd_queue
.futex
);
607 health_code_update();
609 /* Dequeue commands */
610 node
= cds_wfq_dequeue_blocking(&viewer_cmd_queue
.queue
);
612 DBG("Woken up but nothing in the live-viewer "
613 "relay command queue");
614 /* Continue thread execution */
618 relay_cmd
= caa_container_of(node
, struct relay_command
, node
);
619 DBG("Dispatching viewer request waiting on sock %d",
620 relay_cmd
->sock
->fd
);
623 * Inform worker thread of the new request. This call is blocking
624 * so we can be assured that the data will be read at some point in
625 * time or wait to the end of the world :)
627 ret
= lttng_write(live_relay_cmd_pipe
[1], relay_cmd
,
630 if (ret
< sizeof(struct relay_command
)) {
631 PERROR("write cmd pipe");
634 } while (node
!= NULL
);
636 /* Futex wait on queue. Blocking call on futex() */
638 futex_nto1_wait(&viewer_cmd_queue
.futex
);
642 /* Normal exit, no error */
649 ERR("Health error occurred in %s", __func__
);
651 health_unregister(health_relayd
);
652 DBG("Live viewer dispatch thread dying");
658 * Establish connection with the viewer and check the versions.
660 * Return 0 on success or else negative value.
663 int viewer_connect(struct relay_command
*cmd
)
666 struct lttng_viewer_connect reply
, msg
;
670 cmd
->version_check_done
= 1;
672 health_code_update();
674 DBG("Viewer is establishing a connection to the relayd.");
676 ret
= recv_request(cmd
->sock
, &msg
, sizeof(msg
));
681 health_code_update();
683 reply
.major
= RELAYD_VERSION_COMM_MAJOR
;
684 reply
.minor
= RELAYD_VERSION_COMM_MINOR
;
686 /* Major versions must be the same */
687 if (reply
.major
!= be32toh(msg
.major
)) {
688 DBG("Incompatible major versions ([relayd] %u vs [client] %u)",
689 reply
.major
, be32toh(msg
.major
));
694 cmd
->major
= reply
.major
;
695 /* We adapt to the lowest compatible version */
696 if (reply
.minor
<= be32toh(msg
.minor
)) {
697 cmd
->minor
= reply
.minor
;
699 cmd
->minor
= be32toh(msg
.minor
);
702 if (be32toh(msg
.type
) == VIEWER_CLIENT_COMMAND
) {
703 cmd
->type
= RELAY_VIEWER_COMMAND
;
704 } else if (be32toh(msg
.type
) == VIEWER_CLIENT_NOTIFICATION
) {
705 cmd
->type
= RELAY_VIEWER_NOTIFICATION
;
707 ERR("Unknown connection type : %u", be32toh(msg
.type
));
712 reply
.major
= htobe32(reply
.major
);
713 reply
.minor
= htobe32(reply
.minor
);
714 if (cmd
->type
== RELAY_VIEWER_COMMAND
) {
715 reply
.viewer_session_id
= htobe64(++last_relay_viewer_session_id
);
718 health_code_update();
721 ret
= send_response(cmd
->sock
, &reply
, sizeof(reply
));
726 health_code_update();
728 DBG("Version check done using protocol %u.%u", cmd
->major
, cmd
->minor
);
736 * Send the viewer the list of current sessions.
738 * Return 0 on success or else a negative value.
741 int viewer_list_sessions(struct relay_command
*cmd
,
742 struct lttng_ht
*sessions_ht
)
745 struct lttng_viewer_list_sessions session_list
;
747 long approx_before
, approx_after
;
748 struct lttng_ht_iter iter
;
749 struct lttng_viewer_session send_session
;
750 struct relay_session
*session
;
752 DBG("List sessions received");
755 cds_lfht_count_nodes(sessions_ht
->ht
, &approx_before
, &count
, &approx_after
);
756 session_list
.sessions_count
= htobe32(count
);
758 health_code_update();
760 ret
= send_response(cmd
->sock
, &session_list
, sizeof(session_list
));
765 health_code_update();
767 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
769 health_code_update();
771 strncpy(send_session
.session_name
, session
->session_name
,
772 sizeof(send_session
.session_name
));
773 strncpy(send_session
.hostname
, session
->hostname
,
774 sizeof(send_session
.hostname
));
775 send_session
.id
= htobe64(session
->id
);
776 send_session
.live_timer
= htobe32(session
->live_timer
);
777 send_session
.clients
= htobe32(session
->viewer_attached
);
778 send_session
.streams
= htobe32(session
->stream_count
);
780 health_code_update();
782 ret
= send_response(cmd
->sock
, &send_session
, sizeof(send_session
));
787 health_code_update();
801 * Send the viewer the list of current sessions.
804 int viewer_get_new_streams(struct relay_command
*cmd
,
805 struct lttng_ht
*sessions_ht
)
807 int ret
, send_streams
= 0;
808 uint32_t nb_created
= 0, nb_unsent
= 0, nb_streams
= 0;
809 struct lttng_viewer_new_streams_request request
;
810 struct lttng_viewer_new_streams_response response
;
811 struct relay_session
*session
;
816 DBG("Get new streams received");
818 health_code_update();
820 /* Receive the request from the connected client. */
821 ret
= recv_request(cmd
->sock
, &request
, sizeof(request
));
826 health_code_update();
829 session
= session_find_by_id(sessions_ht
, be64toh(request
.session_id
));
831 DBG("Relay session %" PRIu64
" not found",
832 be64toh(request
.session_id
));
833 response
.status
= htobe32(VIEWER_NEW_STREAMS_ERR
);
837 if (cmd
->session_id
== session
->id
) {
838 /* We confirmed the viewer is asking for the same session. */
840 response
.status
= htobe32(VIEWER_NEW_STREAMS_OK
);
843 response
.status
= htobe32(VIEWER_NEW_STREAMS_ERR
);
851 ret
= make_viewer_streams(session
, VIEWER_SEEK_LAST
, NULL
, &nb_unsent
,
856 /* Only send back the newly created streams with the unsent ones. */
857 nb_streams
= nb_created
+ nb_unsent
;
858 response
.streams_count
= htobe32(nb_streams
);
861 health_code_update();
862 ret
= send_response(cmd
->sock
, &response
, sizeof(response
));
866 health_code_update();
869 * Unknown or empty session, just return gracefully, the viewer knows what
872 if (!send_streams
|| !nb_streams
) {
878 * Send stream and *DON'T* ignore the sent flag so every viewer streams
879 * that were not sent from that point will be sent to the viewer.
881 ret
= send_viewer_streams(cmd
->sock
, session
, 0);
893 * Send the viewer the list of current sessions.
896 int viewer_attach_session(struct relay_command
*cmd
,
897 struct lttng_ht
*sessions_ht
)
899 int send_streams
= 0;
901 uint32_t nb_streams
= 0;
902 enum lttng_viewer_seek seek_type
;
903 struct lttng_viewer_attach_session_request request
;
904 struct lttng_viewer_attach_session_response response
;
905 struct relay_session
*session
;
910 DBG("Attach session received");
912 health_code_update();
914 /* Receive the request from the connected client. */
915 ret
= recv_request(cmd
->sock
, &request
, sizeof(request
));
920 health_code_update();
923 session
= session_find_by_id(sessions_ht
, be64toh(request
.session_id
));
925 DBG("Relay session %" PRIu64
" not found",
926 be64toh(request
.session_id
));
927 response
.status
= htobe32(VIEWER_ATTACH_UNK
);
931 if (cmd
->session_id
== session
->id
) {
932 /* Same viewer already attached, just send the stream list. */
934 response
.status
= htobe32(VIEWER_ATTACH_OK
);
935 } else if (session
->viewer_attached
!= 0) {
936 DBG("Already a viewer attached");
937 response
.status
= htobe32(VIEWER_ATTACH_ALREADY
);
939 } else if (session
->live_timer
== 0) {
940 DBG("Not live session");
941 response
.status
= htobe32(VIEWER_ATTACH_NOT_LIVE
);
944 session
->viewer_attached
++;
946 response
.status
= htobe32(VIEWER_ATTACH_OK
);
947 cmd
->session_id
= session
->id
;
948 cmd
->session
= session
;
951 switch (be32toh(request
.seek
)) {
952 case VIEWER_SEEK_BEGINNING
:
953 case VIEWER_SEEK_LAST
:
954 seek_type
= be32toh(request
.seek
);
957 ERR("Wrong seek parameter");
958 response
.status
= htobe32(VIEWER_ATTACH_SEEK_ERR
);
967 ret
= make_viewer_streams(session
, seek_type
, &nb_streams
, NULL
, NULL
);
971 response
.streams_count
= htobe32(nb_streams
);
974 health_code_update();
975 ret
= send_response(cmd
->sock
, &response
, sizeof(response
));
979 health_code_update();
982 * Unknown or empty session, just return gracefully, the viewer knows what
985 if (!send_streams
|| !nb_streams
) {
990 /* Send stream and ignore the sent flag. */
991 ret
= send_viewer_streams(cmd
->sock
, session
, 1);
1003 * Send the next index for a stream.
1005 * Return 0 on success or else a negative value.
1008 int viewer_get_next_index(struct relay_command
*cmd
,
1009 struct lttng_ht
*sessions_ht
)
1012 struct lttng_viewer_get_next_index request_index
;
1013 struct lttng_viewer_index viewer_index
;
1014 struct ctf_packet_index packet_index
;
1015 struct relay_viewer_stream
*vstream
;
1016 struct relay_stream
*rstream
;
1019 assert(sessions_ht
);
1021 DBG("Viewer get next index");
1023 health_code_update();
1025 ret
= recv_request(cmd
->sock
, &request_index
, sizeof(request_index
));
1029 health_code_update();
1032 vstream
= viewer_stream_find_by_id(be64toh(request_index
.stream_id
));
1038 memset(&viewer_index
, 0, sizeof(viewer_index
));
1041 * The viewer should not ask for index on metadata stream.
1043 if (vstream
->metadata_flag
) {
1044 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1048 /* First time, we open the index file */
1049 if (vstream
->index_read_fd
< 0) {
1050 ret
= index_open(vstream
->path_name
, vstream
->channel_name
,
1051 vstream
->tracefile_count
, vstream
->tracefile_count_current
);
1052 if (ret
== -ENOENT
) {
1054 * The index is created only when the first data packet arrives, it
1055 * might not be ready at the beginning of the session
1057 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
1059 } else if (ret
< 0) {
1060 viewer_index
.status
= htobe32(VIEWER_INDEX_ERR
);
1063 vstream
->index_read_fd
= ret
;
1066 rstream
= relay_stream_find_by_id(vstream
->stream_handle
);
1068 if (vstream
->abort_flag
) {
1069 /* Rotate on abort (overwrite). */
1070 DBG("Viewer rotate because of overwrite");
1071 ret
= viewer_stream_rotate(vstream
, rstream
);
1074 } else if (ret
== 1) {
1075 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1076 viewer_stream_delete(vstream
);
1077 viewer_stream_destroy(vstream
);
1081 pthread_mutex_lock(&rstream
->viewer_stream_rotation_lock
);
1082 if (rstream
->tracefile_count_current
== vstream
->tracefile_count_current
) {
1083 if (rstream
->beacon_ts_end
!= -1ULL &&
1084 vstream
->last_sent_index
== rstream
->total_index_received
) {
1085 viewer_index
.status
= htobe32(VIEWER_INDEX_INACTIVE
);
1086 viewer_index
.timestamp_end
= htobe64(rstream
->beacon_ts_end
);
1087 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1090 * Reader and writer are working in the same tracefile, so we care
1091 * about the number of index received and sent. Otherwise, we read
1094 } else if (rstream
->total_index_received
<= vstream
->last_sent_index
1095 && !vstream
->close_write_flag
) {
1096 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1097 /* No new index to send, retry later. */
1098 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
1102 pthread_mutex_unlock(&rstream
->viewer_stream_rotation_lock
);
1103 } else if (!rstream
&& vstream
->close_write_flag
&&
1104 vstream
->total_index_received
== vstream
->last_sent_index
) {
1105 /* Last index sent and current tracefile closed in write */
1106 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1107 viewer_stream_delete(vstream
);
1108 viewer_stream_destroy(vstream
);
1111 vstream
->close_write_flag
= 1;
1114 if (!vstream
->ctf_trace
->metadata_received
||
1115 vstream
->ctf_trace
->metadata_received
>
1116 vstream
->ctf_trace
->metadata_sent
) {
1117 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1120 ret
= check_new_streams(vstream
->session_id
, sessions_ht
);
1123 } else if (ret
== 1) {
1124 viewer_index
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1127 pthread_mutex_lock(&vstream
->overwrite_lock
);
1128 if (vstream
->abort_flag
) {
1130 * The file is being overwritten by the writer, we cannot
1133 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
1134 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1135 ret
= viewer_stream_rotate(vstream
, rstream
);
1138 } else if (ret
== 1) {
1139 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1140 viewer_stream_delete(vstream
);
1141 viewer_stream_destroy(vstream
);
1147 ret
= lttng_read(vstream
->index_read_fd
, &packet_index
,
1148 sizeof(packet_index
));
1149 pthread_mutex_unlock(&vstream
->overwrite_lock
);
1150 if (ret
< sizeof(packet_index
)) {
1152 * The tracefile is closed in write, so we read up to EOF.
1154 if (vstream
->close_write_flag
== 1) {
1155 viewer_index
.status
= htobe32(VIEWER_INDEX_RETRY
);
1156 /* Rotate on normal EOF */
1157 ret
= viewer_stream_rotate(vstream
, rstream
);
1160 } else if (ret
== 1) {
1161 viewer_index
.status
= htobe32(VIEWER_INDEX_HUP
);
1162 viewer_stream_delete(vstream
);
1163 viewer_stream_destroy(vstream
);
1167 PERROR("Relay reading index file %d", vstream
->index_read_fd
);
1168 viewer_index
.status
= htobe32(VIEWER_INDEX_ERR
);
1172 viewer_index
.status
= htobe32(VIEWER_INDEX_OK
);
1173 vstream
->last_sent_index
++;
1177 * Indexes are stored in big endian, no need to switch before sending.
1179 viewer_index
.offset
= packet_index
.offset
;
1180 viewer_index
.packet_size
= packet_index
.packet_size
;
1181 viewer_index
.content_size
= packet_index
.content_size
;
1182 viewer_index
.timestamp_begin
= packet_index
.timestamp_begin
;
1183 viewer_index
.timestamp_end
= packet_index
.timestamp_end
;
1184 viewer_index
.events_discarded
= packet_index
.events_discarded
;
1185 viewer_index
.stream_id
= packet_index
.stream_id
;
1188 viewer_index
.flags
= htobe32(viewer_index
.flags
);
1189 health_code_update();
1191 ret
= send_response(cmd
->sock
, &viewer_index
, sizeof(viewer_index
));
1195 health_code_update();
1197 DBG("Index %" PRIu64
" for stream %" PRIu64
" sent",
1198 vstream
->last_sent_index
, vstream
->stream_handle
);
1208 * Send the next index for a stream
1210 * Return 0 on success or else a negative value.
1213 int viewer_get_packet(struct relay_command
*cmd
,
1214 struct lttng_ht
*sessions_ht
)
1216 int ret
, send_data
= 0;
1220 struct lttng_viewer_get_packet get_packet_info
;
1221 struct lttng_viewer_trace_packet reply
;
1222 struct relay_viewer_stream
*stream
;
1226 DBG2("Relay get data packet");
1228 health_code_update();
1230 ret
= recv_request(cmd
->sock
, &get_packet_info
, sizeof(get_packet_info
));
1234 health_code_update();
1236 /* From this point on, the error label can be reached. */
1237 memset(&reply
, 0, sizeof(reply
));
1240 stream
= viewer_stream_find_by_id(be64toh(get_packet_info
.stream_id
));
1244 assert(stream
->ctf_trace
);
1247 * First time we read this stream, we need open the tracefile, we should
1248 * only arrive here if an index has already been sent to the viewer, so the
1249 * tracefile must exist, if it does not it is a fatal error.
1251 if (stream
->read_fd
< 0) {
1252 char fullpath
[PATH_MAX
];
1254 if (stream
->tracefile_count
> 0) {
1255 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s_%" PRIu64
, stream
->path_name
,
1256 stream
->channel_name
,
1257 stream
->tracefile_count_current
);
1259 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1260 stream
->channel_name
);
1265 ret
= open(fullpath
, O_RDONLY
);
1267 PERROR("Relay opening trace file");
1270 stream
->read_fd
= ret
;
1273 if (!stream
->ctf_trace
->metadata_received
||
1274 stream
->ctf_trace
->metadata_received
>
1275 stream
->ctf_trace
->metadata_sent
) {
1276 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1277 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_METADATA
;
1281 ret
= check_new_streams(stream
->session_id
, sessions_ht
);
1284 } else if (ret
== 1) {
1285 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1286 reply
.flags
|= LTTNG_VIEWER_FLAG_NEW_STREAM
;
1290 len
= be32toh(get_packet_info
.len
);
1291 data
= zmalloc(len
);
1293 PERROR("relay data zmalloc");
1297 ret
= lseek(stream
->read_fd
, be64toh(get_packet_info
.offset
), SEEK_SET
);
1300 * If the read fd was closed by the streaming side, the
1301 * abort_flag will be set to 1, otherwise it is an error.
1303 if (stream
->abort_flag
== 0) {
1307 reply
.status
= htobe32(VIEWER_GET_PACKET_EOF
);
1310 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1311 if (read_len
< len
) {
1313 * If the read fd was closed by the streaming side, the
1314 * abort_flag will be set to 1, otherwise it is an error.
1316 if (stream
->abort_flag
== 0) {
1317 PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64
,
1319 be64toh(get_packet_info
.offset
));
1322 reply
.status
= htobe32(VIEWER_GET_PACKET_EOF
);
1326 reply
.status
= htobe32(VIEWER_GET_PACKET_OK
);
1327 reply
.len
= htobe32(len
);
1332 reply
.status
= htobe32(VIEWER_GET_PACKET_ERR
);
1335 reply
.flags
= htobe32(reply
.flags
);
1337 health_code_update();
1339 ret
= send_response(cmd
->sock
, &reply
, sizeof(reply
));
1343 health_code_update();
1346 health_code_update();
1347 ret
= send_response(cmd
->sock
, data
, len
);
1351 health_code_update();
1354 DBG("Sent %u bytes for stream %" PRIu64
, len
,
1355 be64toh(get_packet_info
.stream_id
));
1366 * Send the session's metadata
1368 * Return 0 on success else a negative value.
1371 int viewer_get_metadata(struct relay_command
*cmd
)
1377 struct lttng_viewer_get_metadata request
;
1378 struct lttng_viewer_metadata_packet reply
;
1379 struct relay_viewer_stream
*stream
;
1383 DBG("Relay get metadata");
1385 health_code_update();
1387 ret
= recv_request(cmd
->sock
, &request
, sizeof(request
));
1391 health_code_update();
1394 stream
= viewer_stream_find_by_id(be64toh(request
.stream_id
));
1395 if (!stream
|| !stream
->metadata_flag
) {
1396 ERR("Invalid metadata stream");
1399 assert(stream
->ctf_trace
);
1400 assert(stream
->ctf_trace
->metadata_sent
<=
1401 stream
->ctf_trace
->metadata_received
);
1403 len
= stream
->ctf_trace
->metadata_received
-
1404 stream
->ctf_trace
->metadata_sent
;
1406 reply
.status
= htobe32(VIEWER_NO_NEW_METADATA
);
1410 /* first time, we open the metadata file */
1411 if (stream
->read_fd
< 0) {
1412 char fullpath
[PATH_MAX
];
1414 ret
= snprintf(fullpath
, PATH_MAX
, "%s/%s", stream
->path_name
,
1415 stream
->channel_name
);
1419 ret
= open(fullpath
, O_RDONLY
);
1421 PERROR("Relay opening metadata file");
1424 stream
->read_fd
= ret
;
1427 reply
.len
= htobe64(len
);
1428 data
= zmalloc(len
);
1430 PERROR("viewer metadata zmalloc");
1434 read_len
= lttng_read(stream
->read_fd
, data
, len
);
1435 if (read_len
< len
) {
1436 PERROR("Relay reading metadata file");
1439 stream
->ctf_trace
->metadata_sent
+= read_len
;
1440 reply
.status
= htobe32(VIEWER_METADATA_OK
);
1444 reply
.status
= htobe32(VIEWER_METADATA_ERR
);
1447 health_code_update();
1448 ret
= send_response(cmd
->sock
, &reply
, sizeof(reply
));
1452 health_code_update();
1455 ret
= send_response(cmd
->sock
, data
, len
);
1461 DBG("Sent %" PRIu64
" bytes of metadata for stream %" PRIu64
, len
,
1462 be64toh(request
.stream_id
));
1464 DBG("Metadata sent");
1474 * live_relay_unknown_command: send -1 if received unknown command
1477 void live_relay_unknown_command(struct relay_command
*cmd
)
1479 struct lttcomm_relayd_generic_reply reply
;
1481 reply
.ret_code
= htobe32(LTTNG_ERR_UNK
);
1482 (void) send_response(cmd
->sock
, &reply
, sizeof(reply
));
1486 * Process the commands received on the control socket
1489 int process_control(struct lttng_viewer_cmd
*recv_hdr
,
1490 struct relay_command
*cmd
, struct lttng_ht
*sessions_ht
)
1497 assert(sessions_ht
);
1499 msg_value
= be32toh(recv_hdr
->cmd
);
1502 * Make sure we've done the version check before any command other then a
1503 * new client connection.
1505 if (msg_value
!= VIEWER_CONNECT
&& !cmd
->version_check_done
) {
1506 ERR("Viewer cmd value %" PRIu32
" before version check", msg_value
);
1511 switch (msg_value
) {
1512 case VIEWER_CONNECT
:
1513 ret
= viewer_connect(cmd
);
1515 case VIEWER_LIST_SESSIONS
:
1516 ret
= viewer_list_sessions(cmd
, sessions_ht
);
1518 case VIEWER_ATTACH_SESSION
:
1519 ret
= viewer_attach_session(cmd
, sessions_ht
);
1521 case VIEWER_GET_NEXT_INDEX
:
1522 ret
= viewer_get_next_index(cmd
, sessions_ht
);
1524 case VIEWER_GET_PACKET
:
1525 ret
= viewer_get_packet(cmd
, sessions_ht
);
1527 case VIEWER_GET_METADATA
:
1528 ret
= viewer_get_metadata(cmd
);
1530 case VIEWER_GET_NEW_STREAMS
:
1531 ret
= viewer_get_new_streams(cmd
, sessions_ht
);
1534 ERR("Received unknown viewer command (%u)", be32toh(recv_hdr
->cmd
));
1535 live_relay_unknown_command(cmd
);
1545 void cleanup_poll_connection(struct lttng_poll_event
*events
, int pollfd
)
1551 lttng_poll_del(events
, pollfd
);
1553 ret
= close(pollfd
);
1555 ERR("Closing pollfd %d", pollfd
);
1560 * Create and add connection to the given hash table.
1562 * Return poll add value or else -1 on error.
1565 int add_connection(int fd
, struct lttng_poll_event
*events
,
1566 struct lttng_ht
*relay_connections_ht
)
1569 struct relay_command
*relay_connection
;
1572 assert(relay_connections_ht
);
1574 relay_connection
= zmalloc(sizeof(struct relay_command
));
1575 if (relay_connection
== NULL
) {
1576 PERROR("Relay command zmalloc");
1580 ret
= lttng_read(fd
, relay_connection
, sizeof(*relay_connection
));
1581 if (ret
< sizeof(*relay_connection
)) {
1582 PERROR("read relay cmd pipe");
1586 lttng_ht_node_init_ulong(&relay_connection
->sock_n
,
1587 (unsigned long) relay_connection
->sock
->fd
);
1589 lttng_ht_add_unique_ulong(relay_connections_ht
,
1590 &relay_connection
->sock_n
);
1593 return lttng_poll_add(events
, relay_connection
->sock
->fd
,
1594 LPOLLIN
| LPOLLRDHUP
);
1597 free(relay_connection
);
1603 void deferred_free_connection(struct rcu_head
*head
)
1605 struct relay_command
*relay_connection
=
1606 caa_container_of(head
, struct relay_command
, rcu_node
);
1608 if (relay_connection
->session
&&
1609 relay_connection
->session
->viewer_attached
> 0) {
1610 relay_connection
->session
->viewer_attached
--;
1612 lttcomm_destroy_sock(relay_connection
->sock
);
1613 free(relay_connection
);
1617 * Delete all streams for a specific session ID.
1620 void viewer_del_streams(uint64_t session_id
)
1622 struct relay_viewer_stream
*stream
;
1623 struct lttng_ht_iter iter
;
1626 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
, stream
,
1628 health_code_update();
1630 if (stream
->session_id
!= session_id
) {
1634 viewer_stream_delete(stream
);
1635 assert(stream
->ctf_trace
);
1637 if (stream
->metadata_flag
) {
1639 * The metadata viewer stream is destroyed once the refcount on the
1640 * ctf trace goes to 0 in the destroy stream function thus there is
1641 * no explicit call to that function here.
1643 stream
->ctf_trace
->metadata_sent
= 0;
1644 stream
->ctf_trace
->viewer_metadata_stream
= NULL
;
1646 viewer_stream_destroy(stream
);
1653 * Delete and free a connection.
1655 * RCU read side lock MUST be acquired.
1658 void del_connection(struct lttng_ht
*relay_connections_ht
,
1659 struct lttng_ht_iter
*iter
, struct relay_command
*relay_connection
)
1663 assert(relay_connections_ht
);
1665 assert(relay_connection
);
1667 DBG("Cleaning connection of session ID %" PRIu64
,
1668 relay_connection
->session_id
);
1670 ret
= lttng_ht_del(relay_connections_ht
, iter
);
1673 viewer_del_streams(relay_connection
->session_id
);
1675 call_rcu(&relay_connection
->rcu_node
, deferred_free_connection
);
1679 * This thread does the actual work
1682 void *thread_worker(void *data
)
1686 struct relay_command
*relay_connection
;
1687 struct lttng_poll_event events
;
1688 struct lttng_ht
*relay_connections_ht
;
1689 struct lttng_ht_node_ulong
*node
;
1690 struct lttng_ht_iter iter
;
1691 struct lttng_viewer_cmd recv_hdr
;
1692 struct relay_local_data
*relay_ctx
= (struct relay_local_data
*) data
;
1693 struct lttng_ht
*sessions_ht
= relay_ctx
->sessions_ht
;
1695 DBG("[thread] Live viewer relay worker started");
1697 rcu_register_thread();
1699 health_register(health_relayd
, HEALTH_RELAYD_TYPE_LIVE_WORKER
);
1701 if (testpoint(relayd_thread_live_worker
)) {
1702 goto error_testpoint
;
1705 /* table of connections indexed on socket */
1706 relay_connections_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1707 if (!relay_connections_ht
) {
1708 goto relay_connections_ht_error
;
1711 ret
= create_thread_poll_set(&events
, 2);
1713 goto error_poll_create
;
1716 ret
= lttng_poll_add(&events
, live_relay_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1725 health_code_update();
1727 /* Infinite blocking call, waiting for transmission */
1728 DBG3("Relayd live viewer worker thread polling...");
1729 health_poll_entry();
1730 ret
= lttng_poll_wait(&events
, -1);
1734 * Restart interrupted system call.
1736 if (errno
== EINTR
) {
1745 * Process control. The control connection is prioritised so we don't
1746 * starve it with high throughput tracing data on the data
1749 for (i
= 0; i
< nb_fd
; i
++) {
1750 /* Fetch once the poll data */
1751 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1752 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1754 health_code_update();
1756 /* Thread quit pipe has been closed. Killing thread. */
1757 ret
= check_thread_quit_pipe(pollfd
, revents
);
1763 /* Inspect the relay cmd pipe for new connection */
1764 if (pollfd
== live_relay_cmd_pipe
[0]) {
1765 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1766 ERR("Relay live pipe error");
1768 } else if (revents
& LPOLLIN
) {
1769 DBG("Relay live viewer command received");
1770 ret
= add_connection(live_relay_cmd_pipe
[0],
1771 &events
, relay_connections_ht
);
1776 } else if (revents
) {
1778 lttng_ht_lookup(relay_connections_ht
,
1779 (void *)((unsigned long) pollfd
), &iter
);
1780 node
= lttng_ht_iter_get_node_ulong(&iter
);
1782 DBG2("Relay viewer sock %d not found", pollfd
);
1786 relay_connection
= caa_container_of(node
, struct relay_command
,
1789 if (revents
& (LPOLLERR
)) {
1790 cleanup_poll_connection(&events
, pollfd
);
1791 del_connection(relay_connections_ht
, &iter
,
1793 } else if (revents
& (LPOLLHUP
| LPOLLRDHUP
)) {
1794 DBG("Viewer socket %d hung up", pollfd
);
1795 cleanup_poll_connection(&events
, pollfd
);
1796 del_connection(relay_connections_ht
, &iter
,
1798 } else if (revents
& LPOLLIN
) {
1799 ret
= relay_connection
->sock
->ops
->recvmsg(
1800 relay_connection
->sock
, &recv_hdr
,
1801 sizeof(struct lttng_viewer_cmd
),
1803 /* connection closed */
1805 cleanup_poll_connection(&events
, pollfd
);
1806 del_connection(relay_connections_ht
, &iter
,
1808 DBG("Viewer control connection closed with %d",
1811 if (relay_connection
->session
) {
1812 DBG2("Relay viewer worker receiving data for "
1813 "session: %" PRIu64
,
1814 relay_connection
->session
->id
);
1816 ret
= process_control(&recv_hdr
, relay_connection
,
1819 /* Clear the session on error. */
1820 cleanup_poll_connection(&events
, pollfd
);
1821 del_connection(relay_connections_ht
, &iter
,
1823 DBG("Viewer connection closed with %d", pollfd
);
1834 lttng_poll_clean(&events
);
1836 /* empty the hash table and free the memory */
1838 cds_lfht_for_each_entry(relay_connections_ht
->ht
, &iter
.iter
, node
, node
) {
1839 health_code_update();
1841 node
= lttng_ht_iter_get_node_ulong(&iter
);
1846 relay_connection
= caa_container_of(node
, struct relay_command
,
1848 del_connection(relay_connections_ht
, &iter
, relay_connection
);
1852 lttng_ht_destroy(relay_connections_ht
);
1853 relay_connections_ht_error
:
1854 /* Close relay cmd pipes */
1855 utils_close_pipe(live_relay_cmd_pipe
);
1857 DBG("Viewer worker thread exited with error");
1859 DBG("Viewer worker thread cleanup complete");
1863 ERR("Health error occurred in %s", __func__
);
1865 health_unregister(health_relayd
);
1867 rcu_unregister_thread();
1872 * Create the relay command pipe to wake thread_manage_apps.
1873 * Closed in cleanup().
1875 static int create_relay_cmd_pipe(void)
1879 ret
= utils_create_pipe_cloexec(live_relay_cmd_pipe
);
1884 void live_stop_threads(void)
1891 ret
= pthread_join(live_listener_thread
, &status
);
1893 PERROR("pthread_join live listener");
1894 goto error
; /* join error, exit without cleanup */
1897 ret
= pthread_join(live_worker_thread
, &status
);
1899 PERROR("pthread_join live worker");
1900 goto error
; /* join error, exit without cleanup */
1903 ret
= pthread_join(live_dispatcher_thread
, &status
);
1905 PERROR("pthread_join live dispatcher");
1906 goto error
; /* join error, exit without cleanup */
1918 int live_start_threads(struct lttng_uri
*uri
,
1919 struct relay_local_data
*relay_ctx
)
1928 /* Check if daemon is UID = 0 */
1929 is_root
= !getuid();
1932 if (live_uri
->port
< 1024) {
1933 ERR("Need to be root to use ports < 1024");
1939 /* Setup the thread apps communication pipe. */
1940 if ((ret
= create_relay_cmd_pipe()) < 0) {
1944 /* Init relay command queue. */
1945 cds_wfq_init(&viewer_cmd_queue
.queue
);
1947 /* Set up max poll set size */
1948 lttng_poll_set_max_size();
1950 /* Setup the dispatcher thread */
1951 ret
= pthread_create(&live_dispatcher_thread
, NULL
,
1952 thread_dispatcher
, (void *) NULL
);
1954 PERROR("pthread_create viewer dispatcher");
1955 goto exit_dispatcher
;
1958 /* Setup the worker thread */
1959 ret
= pthread_create(&live_worker_thread
, NULL
,
1960 thread_worker
, relay_ctx
);
1962 PERROR("pthread_create viewer worker");
1966 /* Setup the listener thread */
1967 ret
= pthread_create(&live_listener_thread
, NULL
,
1968 thread_listener
, (void *) NULL
);
1970 PERROR("pthread_create viewer listener");
1978 ret
= pthread_join(live_listener_thread
, &status
);
1980 PERROR("pthread_join live listener");
1981 goto error
; /* join error, exit without cleanup */
1985 ret
= pthread_join(live_worker_thread
, &status
);
1987 PERROR("pthread_join live worker");
1988 goto error
; /* join error, exit without cleanup */
1992 ret
= pthread_join(live_dispatcher_thread
, &status
);
1994 PERROR("pthread_join live dispatcher");
1995 goto error
; /* join error, exit without cleanup */