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 bool relayd_supports_chunks(const struct lttcomm_relayd_sock
*sock
)
40 if (sock
->major
> 2) {
42 } else if (sock
->major
== 2 && sock
->minor
>= 11) {
49 bool relayd_supports_get_configuration(const struct lttcomm_relayd_sock
*sock
)
51 if (sock
->major
> 2) {
53 } else if (sock
->major
== 2 && sock
->minor
>= 12) {
60 * Send command. Fill up the header and append the data.
62 static int send_command(struct lttcomm_relayd_sock
*rsock
,
63 enum lttcomm_relayd_command cmd
, const void *data
, size_t size
,
67 struct lttcomm_relayd_hdr header
;
69 uint64_t buf_size
= sizeof(header
);
71 if (rsock
->sock
.fd
< 0) {
79 buf
= zmalloc(buf_size
);
81 PERROR("zmalloc relayd send command buf");
86 memset(&header
, 0, sizeof(header
));
87 header
.cmd
= htobe32(cmd
);
88 header
.data_size
= htobe64(size
);
90 /* Zeroed for now since not used. */
91 header
.cmd_version
= 0;
92 header
.circuit_id
= 0;
94 /* Prepare buffer to send. */
95 memcpy(buf
, &header
, sizeof(header
));
97 memcpy(buf
+ sizeof(header
), data
, size
);
100 DBG3("Relayd sending command %d of size %" PRIu64
, (int) cmd
, buf_size
);
101 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
103 PERROR("Failed to send command %d of size %" PRIu64
,
104 (int) cmd
, buf_size
);
115 * Receive reply data on socket. This MUST be call after send_command or else
116 * could result in unexpected behavior(s).
118 static int recv_reply(struct lttcomm_relayd_sock
*rsock
, void *data
, size_t size
)
122 if (rsock
->sock
.fd
< 0) {
126 DBG3("Relayd waiting for reply of size %zu", size
);
128 ret
= rsock
->sock
.ops
->recvmsg(&rsock
->sock
, data
, size
, 0);
129 if (ret
<= 0 || ret
!= size
) {
131 /* Orderly shutdown. */
132 DBG("Socket %d has performed an orderly shutdown", rsock
->sock
.fd
);
134 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
135 rsock
->sock
.fd
, size
, ret
);
137 /* Always return -1 here and the caller can use errno. */
147 * Starting from 2.11, RELAYD_CREATE_SESSION payload (session_name,
148 * hostname, and base_path) have no length restriction on the sender side.
149 * Length for both payloads is stored in the msg struct. A new dynamic size
150 * payload size is introduced.
152 static int relayd_create_session_2_11(struct lttcomm_relayd_sock
*rsock
,
153 const char *session_name
, const char *hostname
,
154 const char *base_path
, int session_live_timer
,
155 unsigned int snapshot
, uint64_t sessiond_session_id
,
156 const lttng_uuid sessiond_uuid
, const uint64_t *current_chunk_id
,
157 time_t creation_time
, bool session_name_contains_creation_time
,
158 struct lttcomm_relayd_create_session_reply_2_11
*reply
,
162 struct lttcomm_relayd_create_session_2_11
*msg
= NULL
;
163 size_t session_name_len
;
165 size_t base_path_len
;
172 /* The three names are sent with a '\0' delimiter between them. */
173 session_name_len
= strlen(session_name
) + 1;
174 hostname_len
= strlen(hostname
) + 1;
175 base_path_len
= strlen(base_path
) + 1;
177 msg_length
= sizeof(*msg
) + session_name_len
+ hostname_len
+ base_path_len
;
178 msg
= zmalloc(msg_length
);
180 PERROR("zmalloc create_session_2_11 command message");
185 assert(session_name_len
<= UINT32_MAX
);
186 msg
->session_name_len
= htobe32(session_name_len
);
188 assert(hostname_len
<= UINT32_MAX
);
189 msg
->hostname_len
= htobe32(hostname_len
);
191 assert(base_path_len
<= UINT32_MAX
);
192 msg
->base_path_len
= htobe32(base_path_len
);
195 if (lttng_strncpy(dst
, session_name
, session_name_len
)) {
199 dst
+= session_name_len
;
200 if (lttng_strncpy(dst
, hostname
, hostname_len
)) {
205 if (lttng_strncpy(dst
, base_path
, base_path_len
)) {
210 msg
->live_timer
= htobe32(session_live_timer
);
211 msg
->snapshot
= !!snapshot
;
213 lttng_uuid_copy(msg
->sessiond_uuid
, sessiond_uuid
);
214 msg
->session_id
= htobe64(sessiond_session_id
);
215 msg
->session_name_contains_creation_time
= session_name_contains_creation_time
;
216 if (current_chunk_id
) {
217 LTTNG_OPTIONAL_SET(&msg
->current_chunk_id
,
218 htobe64(*current_chunk_id
));
221 msg
->creation_time
= htobe64((uint64_t) creation_time
);
224 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, msg
, msg_length
, 0);
228 /* Receive response */
229 ret
= recv_reply(rsock
, reply
, sizeof(*reply
));
233 reply
->generic
.session_id
= be64toh(reply
->generic
.session_id
);
234 reply
->generic
.ret_code
= be32toh(reply
->generic
.ret_code
);
235 reply
->output_path_length
= be32toh(reply
->output_path_length
);
236 if (reply
->output_path_length
>= LTTNG_PATH_MAX
) {
237 ERR("Invalid session output path length in reply (%" PRIu32
" bytes) exceeds maximal allowed length (%d bytes)",
238 reply
->output_path_length
, LTTNG_PATH_MAX
);
242 ret
= recv_reply(rsock
, output_path
, reply
->output_path_length
);
251 * From 2.4 to 2.10, RELAYD_CREATE_SESSION takes additional parameters to
252 * support the live reading capability.
254 static int relayd_create_session_2_4(struct lttcomm_relayd_sock
*rsock
,
255 const char *session_name
, const char *hostname
,
256 int session_live_timer
, unsigned int snapshot
,
257 struct lttcomm_relayd_status_session
*reply
)
260 struct lttcomm_relayd_create_session_2_4 msg
;
262 if (lttng_strncpy(msg
.session_name
, session_name
,
263 sizeof(msg
.session_name
))) {
267 if (lttng_strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
))) {
271 msg
.live_timer
= htobe32(session_live_timer
);
272 msg
.snapshot
= htobe32(snapshot
);
275 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, &msg
, sizeof(msg
), 0);
280 /* Receive response */
281 ret
= recv_reply(rsock
, reply
, sizeof(*reply
));
285 reply
->session_id
= be64toh(reply
->session_id
);
286 reply
->ret_code
= be32toh(reply
->ret_code
);
292 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
294 static int relayd_create_session_2_1(struct lttcomm_relayd_sock
*rsock
,
295 struct lttcomm_relayd_status_session
*reply
)
300 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
305 /* Receive response */
306 ret
= recv_reply(rsock
, reply
, sizeof(*reply
));
310 reply
->session_id
= be64toh(reply
->session_id
);
311 reply
->ret_code
= be32toh(reply
->ret_code
);
317 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
318 * set session_id of the relayd if we have a successful reply from the relayd.
320 * On success, return 0 else a negative value which is either an errno error or
321 * a lttng error code from the relayd.
323 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
,
324 uint64_t *relayd_session_id
,
325 const char *session_name
, const char *hostname
,
326 const char *base_path
, int session_live_timer
,
327 unsigned int snapshot
, uint64_t sessiond_session_id
,
328 const lttng_uuid sessiond_uuid
,
329 const uint64_t *current_chunk_id
,
330 time_t creation_time
, bool session_name_contains_creation_time
,
334 struct lttcomm_relayd_create_session_reply_2_11 reply
= {};
337 assert(relayd_session_id
);
339 DBG("Relayd create session");
341 if (rsock
->minor
< 4) {
342 /* From 2.1 to 2.3 */
343 ret
= relayd_create_session_2_1(rsock
, &reply
.generic
);
344 } else if (rsock
->minor
>= 4 && rsock
->minor
< 11) {
345 /* From 2.4 to 2.10 */
346 ret
= relayd_create_session_2_4(rsock
, session_name
,
347 hostname
, session_live_timer
, snapshot
,
350 /* From 2.11 to ... */
351 ret
= relayd_create_session_2_11(rsock
, session_name
,
352 hostname
, base_path
, session_live_timer
, snapshot
,
353 sessiond_session_id
, sessiond_uuid
,
354 current_chunk_id
, creation_time
,
355 session_name_contains_creation_time
,
356 &reply
, output_path
);
363 /* Return session id or negative ret code. */
364 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
366 ERR("Relayd create session replied error %d",
367 reply
.generic
.ret_code
);
371 *relayd_session_id
= reply
.generic
.session_id
;
374 DBG("Relayd session created with id %" PRIu64
, reply
.generic
.session_id
);
380 static int relayd_add_stream_2_1(struct lttcomm_relayd_sock
*rsock
,
381 const char *channel_name
, const char *pathname
)
384 struct lttcomm_relayd_add_stream msg
;
386 memset(&msg
, 0, sizeof(msg
));
387 if (lttng_strncpy(msg
.channel_name
, channel_name
,
388 sizeof(msg
.channel_name
))) {
393 if (lttng_strncpy(msg
.pathname
, pathname
,
394 sizeof(msg
.pathname
))) {
400 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
410 static int relayd_add_stream_2_2(struct lttcomm_relayd_sock
*rsock
,
411 const char *channel_name
, const char *pathname
,
412 uint64_t tracefile_size
, uint64_t tracefile_count
)
415 struct lttcomm_relayd_add_stream_2_2 msg
;
417 memset(&msg
, 0, sizeof(msg
));
418 /* Compat with relayd 2.2 to 2.10 */
419 if (lttng_strncpy(msg
.channel_name
, channel_name
,
420 sizeof(msg
.channel_name
))) {
424 if (lttng_strncpy(msg
.pathname
, pathname
,
425 sizeof(msg
.pathname
))) {
429 msg
.tracefile_size
= htobe64(tracefile_size
);
430 msg
.tracefile_count
= htobe64(tracefile_count
);
433 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
442 static int relayd_add_stream_2_11(struct lttcomm_relayd_sock
*rsock
,
443 const char *channel_name
, const char *pathname
,
444 uint64_t tracefile_size
, uint64_t tracefile_count
,
445 uint64_t trace_archive_id
)
448 struct lttcomm_relayd_add_stream_2_11
*msg
= NULL
;
449 size_t channel_name_len
;
453 /* The two names are sent with a '\0' delimiter between them. */
454 channel_name_len
= strlen(channel_name
) + 1;
455 pathname_len
= strlen(pathname
) + 1;
457 msg_length
= sizeof(*msg
) + channel_name_len
+ pathname_len
;
458 msg
= zmalloc(msg_length
);
460 PERROR("zmalloc add_stream_2_11 command message");
465 assert(channel_name_len
<= UINT32_MAX
);
466 msg
->channel_name_len
= htobe32(channel_name_len
);
468 assert(pathname_len
<= UINT32_MAX
);
469 msg
->pathname_len
= htobe32(pathname_len
);
471 if (lttng_strncpy(msg
->names
, channel_name
, channel_name_len
)) {
475 if (lttng_strncpy(msg
->names
+ channel_name_len
, pathname
, pathname_len
)) {
480 msg
->tracefile_size
= htobe64(tracefile_size
);
481 msg
->tracefile_count
= htobe64(tracefile_count
);
482 msg
->trace_chunk_id
= htobe64(trace_archive_id
);
485 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) msg
, msg_length
, 0);
496 * Add stream on the relayd and assign stream handle to the stream_id argument.
498 * Chunks are not supported by relayd prior to 2.11, but are used to
499 * internally between session daemon and consumer daemon to keep track
500 * of the channel and stream output path.
502 * On success return 0 else return ret_code negative value.
504 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
505 const char *domain_name
, const char *_pathname
, uint64_t *stream_id
,
506 uint64_t tracefile_size
, uint64_t tracefile_count
,
507 struct lttng_trace_chunk
*trace_chunk
)
510 struct lttcomm_relayd_status_stream reply
;
511 char pathname
[RELAYD_COMM_LTTNG_PATH_MAX
];
514 /* Code flow error. Safety net. */
516 assert(channel_name
);
521 DBG("Relayd adding stream for channel name %s", channel_name
);
523 if (_pathname
[0] == '\0') {
528 ret
= snprintf(pathname
, RELAYD_COMM_LTTNG_PATH_MAX
, "%s%s%s",
529 domain_name
, separator
, _pathname
);
530 if (ret
<= 0 || ret
>= RELAYD_COMM_LTTNG_PATH_MAX
) {
531 ERR("stream path too long.");
536 /* Compat with relayd 2.1 */
537 if (rsock
->minor
== 1) {
539 ret
= relayd_add_stream_2_1(rsock
, channel_name
, pathname
);
541 } else if (rsock
->minor
> 1 && rsock
->minor
< 11) {
542 /* From 2.2 to 2.10 */
543 ret
= relayd_add_stream_2_2(rsock
, channel_name
, pathname
,
544 tracefile_size
, tracefile_count
);
546 enum lttng_trace_chunk_status chunk_status
;
549 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
,
551 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
553 /* From 2.11 to ...*/
554 ret
= relayd_add_stream_2_11(rsock
, channel_name
, pathname
,
555 tracefile_size
, tracefile_count
,
564 /* Waiting for reply */
565 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
570 /* Back to host bytes order. */
571 reply
.handle
= be64toh(reply
.handle
);
572 reply
.ret_code
= be32toh(reply
.ret_code
);
574 /* Return session id or negative ret code. */
575 if (reply
.ret_code
!= LTTNG_OK
) {
577 ERR("Relayd add stream replied error %d", reply
.ret_code
);
581 *stream_id
= reply
.handle
;
584 DBG("Relayd stream added successfully with handle %" PRIu64
,
592 * Inform the relay that all the streams for the current channel has been sent.
594 * On success return 0 else return ret_code negative value.
596 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
599 struct lttcomm_relayd_generic_reply reply
;
601 /* Code flow error. Safety net. */
604 DBG("Relayd sending streams sent.");
606 /* This feature was introduced in 2.4, ignore it for earlier versions. */
607 if (rsock
->minor
< 4) {
613 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, NULL
, 0, 0);
618 /* Waiting for reply */
619 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
624 /* Back to host bytes order. */
625 reply
.ret_code
= be32toh(reply
.ret_code
);
627 /* Return session id or negative ret code. */
628 if (reply
.ret_code
!= LTTNG_OK
) {
630 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
637 DBG("Relayd streams sent success");
645 * Check version numbers on the relayd.
646 * If major versions are compatible, we assign minor_to_use to the
647 * minor version of the procotol we are going to use for this session.
649 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
650 * otherwise, or a negative value on network errors.
652 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
655 struct lttcomm_relayd_version msg
;
657 /* Code flow error. Safety net. */
660 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
663 memset(&msg
, 0, sizeof(msg
));
664 /* Prepare network byte order before transmission. */
665 msg
.major
= htobe32(rsock
->major
);
666 msg
.minor
= htobe32(rsock
->minor
);
669 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
674 /* Receive response */
675 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
680 /* Set back to host bytes order */
681 msg
.major
= be32toh(msg
.major
);
682 msg
.minor
= be32toh(msg
.minor
);
685 * Only validate the major version. If the other side is higher,
686 * communication is not possible. Only major version equal can talk to each
687 * other. If the minor version differs, the lowest version is used by both
690 if (msg
.major
!= rsock
->major
) {
692 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
693 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
694 msg
.major
, rsock
->major
);
699 * If the relayd's minor version is higher, it will adapt to our version so
700 * we can continue to use the latest relayd communication data structure.
701 * If the received minor version is higher, the relayd should adapt to us.
703 if (rsock
->minor
> msg
.minor
) {
704 rsock
->minor
= msg
.minor
;
707 /* Version number compatible */
708 DBG2("Relayd version is compatible, using protocol version %u.%u",
709 rsock
->major
, rsock
->minor
);
717 * Add stream on the relayd and assign stream handle to the stream_id argument.
719 * On success return 0 else return ret_code negative value.
721 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
725 /* Code flow error. Safety net. */
728 DBG("Relayd sending metadata of size %zu", len
);
731 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
736 DBG2("Relayd metadata added successfully");
739 * After that call, the metadata data MUST be sent to the relayd so the
740 * receive size on the other end matches the len of the metadata packet
741 * header. This is why we don't wait for a reply here.
749 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
751 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
753 /* Code flow error. Safety net. */
756 if (!rsock
->sock
.ops
) {
758 * Attempting a connect on a non-initialized socket.
763 DBG3("Relayd connect ...");
765 return rsock
->sock
.ops
->connect(&rsock
->sock
);
769 * Close relayd socket with an allocated lttcomm_relayd_sock.
771 * If no socket operations are found, simply return 0 meaning that everything
772 * is fine. Without operations, the socket can not possibly be opened or used.
773 * This is possible if the socket was allocated but not created. However, the
774 * caller could simply use it to store a valid file descriptor for instance
775 * passed over a Unix socket and call this to cleanup but still without a valid
778 * Return the close returned value. On error, a negative value is usually
779 * returned back from close(2).
781 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
785 /* Code flow error. Safety net. */
788 /* An invalid fd is fine, return success. */
789 if (rsock
->sock
.fd
< 0) {
794 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
796 if (rsock
->sock
.ops
) {
797 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
799 /* Default call if no specific ops found. */
800 ret
= close(rsock
->sock
.fd
);
802 PERROR("relayd_close default close");
812 * Send data header structure to the relayd.
814 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
815 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
819 /* Code flow error. Safety net. */
823 if (rsock
->sock
.fd
< 0) {
827 DBG3("Relayd sending data header of size %zu", size
);
829 /* Again, safety net */
831 size
= sizeof(struct lttcomm_relayd_data_hdr
);
834 /* Only send data header. */
835 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
842 * The data MUST be sent right after that command for the receive on the
843 * other end to match the size in the header.
851 * Send close stream command to the relayd.
853 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
854 uint64_t last_net_seq_num
)
857 struct lttcomm_relayd_close_stream msg
;
858 struct lttcomm_relayd_generic_reply reply
;
860 /* Code flow error. Safety net. */
863 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
865 memset(&msg
, 0, sizeof(msg
));
866 msg
.stream_id
= htobe64(stream_id
);
867 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
870 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
875 /* Receive response */
876 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
881 reply
.ret_code
= be32toh(reply
.ret_code
);
883 /* Return session id or negative ret code. */
884 if (reply
.ret_code
!= LTTNG_OK
) {
886 ERR("Relayd close stream replied error %d", reply
.ret_code
);
892 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
899 * Check for data availability for a given stream id.
901 * Return 0 if NOT pending, 1 if so and a negative value on error.
903 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
904 uint64_t last_net_seq_num
)
907 struct lttcomm_relayd_data_pending msg
;
908 struct lttcomm_relayd_generic_reply reply
;
910 /* Code flow error. Safety net. */
913 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
915 memset(&msg
, 0, sizeof(msg
));
916 msg
.stream_id
= htobe64(stream_id
);
917 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
920 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
926 /* Receive response */
927 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
932 reply
.ret_code
= be32toh(reply
.ret_code
);
934 /* Return session id or negative ret code. */
935 if (reply
.ret_code
>= LTTNG_OK
) {
936 ERR("Relayd data pending replied error %d", reply
.ret_code
);
939 /* At this point, the ret code is either 1 or 0 */
940 ret
= reply
.ret_code
;
942 DBG("Relayd data is %s pending for stream id %" PRIu64
,
943 ret
== 1 ? "" : "NOT", stream_id
);
950 * Check on the relayd side for a quiescent state on the control socket.
952 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
953 uint64_t metadata_stream_id
)
956 struct lttcomm_relayd_quiescent_control msg
;
957 struct lttcomm_relayd_generic_reply reply
;
959 /* Code flow error. Safety net. */
962 DBG("Relayd checking quiescent control state");
964 memset(&msg
, 0, sizeof(msg
));
965 msg
.stream_id
= htobe64(metadata_stream_id
);
968 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
973 /* Receive response */
974 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
979 reply
.ret_code
= be32toh(reply
.ret_code
);
981 /* Return session id or negative ret code. */
982 if (reply
.ret_code
!= LTTNG_OK
) {
984 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
988 /* Control socket is quiescent */
996 * Begin a data pending command for a specific session id.
998 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
1001 struct lttcomm_relayd_begin_data_pending msg
;
1002 struct lttcomm_relayd_generic_reply reply
;
1004 /* Code flow error. Safety net. */
1007 DBG("Relayd begin data pending");
1009 memset(&msg
, 0, sizeof(msg
));
1010 msg
.session_id
= htobe64(id
);
1013 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
1018 /* Receive response */
1019 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1024 reply
.ret_code
= be32toh(reply
.ret_code
);
1026 /* Return session id or negative ret code. */
1027 if (reply
.ret_code
!= LTTNG_OK
) {
1029 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
1040 * End a data pending command for a specific session id.
1042 * Return 0 on success and set is_data_inflight to 0 if no data is being
1043 * streamed or 1 if it is the case.
1045 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
1046 unsigned int *is_data_inflight
)
1049 struct lttcomm_relayd_end_data_pending msg
;
1050 struct lttcomm_relayd_generic_reply reply
;
1052 /* Code flow error. Safety net. */
1055 DBG("Relayd end data pending");
1057 memset(&msg
, 0, sizeof(msg
));
1058 msg
.session_id
= htobe64(id
);
1061 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
1066 /* Receive response */
1067 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1072 recv_ret
= be32toh(reply
.ret_code
);
1078 *is_data_inflight
= recv_ret
;
1080 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
1089 * Send index to the relayd.
1091 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
1092 struct ctf_packet_index
*index
, uint64_t relay_stream_id
,
1093 uint64_t net_seq_num
)
1096 struct lttcomm_relayd_index msg
;
1097 struct lttcomm_relayd_generic_reply reply
;
1099 /* Code flow error. Safety net. */
1102 if (rsock
->minor
< 4) {
1103 DBG("Not sending indexes before protocol 2.4");
1108 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
1110 memset(&msg
, 0, sizeof(msg
));
1111 msg
.relay_stream_id
= htobe64(relay_stream_id
);
1112 msg
.net_seq_num
= htobe64(net_seq_num
);
1114 /* The index is already in big endian. */
1115 msg
.packet_size
= index
->packet_size
;
1116 msg
.content_size
= index
->content_size
;
1117 msg
.timestamp_begin
= index
->timestamp_begin
;
1118 msg
.timestamp_end
= index
->timestamp_end
;
1119 msg
.events_discarded
= index
->events_discarded
;
1120 msg
.stream_id
= index
->stream_id
;
1122 if (rsock
->minor
>= 8) {
1123 msg
.stream_instance_id
= index
->stream_instance_id
;
1124 msg
.packet_seq_num
= index
->packet_seq_num
;
1128 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
,
1129 lttcomm_relayd_index_len(lttng_to_index_major(rsock
->major
,
1131 lttng_to_index_minor(rsock
->major
, rsock
->minor
)),
1137 /* Receive response */
1138 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1143 reply
.ret_code
= be32toh(reply
.ret_code
);
1145 /* Return session id or negative ret code. */
1146 if (reply
.ret_code
!= LTTNG_OK
) {
1148 ERR("Relayd send index replied error %d", reply
.ret_code
);
1159 * Ask the relay to reset the metadata trace file (regeneration).
1161 int relayd_reset_metadata(struct lttcomm_relayd_sock
*rsock
,
1162 uint64_t stream_id
, uint64_t version
)
1165 struct lttcomm_relayd_reset_metadata msg
;
1166 struct lttcomm_relayd_generic_reply reply
;
1168 /* Code flow error. Safety net. */
1171 /* Should have been prevented by the sessiond. */
1172 if (rsock
->minor
< 8) {
1173 ERR("Metadata regeneration unsupported before 2.8");
1178 DBG("Relayd reset metadata stream id %" PRIu64
, stream_id
);
1180 memset(&msg
, 0, sizeof(msg
));
1181 msg
.stream_id
= htobe64(stream_id
);
1182 msg
.version
= htobe64(version
);
1185 ret
= send_command(rsock
, RELAYD_RESET_METADATA
, (void *) &msg
, sizeof(msg
), 0);
1190 /* Receive response */
1191 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1196 reply
.ret_code
= be32toh(reply
.ret_code
);
1198 /* Return session id or negative ret code. */
1199 if (reply
.ret_code
!= LTTNG_OK
) {
1201 ERR("Relayd reset metadata replied error %d", reply
.ret_code
);
1207 DBG("Relayd reset metadata stream id %" PRIu64
" successfully", stream_id
);
1213 int relayd_rotate_streams(struct lttcomm_relayd_sock
*sock
,
1214 unsigned int stream_count
, const uint64_t *new_chunk_id
,
1215 const struct relayd_stream_rotation_position
*positions
)
1219 struct lttng_dynamic_buffer payload
;
1220 struct lttcomm_relayd_generic_reply reply
= {};
1221 const struct lttcomm_relayd_rotate_streams msg
= {
1222 .stream_count
= htobe32((uint32_t) stream_count
),
1223 .new_chunk_id
= (typeof(msg
.new_chunk_id
)) {
1224 .is_set
= !!new_chunk_id
,
1225 .value
= htobe64(new_chunk_id
? *new_chunk_id
: 0),
1228 char new_chunk_id_buf
[MAX_INT_DEC_LEN(*new_chunk_id
)] = {};
1229 const char *new_chunk_id_str
;
1231 if (!relayd_supports_chunks(sock
)) {
1232 DBG("Refusing to rotate remote streams: relayd does not support chunks");
1236 lttng_dynamic_buffer_init(&payload
);
1238 /* Code flow error. Safety net. */
1242 ret
= snprintf(new_chunk_id_buf
, sizeof(new_chunk_id_buf
),
1243 "%" PRIu64
, *new_chunk_id
);
1244 if (ret
== -1 || ret
>= sizeof(new_chunk_id_buf
)) {
1245 new_chunk_id_str
= "formatting error";
1247 new_chunk_id_str
= new_chunk_id_buf
;
1250 new_chunk_id_str
= "none";
1253 DBG("Preparing \"rotate streams\" command payload: new_chunk_id = %s, stream_count = %u",
1254 new_chunk_id_str
, stream_count
);
1256 ret
= lttng_dynamic_buffer_append(&payload
, &msg
, sizeof(msg
));
1258 ERR("Failed to allocate \"rotate streams\" command payload");
1262 for (i
= 0; i
< stream_count
; i
++) {
1263 const struct relayd_stream_rotation_position
*position
=
1265 const struct lttcomm_relayd_stream_rotation_position comm_position
= {
1266 .stream_id
= htobe64(position
->stream_id
),
1267 .rotate_at_seq_num
= htobe64(
1268 position
->rotate_at_seq_num
),
1271 DBG("Rotate stream %" PRIu64
"at sequence number %" PRIu64
,
1272 position
->stream_id
,
1273 position
->rotate_at_seq_num
);
1274 ret
= lttng_dynamic_buffer_append(&payload
, &comm_position
,
1275 sizeof(comm_position
));
1277 ERR("Failed to allocate \"rotate streams\" command payload");
1283 ret
= send_command(sock
, RELAYD_ROTATE_STREAMS
, payload
.data
,
1286 ERR("Failed to send \"rotate stream\" command");
1290 /* Receive response. */
1291 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1293 ERR("Failed to receive \"rotate streams\" command reply");
1297 reply
.ret_code
= be32toh(reply
.ret_code
);
1298 if (reply
.ret_code
!= LTTNG_OK
) {
1300 ERR("Relayd rotate streams replied error %d", reply
.ret_code
);
1304 DBG("Relayd rotated streams successfully");
1308 lttng_dynamic_buffer_reset(&payload
);
1312 int relayd_create_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1313 struct lttng_trace_chunk
*chunk
)
1316 enum lttng_trace_chunk_status status
;
1317 struct lttcomm_relayd_create_trace_chunk msg
= {};
1318 struct lttcomm_relayd_generic_reply reply
= {};
1319 struct lttng_dynamic_buffer payload
;
1321 time_t creation_timestamp
;
1322 const char *chunk_name
;
1323 size_t chunk_name_length
;
1324 bool overridden_name
;
1326 lttng_dynamic_buffer_init(&payload
);
1328 if (!relayd_supports_chunks(sock
)) {
1329 DBG("Refusing to create remote trace chunk: relayd does not support chunks");
1333 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1334 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1339 status
= lttng_trace_chunk_get_creation_timestamp(
1340 chunk
, &creation_timestamp
);
1341 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1346 status
= lttng_trace_chunk_get_name(
1347 chunk
, &chunk_name
, &overridden_name
);
1348 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1349 status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1354 chunk_name_length
= overridden_name
? (strlen(chunk_name
) + 1) : 0;
1355 msg
= (typeof(msg
)){
1356 .chunk_id
= htobe64(chunk_id
),
1357 .creation_timestamp
= htobe64((uint64_t) creation_timestamp
),
1358 .override_name_length
= htobe32((uint32_t) chunk_name_length
),
1361 ret
= lttng_dynamic_buffer_append(&payload
, &msg
, sizeof(msg
));
1365 if (chunk_name_length
) {
1366 ret
= lttng_dynamic_buffer_append(
1367 &payload
, chunk_name
, chunk_name_length
);
1373 ret
= send_command(sock
, RELAYD_CREATE_TRACE_CHUNK
, payload
.data
,
1376 ERR("Failed to send trace chunk creation command to relay daemon");
1380 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1382 ERR("Failed to receive relay daemon trace chunk creation command reply");
1386 reply
.ret_code
= be32toh(reply
.ret_code
);
1387 if (reply
.ret_code
!= LTTNG_OK
) {
1389 ERR("Relayd trace chunk create replied error %d",
1393 DBG("Relayd successfully created trace chunk: chunk_id = %" PRIu64
,
1398 lttng_dynamic_buffer_reset(&payload
);
1402 int relayd_close_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1403 struct lttng_trace_chunk
*chunk
,
1407 enum lttng_trace_chunk_status status
;
1408 struct lttcomm_relayd_close_trace_chunk msg
= {};
1409 struct lttcomm_relayd_close_trace_chunk_reply reply
= {};
1411 time_t close_timestamp
;
1412 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
1414 if (!relayd_supports_chunks(sock
)) {
1415 DBG("Refusing to close remote trace chunk: relayd does not support chunks");
1419 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1420 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1421 ERR("Failed to get trace chunk id");
1426 status
= lttng_trace_chunk_get_close_timestamp(chunk
, &close_timestamp
);
1427 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1428 ERR("Failed to get trace chunk close timestamp");
1433 status
= lttng_trace_chunk_get_close_command(chunk
,
1434 &close_command
.value
);
1436 case LTTNG_TRACE_CHUNK_STATUS_OK
:
1437 close_command
.is_set
= 1;
1439 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
1442 ERR("Failed to get trace chunk close command");
1447 msg
= (typeof(msg
)){
1448 .chunk_id
= htobe64(chunk_id
),
1449 .close_timestamp
= htobe64((uint64_t) close_timestamp
),
1451 .value
= htobe32((uint32_t) close_command
.value
),
1452 .is_set
= close_command
.is_set
,
1456 ret
= send_command(sock
, RELAYD_CLOSE_TRACE_CHUNK
, &msg
, sizeof(msg
),
1459 ERR("Failed to send trace chunk close command to relay daemon");
1463 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1465 ERR("Failed to receive relay daemon trace chunk close command reply");
1469 reply
.path_length
= be32toh(reply
.path_length
);
1470 if (reply
.path_length
>= LTTNG_PATH_MAX
) {
1471 ERR("Chunk path too long");
1476 ret
= recv_reply(sock
, path
, reply
.path_length
);
1478 ERR("Failed to receive relay daemon trace chunk close command reply");
1481 if (path
[reply
.path_length
- 1] != '\0') {
1482 ERR("Invalid trace chunk path returned by relay daemon (not null-terminated)");
1487 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1488 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1490 ERR("Relayd trace chunk close replied error %d",
1491 reply
.generic
.ret_code
);
1494 DBG("Relayd successfully closed trace chunk: chunk_id = %" PRIu64
,
1501 int relayd_trace_chunk_exists(struct lttcomm_relayd_sock
*sock
,
1502 uint64_t chunk_id
, bool *chunk_exists
)
1505 struct lttcomm_relayd_trace_chunk_exists msg
= {};
1506 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
1508 if (!relayd_supports_chunks(sock
)) {
1509 DBG("Refusing to check for trace chunk existence: relayd does not support chunks");
1510 /* The chunk will never exist */
1511 *chunk_exists
= false;
1515 msg
= (typeof(msg
)){
1516 .chunk_id
= htobe64(chunk_id
),
1519 ret
= send_command(sock
, RELAYD_TRACE_CHUNK_EXISTS
, &msg
, sizeof(msg
),
1522 ERR("Failed to send trace chunk exists command to relay daemon");
1526 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1528 ERR("Failed to receive relay daemon trace chunk close command reply");
1532 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1533 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1535 ERR("Relayd trace chunk close replied error %d",
1536 reply
.generic
.ret_code
);
1539 DBG("Relayd successfully checked trace chunk existence: chunk_id = %" PRIu64
1540 ", exists = %s", chunk_id
,
1541 reply
.trace_chunk_exists
? "true" : "false");
1542 *chunk_exists
= !!reply
.trace_chunk_exists
;
1548 int relayd_get_configuration(struct lttcomm_relayd_sock
*sock
,
1549 uint64_t query_flags
,
1550 uint64_t *result_flags
)
1553 struct lttcomm_relayd_get_configuration msg
= (typeof(msg
)) {
1554 .query_flags
= htobe64(query_flags
),
1556 struct lttcomm_relayd_get_configuration_reply reply
= {};
1558 if (!relayd_supports_get_configuration(sock
)) {
1559 DBG("Refusing to get relayd configuration (unsupported by relayd)");
1568 ret
= send_command(sock
, RELAYD_GET_CONFIGURATION
, &msg
, sizeof(msg
),
1571 ERR("Failed to send get configuration command to relay daemon");
1575 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1577 ERR("Failed to receive relay daemon get configuration command reply");
1581 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1582 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1584 ERR("Relayd get configuration replied error %d",
1585 reply
.generic
.ret_code
);
1587 reply
.relayd_configuration_flags
=
1588 be64toh(reply
.relayd_configuration_flags
);
1590 DBG("Relayd successfully got configuration: query_flags = %" PRIu64
1591 ", results_flags = %" PRIu64
, query_flags
,
1592 reply
.relayd_configuration_flags
);
1593 *result_flags
= reply
.relayd_configuration_flags
;