2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <common/common.h>
27 #include <common/defaults.h>
28 #include <common/compat/endian.h>
29 #include <common/compat/string.h>
30 #include <common/sessiond-comm/relayd.h>
31 #include <common/index/ctf-index.h>
32 #include <common/trace-chunk.h>
33 #include <common/string-utils/format.h>
38 * Send command. Fill up the header and append the data.
40 static int send_command(struct lttcomm_relayd_sock
*rsock
,
41 enum lttcomm_relayd_command cmd
, const void *data
, size_t size
,
45 struct lttcomm_relayd_hdr header
;
47 uint64_t buf_size
= sizeof(header
);
49 if (rsock
->sock
.fd
< 0) {
57 buf
= zmalloc(buf_size
);
59 PERROR("zmalloc relayd send command buf");
64 memset(&header
, 0, sizeof(header
));
65 header
.cmd
= htobe32(cmd
);
66 header
.data_size
= htobe64(size
);
68 /* Zeroed for now since not used. */
69 header
.cmd_version
= 0;
70 header
.circuit_id
= 0;
72 /* Prepare buffer to send. */
73 memcpy(buf
, &header
, sizeof(header
));
75 memcpy(buf
+ sizeof(header
), data
, size
);
78 DBG3("Relayd sending command %d of size %" PRIu64
, (int) cmd
, buf_size
);
79 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
81 PERROR("Failed to send command %d of size %" PRIu64
,
93 * Receive reply data on socket. This MUST be call after send_command or else
94 * could result in unexpected behavior(s).
96 static int recv_reply(struct lttcomm_relayd_sock
*rsock
, void *data
, size_t size
)
100 if (rsock
->sock
.fd
< 0) {
104 DBG3("Relayd waiting for reply of size %zu", size
);
106 ret
= rsock
->sock
.ops
->recvmsg(&rsock
->sock
, data
, size
, 0);
107 if (ret
<= 0 || ret
!= size
) {
109 /* Orderly shutdown. */
110 DBG("Socket %d has performed an orderly shutdown", rsock
->sock
.fd
);
112 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
113 rsock
->sock
.fd
, size
, ret
);
115 /* Always return -1 here and the caller can use errno. */
125 * Starting from 2.11, RELAYD_CREATE_SESSION payload (session_name & hostname)
126 * have no length restriction on the sender side.
127 * Length for both payloads is stored in the msg struct. A new dynamic size
128 * payload size is introduced.
130 static int relayd_create_session_2_11(struct lttcomm_relayd_sock
*rsock
,
131 const char *session_name
, const char *hostname
,
132 int session_live_timer
, unsigned int snapshot
,
133 uint64_t sessiond_session_id
, const lttng_uuid sessiond_uuid
,
134 const uint64_t *current_chunk_id
,
135 time_t creation_time
)
138 struct lttcomm_relayd_create_session_2_11
*msg
= NULL
;
139 size_t session_name_len
;
143 /* The two names are sent with a '\0' delimiter between them. */
144 session_name_len
= strlen(session_name
) + 1;
145 hostname_len
= strlen(hostname
) + 1;
147 msg_length
= sizeof(*msg
) + session_name_len
+ hostname_len
;
148 msg
= zmalloc(msg_length
);
150 PERROR("zmalloc create_session_2_11 command message");
155 assert(session_name_len
<= UINT32_MAX
);
156 msg
->session_name_len
= htobe32(session_name_len
);
158 assert(hostname_len
<= UINT32_MAX
);
159 msg
->hostname_len
= htobe32(hostname_len
);
161 if (lttng_strncpy(msg
->names
, session_name
, session_name_len
)) {
165 if (lttng_strncpy(msg
->names
+ session_name_len
, hostname
, hostname_len
)) {
170 msg
->live_timer
= htobe32(session_live_timer
);
171 msg
->snapshot
= !!snapshot
;
173 lttng_uuid_copy(msg
->sessiond_uuid
, sessiond_uuid
);
174 msg
->session_id
= htobe64(sessiond_session_id
);
176 if (current_chunk_id
) {
177 LTTNG_OPTIONAL_SET(&msg
->current_chunk_id
,
178 htobe64(*current_chunk_id
));
181 msg
->creation_time
= htobe64((uint64_t) creation_time
);
184 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, msg
, msg_length
, 0);
193 * From 2.4 to 2.10, RELAYD_CREATE_SESSION takes additional parameters to
194 * support the live reading capability.
196 static int relayd_create_session_2_4(struct lttcomm_relayd_sock
*rsock
,
197 const char *session_name
, const char *hostname
,
198 int session_live_timer
, unsigned int snapshot
)
201 struct lttcomm_relayd_create_session_2_4 msg
;
203 if (lttng_strncpy(msg
.session_name
, session_name
,
204 sizeof(msg
.session_name
))) {
208 if (lttng_strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
))) {
212 msg
.live_timer
= htobe32(session_live_timer
);
213 msg
.snapshot
= htobe32(snapshot
);
216 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, &msg
, sizeof(msg
), 0);
226 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
228 static int relayd_create_session_2_1(struct lttcomm_relayd_sock
*rsock
)
233 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
243 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
244 * set session_id of the relayd if we have a successful reply from the relayd.
246 * On success, return 0 else a negative value which is either an errno error or
247 * a lttng error code from the relayd.
249 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
,
250 uint64_t *relayd_session_id
,
251 const char *session_name
, const char *hostname
,
252 int session_live_timer
,
253 unsigned int snapshot
, uint64_t sessiond_session_id
,
254 const lttng_uuid sessiond_uuid
,
255 const uint64_t *current_chunk_id
,
256 time_t creation_time
)
259 struct lttcomm_relayd_status_session reply
;
262 assert(relayd_session_id
);
264 DBG("Relayd create session");
266 if (rsock
->minor
< 4) {
267 /* From 2.1 to 2.3 */
268 ret
= relayd_create_session_2_1(rsock
);
269 } else if (rsock
->minor
>= 4 && rsock
->minor
< 11) {
270 /* From 2.4 to 2.10 */
271 ret
= relayd_create_session_2_4(rsock
, session_name
,
272 hostname
, session_live_timer
, snapshot
);
274 /* From 2.11 to ... */
275 ret
= relayd_create_session_2_11(rsock
, session_name
,
276 hostname
, session_live_timer
, snapshot
,
277 sessiond_session_id
, sessiond_uuid
,
278 current_chunk_id
, creation_time
);
285 /* Receive response */
286 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
291 reply
.session_id
= be64toh(reply
.session_id
);
292 reply
.ret_code
= be32toh(reply
.ret_code
);
294 /* Return session id or negative ret code. */
295 if (reply
.ret_code
!= LTTNG_OK
) {
297 ERR("Relayd create session replied error %d", reply
.ret_code
);
301 *relayd_session_id
= reply
.session_id
;
304 DBG("Relayd session created with id %" PRIu64
, reply
.session_id
);
310 static int relayd_add_stream_2_1(struct lttcomm_relayd_sock
*rsock
,
311 const char *channel_name
, const char *pathname
)
314 struct lttcomm_relayd_add_stream msg
;
316 memset(&msg
, 0, sizeof(msg
));
317 if (lttng_strncpy(msg
.channel_name
, channel_name
,
318 sizeof(msg
.channel_name
))) {
323 if (lttng_strncpy(msg
.pathname
, pathname
,
324 sizeof(msg
.pathname
))) {
330 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
340 static int relayd_add_stream_2_2(struct lttcomm_relayd_sock
*rsock
,
341 const char *channel_name
, const char *pathname
,
342 uint64_t tracefile_size
, uint64_t tracefile_count
)
345 struct lttcomm_relayd_add_stream_2_2 msg
;
347 memset(&msg
, 0, sizeof(msg
));
348 /* Compat with relayd 2.2 to 2.10 */
349 if (lttng_strncpy(msg
.channel_name
, channel_name
,
350 sizeof(msg
.channel_name
))) {
354 if (lttng_strncpy(msg
.pathname
, pathname
,
355 sizeof(msg
.pathname
))) {
359 msg
.tracefile_size
= htobe64(tracefile_size
);
360 msg
.tracefile_count
= htobe64(tracefile_count
);
363 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
372 static int relayd_add_stream_2_11(struct lttcomm_relayd_sock
*rsock
,
373 const char *channel_name
, const char *pathname
,
374 uint64_t tracefile_size
, uint64_t tracefile_count
,
375 uint64_t trace_archive_id
)
378 struct lttcomm_relayd_add_stream_2_11
*msg
= NULL
;
379 size_t channel_name_len
;
383 /* The two names are sent with a '\0' delimiter between them. */
384 channel_name_len
= strlen(channel_name
) + 1;
385 pathname_len
= strlen(pathname
) + 1;
387 msg_length
= sizeof(*msg
) + channel_name_len
+ pathname_len
;
388 msg
= zmalloc(msg_length
);
390 PERROR("zmalloc add_stream_2_11 command message");
395 assert(channel_name_len
<= UINT32_MAX
);
396 msg
->channel_name_len
= htobe32(channel_name_len
);
398 assert(pathname_len
<= UINT32_MAX
);
399 msg
->pathname_len
= htobe32(pathname_len
);
401 if (lttng_strncpy(msg
->names
, channel_name
, channel_name_len
)) {
405 if (lttng_strncpy(msg
->names
+ channel_name_len
, pathname
, pathname_len
)) {
410 msg
->tracefile_size
= htobe64(tracefile_size
);
411 msg
->tracefile_count
= htobe64(tracefile_count
);
412 msg
->trace_chunk_id
= htobe64(trace_archive_id
);
415 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) msg
, msg_length
, 0);
426 * Add stream on the relayd and assign stream handle to the stream_id argument.
428 * On success return 0 else return ret_code negative value.
430 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
431 const char *pathname
, uint64_t *stream_id
,
432 uint64_t tracefile_size
, uint64_t tracefile_count
,
433 struct lttng_trace_chunk
*trace_chunk
)
436 struct lttcomm_relayd_status_stream reply
;
438 /* Code flow error. Safety net. */
440 assert(channel_name
);
443 DBG("Relayd adding stream for channel name %s", channel_name
);
445 /* Compat with relayd 2.1 */
446 if (rsock
->minor
== 1) {
448 assert(!trace_chunk
);
449 ret
= relayd_add_stream_2_1(rsock
, channel_name
, pathname
);
451 } else if (rsock
->minor
> 1 && rsock
->minor
< 11) {
452 /* From 2.2 to 2.10 */
453 assert(!trace_chunk
);
454 ret
= relayd_add_stream_2_2(rsock
, channel_name
, pathname
,
455 tracefile_size
, tracefile_count
);
457 enum lttng_trace_chunk_status chunk_status
;
461 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
,
463 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
465 /* From 2.11 to ...*/
466 ret
= relayd_add_stream_2_11(rsock
, channel_name
, pathname
,
467 tracefile_size
, tracefile_count
,
476 /* Waiting for reply */
477 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
482 /* Back to host bytes order. */
483 reply
.handle
= be64toh(reply
.handle
);
484 reply
.ret_code
= be32toh(reply
.ret_code
);
486 /* Return session id or negative ret code. */
487 if (reply
.ret_code
!= LTTNG_OK
) {
489 ERR("Relayd add stream replied error %d", reply
.ret_code
);
493 *stream_id
= reply
.handle
;
496 DBG("Relayd stream added successfully with handle %" PRIu64
,
504 * Inform the relay that all the streams for the current channel has been sent.
506 * On success return 0 else return ret_code negative value.
508 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
511 struct lttcomm_relayd_generic_reply reply
;
513 /* Code flow error. Safety net. */
516 DBG("Relayd sending streams sent.");
518 /* This feature was introduced in 2.4, ignore it for earlier versions. */
519 if (rsock
->minor
< 4) {
525 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, NULL
, 0, 0);
530 /* Waiting for reply */
531 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
536 /* Back to host bytes order. */
537 reply
.ret_code
= be32toh(reply
.ret_code
);
539 /* Return session id or negative ret code. */
540 if (reply
.ret_code
!= LTTNG_OK
) {
542 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
549 DBG("Relayd streams sent success");
557 * Check version numbers on the relayd.
558 * If major versions are compatible, we assign minor_to_use to the
559 * minor version of the procotol we are going to use for this session.
561 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
562 * otherwise, or a negative value on network errors.
564 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
567 struct lttcomm_relayd_version msg
;
569 /* Code flow error. Safety net. */
572 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
575 memset(&msg
, 0, sizeof(msg
));
576 /* Prepare network byte order before transmission. */
577 msg
.major
= htobe32(rsock
->major
);
578 msg
.minor
= htobe32(rsock
->minor
);
581 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
586 /* Receive response */
587 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
592 /* Set back to host bytes order */
593 msg
.major
= be32toh(msg
.major
);
594 msg
.minor
= be32toh(msg
.minor
);
597 * Only validate the major version. If the other side is higher,
598 * communication is not possible. Only major version equal can talk to each
599 * other. If the minor version differs, the lowest version is used by both
602 if (msg
.major
!= rsock
->major
) {
604 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
605 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
606 msg
.major
, rsock
->major
);
611 * If the relayd's minor version is higher, it will adapt to our version so
612 * we can continue to use the latest relayd communication data structure.
613 * If the received minor version is higher, the relayd should adapt to us.
615 if (rsock
->minor
> msg
.minor
) {
616 rsock
->minor
= msg
.minor
;
619 /* Version number compatible */
620 DBG2("Relayd version is compatible, using protocol version %u.%u",
621 rsock
->major
, rsock
->minor
);
629 * Add stream on the relayd and assign stream handle to the stream_id argument.
631 * On success return 0 else return ret_code negative value.
633 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
637 /* Code flow error. Safety net. */
640 DBG("Relayd sending metadata of size %zu", len
);
643 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
648 DBG2("Relayd metadata added successfully");
651 * After that call, the metadata data MUST be sent to the relayd so the
652 * receive size on the other end matches the len of the metadata packet
653 * header. This is why we don't wait for a reply here.
661 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
663 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
665 /* Code flow error. Safety net. */
668 if (!rsock
->sock
.ops
) {
670 * Attempting a connect on a non-initialized socket.
675 DBG3("Relayd connect ...");
677 return rsock
->sock
.ops
->connect(&rsock
->sock
);
681 * Close relayd socket with an allocated lttcomm_relayd_sock.
683 * If no socket operations are found, simply return 0 meaning that everything
684 * is fine. Without operations, the socket can not possibly be opened or used.
685 * This is possible if the socket was allocated but not created. However, the
686 * caller could simply use it to store a valid file descriptor for instance
687 * passed over a Unix socket and call this to cleanup but still without a valid
690 * Return the close returned value. On error, a negative value is usually
691 * returned back from close(2).
693 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
697 /* Code flow error. Safety net. */
700 /* An invalid fd is fine, return success. */
701 if (rsock
->sock
.fd
< 0) {
706 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
708 if (rsock
->sock
.ops
) {
709 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
711 /* Default call if no specific ops found. */
712 ret
= close(rsock
->sock
.fd
);
714 PERROR("relayd_close default close");
724 * Send data header structure to the relayd.
726 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
727 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
731 /* Code flow error. Safety net. */
735 if (rsock
->sock
.fd
< 0) {
739 DBG3("Relayd sending data header of size %zu", size
);
741 /* Again, safety net */
743 size
= sizeof(struct lttcomm_relayd_data_hdr
);
746 /* Only send data header. */
747 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
754 * The data MUST be sent right after that command for the receive on the
755 * other end to match the size in the header.
763 * Send close stream command to the relayd.
765 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
766 uint64_t last_net_seq_num
)
769 struct lttcomm_relayd_close_stream msg
;
770 struct lttcomm_relayd_generic_reply reply
;
772 /* Code flow error. Safety net. */
775 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
777 memset(&msg
, 0, sizeof(msg
));
778 msg
.stream_id
= htobe64(stream_id
);
779 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
782 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
787 /* Receive response */
788 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
793 reply
.ret_code
= be32toh(reply
.ret_code
);
795 /* Return session id or negative ret code. */
796 if (reply
.ret_code
!= LTTNG_OK
) {
798 ERR("Relayd close stream replied error %d", reply
.ret_code
);
804 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
811 * Check for data availability for a given stream id.
813 * Return 0 if NOT pending, 1 if so and a negative value on error.
815 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
816 uint64_t last_net_seq_num
)
819 struct lttcomm_relayd_data_pending msg
;
820 struct lttcomm_relayd_generic_reply reply
;
822 /* Code flow error. Safety net. */
825 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
827 memset(&msg
, 0, sizeof(msg
));
828 msg
.stream_id
= htobe64(stream_id
);
829 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
832 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
838 /* Receive response */
839 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
844 reply
.ret_code
= be32toh(reply
.ret_code
);
846 /* Return session id or negative ret code. */
847 if (reply
.ret_code
>= LTTNG_OK
) {
848 ERR("Relayd data pending replied error %d", reply
.ret_code
);
851 /* At this point, the ret code is either 1 or 0 */
852 ret
= reply
.ret_code
;
854 DBG("Relayd data is %s pending for stream id %" PRIu64
,
855 ret
== 1 ? "" : "NOT", stream_id
);
862 * Check on the relayd side for a quiescent state on the control socket.
864 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
865 uint64_t metadata_stream_id
)
868 struct lttcomm_relayd_quiescent_control msg
;
869 struct lttcomm_relayd_generic_reply reply
;
871 /* Code flow error. Safety net. */
874 DBG("Relayd checking quiescent control state");
876 memset(&msg
, 0, sizeof(msg
));
877 msg
.stream_id
= htobe64(metadata_stream_id
);
880 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
885 /* Receive response */
886 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
891 reply
.ret_code
= be32toh(reply
.ret_code
);
893 /* Return session id or negative ret code. */
894 if (reply
.ret_code
!= LTTNG_OK
) {
896 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
900 /* Control socket is quiescent */
908 * Begin a data pending command for a specific session id.
910 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
913 struct lttcomm_relayd_begin_data_pending msg
;
914 struct lttcomm_relayd_generic_reply reply
;
916 /* Code flow error. Safety net. */
919 DBG("Relayd begin data pending");
921 memset(&msg
, 0, sizeof(msg
));
922 msg
.session_id
= htobe64(id
);
925 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
930 /* Receive response */
931 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
936 reply
.ret_code
= be32toh(reply
.ret_code
);
938 /* Return session id or negative ret code. */
939 if (reply
.ret_code
!= LTTNG_OK
) {
941 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
952 * End a data pending command for a specific session id.
954 * Return 0 on success and set is_data_inflight to 0 if no data is being
955 * streamed or 1 if it is the case.
957 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
958 unsigned int *is_data_inflight
)
961 struct lttcomm_relayd_end_data_pending msg
;
962 struct lttcomm_relayd_generic_reply reply
;
964 /* Code flow error. Safety net. */
967 DBG("Relayd end data pending");
969 memset(&msg
, 0, sizeof(msg
));
970 msg
.session_id
= htobe64(id
);
973 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
978 /* Receive response */
979 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
984 recv_ret
= be32toh(reply
.ret_code
);
990 *is_data_inflight
= recv_ret
;
992 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
1001 * Send index to the relayd.
1003 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
1004 struct ctf_packet_index
*index
, uint64_t relay_stream_id
,
1005 uint64_t net_seq_num
)
1008 struct lttcomm_relayd_index msg
;
1009 struct lttcomm_relayd_generic_reply reply
;
1011 /* Code flow error. Safety net. */
1014 if (rsock
->minor
< 4) {
1015 DBG("Not sending indexes before protocol 2.4");
1020 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
1022 memset(&msg
, 0, sizeof(msg
));
1023 msg
.relay_stream_id
= htobe64(relay_stream_id
);
1024 msg
.net_seq_num
= htobe64(net_seq_num
);
1026 /* The index is already in big endian. */
1027 msg
.packet_size
= index
->packet_size
;
1028 msg
.content_size
= index
->content_size
;
1029 msg
.timestamp_begin
= index
->timestamp_begin
;
1030 msg
.timestamp_end
= index
->timestamp_end
;
1031 msg
.events_discarded
= index
->events_discarded
;
1032 msg
.stream_id
= index
->stream_id
;
1034 if (rsock
->minor
>= 8) {
1035 msg
.stream_instance_id
= index
->stream_instance_id
;
1036 msg
.packet_seq_num
= index
->packet_seq_num
;
1040 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
,
1041 lttcomm_relayd_index_len(lttng_to_index_major(rsock
->major
,
1043 lttng_to_index_minor(rsock
->major
, rsock
->minor
)),
1049 /* Receive response */
1050 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1055 reply
.ret_code
= be32toh(reply
.ret_code
);
1057 /* Return session id or negative ret code. */
1058 if (reply
.ret_code
!= LTTNG_OK
) {
1060 ERR("Relayd send index replied error %d", reply
.ret_code
);
1071 * Ask the relay to reset the metadata trace file (regeneration).
1073 int relayd_reset_metadata(struct lttcomm_relayd_sock
*rsock
,
1074 uint64_t stream_id
, uint64_t version
)
1077 struct lttcomm_relayd_reset_metadata msg
;
1078 struct lttcomm_relayd_generic_reply reply
;
1080 /* Code flow error. Safety net. */
1083 /* Should have been prevented by the sessiond. */
1084 if (rsock
->minor
< 8) {
1085 ERR("Metadata regeneration unsupported before 2.8");
1090 DBG("Relayd reset metadata stream id %" PRIu64
, stream_id
);
1092 memset(&msg
, 0, sizeof(msg
));
1093 msg
.stream_id
= htobe64(stream_id
);
1094 msg
.version
= htobe64(version
);
1097 ret
= send_command(rsock
, RELAYD_RESET_METADATA
, (void *) &msg
, sizeof(msg
), 0);
1102 /* Receive response */
1103 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1108 reply
.ret_code
= be32toh(reply
.ret_code
);
1110 /* Return session id or negative ret code. */
1111 if (reply
.ret_code
!= LTTNG_OK
) {
1113 ERR("Relayd reset metadata replied error %d", reply
.ret_code
);
1119 DBG("Relayd reset metadata stream id %" PRIu64
" successfully", stream_id
);
1125 int relayd_rotate_streams(struct lttcomm_relayd_sock
*sock
,
1126 unsigned int stream_count
, const uint64_t *new_chunk_id
,
1127 const struct relayd_stream_rotation_position
*positions
)
1131 struct lttng_dynamic_buffer payload
;
1132 struct lttcomm_relayd_generic_reply reply
= {};
1133 const struct lttcomm_relayd_rotate_streams msg
= {
1134 .stream_count
= htobe32((uint32_t) stream_count
),
1135 .new_chunk_id
= (typeof(msg
.new_chunk_id
)) {
1136 .is_set
= !!new_chunk_id
,
1137 .value
= htobe64(new_chunk_id
? *new_chunk_id
: 0),
1140 char new_chunk_id_buf
[MAX_INT_DEC_LEN(*new_chunk_id
)] = {};
1141 const char *new_chunk_id_str
;
1143 lttng_dynamic_buffer_init(&payload
);
1145 /* Code flow error. Safety net. */
1149 ret
= snprintf(new_chunk_id_buf
, sizeof(new_chunk_id_buf
),
1150 "%" PRIu64
, *new_chunk_id
);
1151 if (ret
== -1 || ret
>= sizeof(new_chunk_id_buf
)) {
1152 new_chunk_id_str
= "formatting error";
1154 new_chunk_id_str
= new_chunk_id_buf
;
1157 new_chunk_id_str
= "none";
1160 DBG("Preparing \"rotate streams\" command payload: new_chunk_id = %s, stream_count = %u",
1161 new_chunk_id_str
, stream_count
);
1163 ret
= lttng_dynamic_buffer_append(&payload
, &msg
, sizeof(msg
));
1165 ERR("Failed to allocate \"rotate streams\" command payload");
1169 for (i
= 0; i
< stream_count
; i
++) {
1170 const struct relayd_stream_rotation_position
*position
=
1172 const struct lttcomm_relayd_stream_rotation_position comm_position
= {
1173 .stream_id
= htobe64(position
->stream_id
),
1174 .rotate_at_seq_num
= htobe64(
1175 position
->rotate_at_seq_num
),
1178 DBG("Rotate stream %" PRIu64
"at sequence number %" PRIu64
,
1179 position
->stream_id
,
1180 position
->rotate_at_seq_num
);
1181 ret
= lttng_dynamic_buffer_append(&payload
, &comm_position
,
1182 sizeof(comm_position
));
1184 ERR("Failed to allocate \"rotate streams\" command payload");
1190 ret
= send_command(sock
, RELAYD_ROTATE_STREAMS
, payload
.data
,
1193 ERR("Failed to send \"rotate stream\" command");
1197 /* Receive response. */
1198 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1200 ERR("Failed to receive \"rotate streams\" command reply");
1204 reply
.ret_code
= be32toh(reply
.ret_code
);
1205 if (reply
.ret_code
!= LTTNG_OK
) {
1207 ERR("Relayd rotate streams replied error %d", reply
.ret_code
);
1211 DBG("Relayd rotated streams successfully");
1215 lttng_dynamic_buffer_reset(&payload
);
1219 int relayd_create_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1220 struct lttng_trace_chunk
*chunk
)
1223 enum lttng_trace_chunk_status status
;
1224 struct lttcomm_relayd_create_trace_chunk msg
= {};
1225 struct lttcomm_relayd_generic_reply reply
= {};
1226 struct lttng_dynamic_buffer payload
;
1228 time_t creation_timestamp
;
1229 const char *chunk_name
;
1230 size_t chunk_name_length
;
1231 bool overridden_name
;
1233 lttng_dynamic_buffer_init(&payload
);
1235 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1236 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1241 status
= lttng_trace_chunk_get_creation_timestamp(
1242 chunk
, &creation_timestamp
);
1243 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1248 status
= lttng_trace_chunk_get_name(
1249 chunk
, &chunk_name
, &overridden_name
);
1250 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1251 status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1256 chunk_name_length
= overridden_name
? (strlen(chunk_name
) + 1) : 0;
1257 msg
= (typeof(msg
)){
1258 .chunk_id
= htobe64(chunk_id
),
1259 .creation_timestamp
= htobe64((uint64_t) creation_timestamp
),
1260 .override_name_length
= htobe32((uint32_t) chunk_name_length
),
1263 ret
= lttng_dynamic_buffer_append(&payload
, &msg
, sizeof(msg
));
1267 if (chunk_name_length
) {
1268 ret
= lttng_dynamic_buffer_append(
1269 &payload
, chunk_name
, chunk_name_length
);
1275 ret
= send_command(sock
, RELAYD_CREATE_TRACE_CHUNK
, payload
.data
,
1278 ERR("Failed to send trace chunk creation command to relay daemon");
1282 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1284 ERR("Failed to receive relay daemon trace chunk creation command reply");
1288 reply
.ret_code
= be32toh(reply
.ret_code
);
1289 if (reply
.ret_code
!= LTTNG_OK
) {
1291 ERR("Relayd trace chunk create replied error %d",
1295 DBG("Relayd successfully created trace chunk: chunk_id = %" PRIu64
,
1300 lttng_dynamic_buffer_reset(&payload
);
1304 int relayd_close_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1305 struct lttng_trace_chunk
*chunk
)
1308 enum lttng_trace_chunk_status status
;
1309 struct lttcomm_relayd_close_trace_chunk msg
= {};
1310 struct lttcomm_relayd_generic_reply reply
= {};
1312 time_t close_timestamp
;
1313 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
1315 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1316 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1317 ERR("Failed to get trace chunk id");
1322 status
= lttng_trace_chunk_get_close_timestamp(chunk
, &close_timestamp
);
1323 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1324 ERR("Failed to get trace chunk close timestamp");
1329 status
= lttng_trace_chunk_get_close_command(chunk
,
1330 &close_command
.value
);
1332 case LTTNG_TRACE_CHUNK_STATUS_OK
:
1333 close_command
.is_set
= 1;
1335 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
1338 ERR("Failed to get trace chunk close command");
1343 msg
= (typeof(msg
)){
1344 .chunk_id
= htobe64(chunk_id
),
1345 .close_timestamp
= htobe64((uint64_t) close_timestamp
),
1347 .value
= htobe32((uint32_t) close_command
.value
),
1348 .is_set
= close_command
.is_set
,
1352 ret
= send_command(sock
, RELAYD_CLOSE_TRACE_CHUNK
, &msg
, sizeof(msg
),
1355 ERR("Failed to send trace chunk close command to relay daemon");
1359 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1361 ERR("Failed to receive relay daemon trace chunk close command reply");
1365 reply
.ret_code
= be32toh(reply
.ret_code
);
1366 if (reply
.ret_code
!= LTTNG_OK
) {
1368 ERR("Relayd trace chunk close replied error %d",
1372 DBG("Relayd successfully closed trace chunk: chunk_id = %" PRIu64
,
1379 int relayd_trace_chunk_exists(struct lttcomm_relayd_sock
*sock
,
1380 uint64_t chunk_id
, bool *chunk_exists
)
1383 struct lttcomm_relayd_trace_chunk_exists msg
= {};
1384 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
1386 msg
= (typeof(msg
)){
1387 .chunk_id
= htobe64(chunk_id
),
1390 ret
= send_command(sock
, RELAYD_TRACE_CHUNK_EXISTS
, &msg
, sizeof(msg
),
1393 ERR("Failed to send trace chunk exists command to relay daemon");
1397 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1399 ERR("Failed to receive relay daemon trace chunk close command reply");
1403 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1404 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1406 ERR("Relayd trace chunk close replied error %d",
1407 reply
.generic
.ret_code
);
1410 DBG("Relayd successfully checked trace chunk existence: chunk_id = %" PRIu64
1411 ", exists = %s", chunk_id
,
1412 reply
.trace_chunk_exists
? "true" : "false");
1413 *chunk_exists
= !!reply
.trace_chunk_exists
;