2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <common/common.hpp>
16 #include <common/defaults.hpp>
17 #include <common/compat/endian.hpp>
18 #include <common/compat/string.hpp>
19 #include <common/sessiond-comm/relayd.hpp>
20 #include <common/index/ctf-index.hpp>
21 #include <common/trace-chunk.hpp>
22 #include <common/string-utils/format.hpp>
27 bool relayd_supports_chunks(const struct lttcomm_relayd_sock
*sock
)
29 if (sock
->major
> 2) {
31 } else if (sock
->major
== 2 && sock
->minor
>= 11) {
38 bool relayd_supports_get_configuration(const struct lttcomm_relayd_sock
*sock
)
40 if (sock
->major
> 2) {
42 } else if (sock
->major
== 2 && sock
->minor
>= 12) {
49 * Send command. Fill up the header and append the data.
51 static int send_command(struct lttcomm_relayd_sock
*rsock
,
52 enum lttcomm_relayd_command cmd
, const void *data
, size_t size
,
56 struct lttcomm_relayd_hdr header
;
58 uint64_t buf_size
= sizeof(header
);
60 if (rsock
->sock
.fd
< 0) {
68 buf
= calloc
<char>(buf_size
);
70 PERROR("zmalloc relayd send command buf");
75 memset(&header
, 0, sizeof(header
));
76 header
.cmd
= htobe32(cmd
);
77 header
.data_size
= htobe64(size
);
79 /* Zeroed for now since not used. */
80 header
.cmd_version
= 0;
81 header
.circuit_id
= 0;
83 /* Prepare buffer to send. */
84 memcpy(buf
, &header
, sizeof(header
));
86 memcpy(buf
+ sizeof(header
), data
, size
);
89 DBG3("Relayd sending command %s of size %" PRIu64
,
90 lttcomm_relayd_command_str(cmd
), buf_size
);
91 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
93 PERROR("Failed to send command %s of size %" PRIu64
,
94 lttcomm_relayd_command_str(cmd
), buf_size
);
95 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
106 * Receive reply data on socket. This MUST be call after send_command or else
107 * could result in unexpected behavior(s).
109 static int recv_reply(struct lttcomm_relayd_sock
*rsock
, void *data
, size_t size
)
113 if (rsock
->sock
.fd
< 0) {
117 DBG3("Relayd waiting for reply of size %zu", size
);
119 ret
= rsock
->sock
.ops
->recvmsg(&rsock
->sock
, data
, size
, 0);
120 if (ret
<= 0 || ret
!= size
) {
122 /* Orderly shutdown. */
123 DBG("Socket %d has performed an orderly shutdown", rsock
->sock
.fd
);
125 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
126 rsock
->sock
.fd
, size
, ret
);
128 /* Always return -1 here and the caller can use errno. */
138 * Starting from 2.11, RELAYD_CREATE_SESSION payload (session_name,
139 * hostname, and base_path) have no length restriction on the sender side.
140 * Length for both payloads is stored in the msg struct. A new dynamic size
141 * payload size is introduced.
143 static int relayd_create_session_2_11(struct lttcomm_relayd_sock
*rsock
,
144 const char *session_name
, const char *hostname
,
145 const char *base_path
, int session_live_timer
,
146 unsigned int snapshot
, uint64_t sessiond_session_id
,
147 const lttng_uuid
& sessiond_uuid
, const uint64_t *current_chunk_id
,
148 time_t creation_time
, bool session_name_contains_creation_time
,
149 struct lttcomm_relayd_create_session_reply_2_11
*reply
,
153 struct lttcomm_relayd_create_session_2_11
*msg
= NULL
;
154 size_t session_name_len
;
156 size_t base_path_len
;
163 /* The three names are sent with a '\0' delimiter between them. */
164 session_name_len
= strlen(session_name
) + 1;
165 hostname_len
= strlen(hostname
) + 1;
166 base_path_len
= strlen(base_path
) + 1;
168 msg_length
= sizeof(*msg
) + session_name_len
+ hostname_len
+ base_path_len
;
169 msg
= zmalloc
<lttcomm_relayd_create_session_2_11
>(msg_length
);
171 PERROR("zmalloc create_session_2_11 command message");
176 LTTNG_ASSERT(session_name_len
<= UINT32_MAX
);
177 msg
->session_name_len
= htobe32(session_name_len
);
179 LTTNG_ASSERT(hostname_len
<= UINT32_MAX
);
180 msg
->hostname_len
= htobe32(hostname_len
);
182 LTTNG_ASSERT(base_path_len
<= UINT32_MAX
);
183 msg
->base_path_len
= htobe32(base_path_len
);
186 if (lttng_strncpy(dst
, session_name
, session_name_len
)) {
190 dst
+= session_name_len
;
191 if (lttng_strncpy(dst
, hostname
, hostname_len
)) {
196 if (lttng_strncpy(dst
, base_path
, base_path_len
)) {
201 msg
->live_timer
= htobe32(session_live_timer
);
202 msg
->snapshot
= !!snapshot
;
204 std::copy(sessiond_uuid
.begin(), sessiond_uuid
.end(), msg
->sessiond_uuid
);
205 msg
->session_id
= htobe64(sessiond_session_id
);
206 msg
->session_name_contains_creation_time
= session_name_contains_creation_time
;
207 if (current_chunk_id
) {
208 LTTNG_OPTIONAL_SET(&msg
->current_chunk_id
,
209 htobe64(*current_chunk_id
));
212 msg
->creation_time
= htobe64((uint64_t) creation_time
);
215 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, msg
, msg_length
, 0);
219 /* Receive response */
220 ret
= recv_reply(rsock
, reply
, sizeof(*reply
));
224 reply
->generic
.session_id
= be64toh(reply
->generic
.session_id
);
225 reply
->generic
.ret_code
= be32toh(reply
->generic
.ret_code
);
226 reply
->output_path_length
= be32toh(reply
->output_path_length
);
227 if (reply
->output_path_length
>= LTTNG_PATH_MAX
) {
228 ERR("Invalid session output path length in reply (%" PRIu32
" bytes) exceeds maximal allowed length (%d bytes)",
229 reply
->output_path_length
, LTTNG_PATH_MAX
);
233 ret
= recv_reply(rsock
, output_path
, reply
->output_path_length
);
242 * From 2.4 to 2.10, RELAYD_CREATE_SESSION takes additional parameters to
243 * support the live reading capability.
245 static int relayd_create_session_2_4(struct lttcomm_relayd_sock
*rsock
,
246 const char *session_name
, const char *hostname
,
247 int session_live_timer
, unsigned int snapshot
,
248 struct lttcomm_relayd_status_session
*reply
)
251 struct lttcomm_relayd_create_session_2_4 msg
;
253 if (lttng_strncpy(msg
.session_name
, session_name
,
254 sizeof(msg
.session_name
))) {
258 if (lttng_strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
))) {
262 msg
.live_timer
= htobe32(session_live_timer
);
263 msg
.snapshot
= htobe32(snapshot
);
266 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, &msg
, sizeof(msg
), 0);
271 /* Receive response */
272 ret
= recv_reply(rsock
, reply
, sizeof(*reply
));
276 reply
->session_id
= be64toh(reply
->session_id
);
277 reply
->ret_code
= be32toh(reply
->ret_code
);
283 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
285 static int relayd_create_session_2_1(struct lttcomm_relayd_sock
*rsock
,
286 struct lttcomm_relayd_status_session
*reply
)
291 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
296 /* Receive response */
297 ret
= recv_reply(rsock
, reply
, sizeof(*reply
));
301 reply
->session_id
= be64toh(reply
->session_id
);
302 reply
->ret_code
= be32toh(reply
->ret_code
);
308 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
309 * set session_id of the relayd if we have a successful reply from the relayd.
311 * On success, return 0 else a negative value which is either an errno error or
312 * a lttng error code from the relayd.
314 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
,
315 uint64_t *relayd_session_id
,
316 const char *session_name
, const char *hostname
,
317 const char *base_path
, int session_live_timer
,
318 unsigned int snapshot
, uint64_t sessiond_session_id
,
319 const lttng_uuid
& sessiond_uuid
,
320 const uint64_t *current_chunk_id
,
321 time_t creation_time
, bool session_name_contains_creation_time
,
325 struct lttcomm_relayd_create_session_reply_2_11 reply
= {};
328 LTTNG_ASSERT(relayd_session_id
);
330 DBG("Relayd create session");
332 if (rsock
->minor
< 4) {
333 /* From 2.1 to 2.3 */
334 ret
= relayd_create_session_2_1(rsock
, &reply
.generic
);
335 } else if (rsock
->minor
>= 4 && rsock
->minor
< 11) {
336 /* From 2.4 to 2.10 */
337 ret
= relayd_create_session_2_4(rsock
, session_name
,
338 hostname
, session_live_timer
, snapshot
,
341 /* From 2.11 to ... */
342 ret
= relayd_create_session_2_11(rsock
, session_name
,
343 hostname
, base_path
, session_live_timer
, snapshot
,
344 sessiond_session_id
, sessiond_uuid
,
345 current_chunk_id
, creation_time
,
346 session_name_contains_creation_time
,
347 &reply
, output_path
);
354 /* Return session id or negative ret code. */
355 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
357 ERR("Relayd create session replied error %d",
358 reply
.generic
.ret_code
);
362 *relayd_session_id
= reply
.generic
.session_id
;
365 DBG("Relayd session created with id %" PRIu64
, reply
.generic
.session_id
);
371 static int relayd_add_stream_2_1(struct lttcomm_relayd_sock
*rsock
,
372 const char *channel_name
, const char *pathname
)
375 struct lttcomm_relayd_add_stream msg
;
377 memset(&msg
, 0, sizeof(msg
));
378 if (lttng_strncpy(msg
.channel_name
, channel_name
,
379 sizeof(msg
.channel_name
))) {
384 if (lttng_strncpy(msg
.pathname
, pathname
,
385 sizeof(msg
.pathname
))) {
391 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
401 static int relayd_add_stream_2_2(struct lttcomm_relayd_sock
*rsock
,
402 const char *channel_name
, const char *pathname
,
403 uint64_t tracefile_size
, uint64_t tracefile_count
)
406 struct lttcomm_relayd_add_stream_2_2 msg
;
408 memset(&msg
, 0, sizeof(msg
));
409 /* Compat with relayd 2.2 to 2.10 */
410 if (lttng_strncpy(msg
.channel_name
, channel_name
,
411 sizeof(msg
.channel_name
))) {
415 if (lttng_strncpy(msg
.pathname
, pathname
,
416 sizeof(msg
.pathname
))) {
420 msg
.tracefile_size
= htobe64(tracefile_size
);
421 msg
.tracefile_count
= htobe64(tracefile_count
);
424 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
433 static int relayd_add_stream_2_11(struct lttcomm_relayd_sock
*rsock
,
434 const char *channel_name
, const char *pathname
,
435 uint64_t tracefile_size
, uint64_t tracefile_count
,
436 uint64_t trace_archive_id
)
439 struct lttcomm_relayd_add_stream_2_11
*msg
= NULL
;
440 size_t channel_name_len
;
444 /* The two names are sent with a '\0' delimiter between them. */
445 channel_name_len
= strlen(channel_name
) + 1;
446 pathname_len
= strlen(pathname
) + 1;
448 msg_length
= sizeof(*msg
) + channel_name_len
+ pathname_len
;
449 msg
= zmalloc
<lttcomm_relayd_add_stream_2_11
>(msg_length
);
451 PERROR("zmalloc add_stream_2_11 command message");
456 LTTNG_ASSERT(channel_name_len
<= UINT32_MAX
);
457 msg
->channel_name_len
= htobe32(channel_name_len
);
459 LTTNG_ASSERT(pathname_len
<= UINT32_MAX
);
460 msg
->pathname_len
= htobe32(pathname_len
);
462 if (lttng_strncpy(msg
->names
, channel_name
, channel_name_len
)) {
466 if (lttng_strncpy(msg
->names
+ channel_name_len
, pathname
, pathname_len
)) {
471 msg
->tracefile_size
= htobe64(tracefile_size
);
472 msg
->tracefile_count
= htobe64(tracefile_count
);
473 msg
->trace_chunk_id
= htobe64(trace_archive_id
);
476 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) msg
, msg_length
, 0);
487 * Add stream on the relayd and assign stream handle to the stream_id argument.
489 * Chunks are not supported by relayd prior to 2.11, but are used to
490 * internally between session daemon and consumer daemon to keep track
491 * of the channel and stream output path.
493 * On success return 0 else return ret_code negative value.
495 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
496 const char *domain_name
, const char *_pathname
, uint64_t *stream_id
,
497 uint64_t tracefile_size
, uint64_t tracefile_count
,
498 struct lttng_trace_chunk
*trace_chunk
)
501 struct lttcomm_relayd_status_stream reply
;
502 char pathname
[RELAYD_COMM_LTTNG_PATH_MAX
];
504 /* Code flow error. Safety net. */
506 LTTNG_ASSERT(channel_name
);
507 LTTNG_ASSERT(domain_name
);
508 LTTNG_ASSERT(_pathname
);
509 LTTNG_ASSERT(trace_chunk
);
511 DBG("Relayd adding stream for channel name %s", channel_name
);
513 /* Compat with relayd 2.1 */
514 if (rsock
->minor
== 1) {
516 ret
= relayd_add_stream_2_1(rsock
, channel_name
, _pathname
);
518 } else if (rsock
->minor
> 1 && rsock
->minor
< 11) {
519 /* From 2.2 to 2.10 */
520 ret
= relayd_add_stream_2_2(rsock
, channel_name
, _pathname
,
521 tracefile_size
, tracefile_count
);
523 const char *separator
;
524 enum lttng_trace_chunk_status chunk_status
;
527 if (_pathname
[0] == '\0') {
533 ret
= snprintf(pathname
, RELAYD_COMM_LTTNG_PATH_MAX
, "%s%s%s",
534 domain_name
, separator
, _pathname
);
535 if (ret
<= 0 || ret
>= RELAYD_COMM_LTTNG_PATH_MAX
) {
536 ERR("Failed to format stream path: %s",
537 ret
<= 0 ? "formatting error" :
538 "path exceeds maximal allowed length");
543 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
,
545 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
547 /* From 2.11 to ...*/
548 ret
= relayd_add_stream_2_11(rsock
, channel_name
, pathname
,
549 tracefile_size
, tracefile_count
,
558 /* Waiting for reply */
559 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
564 /* Back to host bytes order. */
565 reply
.handle
= be64toh(reply
.handle
);
566 reply
.ret_code
= be32toh(reply
.ret_code
);
568 /* Return session id or negative ret code. */
569 if (reply
.ret_code
!= LTTNG_OK
) {
571 ERR("Relayd add stream replied error %d", reply
.ret_code
);
575 *stream_id
= reply
.handle
;
578 DBG("Relayd stream added successfully with handle %" PRIu64
,
586 * Inform the relay that all the streams for the current channel has been sent.
588 * On success return 0 else return ret_code negative value.
590 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
593 struct lttcomm_relayd_generic_reply reply
;
595 /* Code flow error. Safety net. */
598 DBG("Relayd sending streams sent.");
600 /* This feature was introduced in 2.4, ignore it for earlier versions. */
601 if (rsock
->minor
< 4) {
607 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, NULL
, 0, 0);
612 /* Waiting for reply */
613 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
618 /* Back to host bytes order. */
619 reply
.ret_code
= be32toh(reply
.ret_code
);
621 /* Return session id or negative ret code. */
622 if (reply
.ret_code
!= LTTNG_OK
) {
624 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
631 DBG("Relayd streams sent success");
639 * Check version numbers on the relayd.
640 * If major versions are compatible, we assign minor_to_use to the
641 * minor version of the procotol we are going to use for this session.
643 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
644 * otherwise, or a negative value on network errors.
646 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
649 struct lttcomm_relayd_version msg
;
651 /* Code flow error. Safety net. */
654 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
657 memset(&msg
, 0, sizeof(msg
));
658 /* Prepare network byte order before transmission. */
659 msg
.major
= htobe32(rsock
->major
);
660 msg
.minor
= htobe32(rsock
->minor
);
663 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
668 /* Receive response */
669 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
674 /* Set back to host bytes order */
675 msg
.major
= be32toh(msg
.major
);
676 msg
.minor
= be32toh(msg
.minor
);
679 * Only validate the major version. If the other side is higher,
680 * communication is not possible. Only major version equal can talk to each
681 * other. If the minor version differs, the lowest version is used by both
684 if (msg
.major
!= rsock
->major
) {
686 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
687 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
688 msg
.major
, rsock
->major
);
693 * If the relayd's minor version is higher, it will adapt to our version so
694 * we can continue to use the latest relayd communication data structure.
695 * If the received minor version is higher, the relayd should adapt to us.
697 if (rsock
->minor
> msg
.minor
) {
698 rsock
->minor
= msg
.minor
;
701 /* Version number compatible */
702 DBG2("Relayd version is compatible, using protocol version %u.%u",
703 rsock
->major
, rsock
->minor
);
711 * Add stream on the relayd and assign stream handle to the stream_id argument.
713 * On success return 0 else return ret_code negative value.
715 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
719 /* Code flow error. Safety net. */
722 DBG("Relayd sending metadata of size %zu", len
);
725 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
730 DBG2("Relayd metadata added successfully");
733 * After that call, the metadata data MUST be sent to the relayd so the
734 * receive size on the other end matches the len of the metadata packet
735 * header. This is why we don't wait for a reply here.
743 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
745 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
747 /* Code flow error. Safety net. */
750 if (!rsock
->sock
.ops
) {
752 * Attempting a connect on a non-initialized socket.
757 DBG3("Relayd connect ...");
759 return rsock
->sock
.ops
->connect(&rsock
->sock
);
763 * Close relayd socket with an allocated lttcomm_relayd_sock.
765 * If no socket operations are found, simply return 0 meaning that everything
766 * is fine. Without operations, the socket can not possibly be opened or used.
767 * This is possible if the socket was allocated but not created. However, the
768 * caller could simply use it to store a valid file descriptor for instance
769 * passed over a Unix socket and call this to cleanup but still without a valid
772 * Return the close returned value. On error, a negative value is usually
773 * returned back from close(2).
775 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
779 /* Code flow error. Safety net. */
782 /* An invalid fd is fine, return success. */
783 if (rsock
->sock
.fd
< 0) {
788 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
790 if (rsock
->sock
.ops
) {
791 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
793 /* Default call if no specific ops found. */
794 ret
= close(rsock
->sock
.fd
);
796 PERROR("relayd_close default close");
806 * Send data header structure to the relayd.
808 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
809 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
813 /* Code flow error. Safety net. */
817 if (rsock
->sock
.fd
< 0) {
821 DBG3("Relayd sending data header of size %zu", size
);
823 /* Again, safety net */
825 size
= sizeof(struct lttcomm_relayd_data_hdr
);
828 /* Only send data header. */
829 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
836 * The data MUST be sent right after that command for the receive on the
837 * other end to match the size in the header.
845 * Send close stream command to the relayd.
847 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
848 uint64_t last_net_seq_num
)
851 struct lttcomm_relayd_close_stream msg
;
852 struct lttcomm_relayd_generic_reply reply
;
854 /* Code flow error. Safety net. */
857 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
859 memset(&msg
, 0, sizeof(msg
));
860 msg
.stream_id
= htobe64(stream_id
);
861 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
864 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
869 /* Receive response */
870 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
875 reply
.ret_code
= be32toh(reply
.ret_code
);
877 /* Return session id or negative ret code. */
878 if (reply
.ret_code
!= LTTNG_OK
) {
880 ERR("Relayd close stream replied error %d", reply
.ret_code
);
886 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
893 * Check for data availability for a given stream id.
895 * Return 0 if NOT pending, 1 if so and a negative value on error.
897 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
898 uint64_t last_net_seq_num
)
901 struct lttcomm_relayd_data_pending msg
;
902 struct lttcomm_relayd_generic_reply reply
;
904 /* Code flow error. Safety net. */
907 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
909 memset(&msg
, 0, sizeof(msg
));
910 msg
.stream_id
= htobe64(stream_id
);
911 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
914 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
920 /* Receive response */
921 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
926 reply
.ret_code
= be32toh(reply
.ret_code
);
928 /* Return session id or negative ret code. */
929 if (reply
.ret_code
>= LTTNG_OK
) {
930 ERR("Relayd data pending replied error %d", reply
.ret_code
);
933 /* At this point, the ret code is either 1 or 0 */
934 ret
= reply
.ret_code
;
936 DBG("Relayd data is %s pending for stream id %" PRIu64
,
937 ret
== 1 ? "" : "NOT", stream_id
);
944 * Check on the relayd side for a quiescent state on the control socket.
946 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
947 uint64_t metadata_stream_id
)
950 struct lttcomm_relayd_quiescent_control msg
;
951 struct lttcomm_relayd_generic_reply reply
;
953 /* Code flow error. Safety net. */
956 DBG("Relayd checking quiescent control state");
958 memset(&msg
, 0, sizeof(msg
));
959 msg
.stream_id
= htobe64(metadata_stream_id
);
962 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
967 /* Receive response */
968 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
973 reply
.ret_code
= be32toh(reply
.ret_code
);
975 /* Return session id or negative ret code. */
976 if (reply
.ret_code
!= LTTNG_OK
) {
978 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
982 /* Control socket is quiescent */
990 * Begin a data pending command for a specific session id.
992 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
995 struct lttcomm_relayd_begin_data_pending msg
;
996 struct lttcomm_relayd_generic_reply reply
;
998 /* Code flow error. Safety net. */
1001 DBG("Relayd begin data pending");
1003 memset(&msg
, 0, sizeof(msg
));
1004 msg
.session_id
= htobe64(id
);
1007 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
1012 /* Receive response */
1013 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1018 reply
.ret_code
= be32toh(reply
.ret_code
);
1020 /* Return session id or negative ret code. */
1021 if (reply
.ret_code
!= LTTNG_OK
) {
1023 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
1034 * End a data pending command for a specific session id.
1036 * Return 0 on success and set is_data_inflight to 0 if no data is being
1037 * streamed or 1 if it is the case.
1039 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
1040 unsigned int *is_data_inflight
)
1043 struct lttcomm_relayd_end_data_pending msg
;
1044 struct lttcomm_relayd_generic_reply reply
;
1046 /* Code flow error. Safety net. */
1047 LTTNG_ASSERT(rsock
);
1049 DBG("Relayd end data pending");
1051 memset(&msg
, 0, sizeof(msg
));
1052 msg
.session_id
= htobe64(id
);
1055 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
1060 /* Receive response */
1061 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1066 recv_ret
= be32toh(reply
.ret_code
);
1072 *is_data_inflight
= recv_ret
;
1074 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
1083 * Send index to the relayd.
1085 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
1086 struct ctf_packet_index
*index
, uint64_t relay_stream_id
,
1087 uint64_t net_seq_num
)
1090 struct lttcomm_relayd_index msg
;
1091 struct lttcomm_relayd_generic_reply reply
;
1093 /* Code flow error. Safety net. */
1094 LTTNG_ASSERT(rsock
);
1096 if (rsock
->minor
< 4) {
1097 DBG("Not sending indexes before protocol 2.4");
1102 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
1104 memset(&msg
, 0, sizeof(msg
));
1105 msg
.relay_stream_id
= htobe64(relay_stream_id
);
1106 msg
.net_seq_num
= htobe64(net_seq_num
);
1108 /* The index is already in big endian. */
1109 msg
.packet_size
= index
->packet_size
;
1110 msg
.content_size
= index
->content_size
;
1111 msg
.timestamp_begin
= index
->timestamp_begin
;
1112 msg
.timestamp_end
= index
->timestamp_end
;
1113 msg
.events_discarded
= index
->events_discarded
;
1114 msg
.stream_id
= index
->stream_id
;
1116 if (rsock
->minor
>= 8) {
1117 msg
.stream_instance_id
= index
->stream_instance_id
;
1118 msg
.packet_seq_num
= index
->packet_seq_num
;
1122 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
,
1123 lttcomm_relayd_index_len(lttng_to_index_major(rsock
->major
,
1125 lttng_to_index_minor(rsock
->major
, rsock
->minor
)),
1131 /* Receive response */
1132 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1137 reply
.ret_code
= be32toh(reply
.ret_code
);
1139 /* Return session id or negative ret code. */
1140 if (reply
.ret_code
!= LTTNG_OK
) {
1142 ERR("Relayd send index replied error %d", reply
.ret_code
);
1153 * Ask the relay to reset the metadata trace file (regeneration).
1155 int relayd_reset_metadata(struct lttcomm_relayd_sock
*rsock
,
1156 uint64_t stream_id
, uint64_t version
)
1159 struct lttcomm_relayd_reset_metadata msg
;
1160 struct lttcomm_relayd_generic_reply reply
;
1162 /* Code flow error. Safety net. */
1163 LTTNG_ASSERT(rsock
);
1165 /* Should have been prevented by the sessiond. */
1166 if (rsock
->minor
< 8) {
1167 ERR("Metadata regeneration unsupported before 2.8");
1172 DBG("Relayd reset metadata stream id %" PRIu64
, stream_id
);
1174 memset(&msg
, 0, sizeof(msg
));
1175 msg
.stream_id
= htobe64(stream_id
);
1176 msg
.version
= htobe64(version
);
1179 ret
= send_command(rsock
, RELAYD_RESET_METADATA
, (void *) &msg
, sizeof(msg
), 0);
1184 /* Receive response */
1185 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1190 reply
.ret_code
= be32toh(reply
.ret_code
);
1192 /* Return session id or negative ret code. */
1193 if (reply
.ret_code
!= LTTNG_OK
) {
1195 ERR("Relayd reset metadata replied error %d", reply
.ret_code
);
1201 DBG("Relayd reset metadata stream id %" PRIu64
" successfully", stream_id
);
1207 int relayd_rotate_streams(struct lttcomm_relayd_sock
*sock
,
1208 unsigned int stream_count
, const uint64_t *new_chunk_id
,
1209 const struct relayd_stream_rotation_position
*positions
)
1213 struct lttng_dynamic_buffer payload
;
1214 struct lttcomm_relayd_generic_reply reply
= {};
1215 struct lttcomm_relayd_rotate_streams msg
;
1216 char new_chunk_id_buf
[MAX_INT_DEC_LEN(*new_chunk_id
)] = {};
1217 const char *new_chunk_id_str
;
1219 msg
.stream_count
= htobe32((uint32_t) stream_count
);
1220 msg
.new_chunk_id
= (typeof(msg
.new_chunk_id
)){
1221 .is_set
= !!new_chunk_id
,
1222 .value
= htobe64(new_chunk_id
? *new_chunk_id
: 0),
1225 if (!relayd_supports_chunks(sock
)) {
1226 DBG("Refusing to rotate remote streams: relayd does not support chunks");
1230 lttng_dynamic_buffer_init(&payload
);
1232 /* Code flow error. Safety net. */
1236 ret
= snprintf(new_chunk_id_buf
, sizeof(new_chunk_id_buf
),
1237 "%" PRIu64
, *new_chunk_id
);
1238 if (ret
== -1 || ret
>= sizeof(new_chunk_id_buf
)) {
1239 new_chunk_id_str
= "formatting error";
1241 new_chunk_id_str
= new_chunk_id_buf
;
1244 new_chunk_id_str
= "none";
1247 DBG("Preparing \"rotate streams\" command payload: new_chunk_id = %s, stream_count = %u",
1248 new_chunk_id_str
, stream_count
);
1250 ret
= lttng_dynamic_buffer_append(&payload
, &msg
, sizeof(msg
));
1252 ERR("Failed to allocate \"rotate streams\" command payload");
1256 for (i
= 0; i
< stream_count
; i
++) {
1257 const struct relayd_stream_rotation_position
*position
=
1259 const struct lttcomm_relayd_stream_rotation_position comm_position
= {
1260 .stream_id
= htobe64(position
->stream_id
),
1261 .rotate_at_seq_num
= htobe64(
1262 position
->rotate_at_seq_num
),
1265 DBG("Rotate stream %" PRIu64
" at sequence number %" PRIu64
,
1266 position
->stream_id
,
1267 position
->rotate_at_seq_num
);
1268 ret
= lttng_dynamic_buffer_append(&payload
, &comm_position
,
1269 sizeof(comm_position
));
1271 ERR("Failed to allocate \"rotate streams\" command payload");
1277 ret
= send_command(sock
, RELAYD_ROTATE_STREAMS
, payload
.data
,
1280 ERR("Failed to send \"rotate stream\" command");
1284 /* Receive response. */
1285 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1287 ERR("Failed to receive \"rotate streams\" command reply");
1291 reply
.ret_code
= be32toh(reply
.ret_code
);
1292 if (reply
.ret_code
!= LTTNG_OK
) {
1294 ERR("Relayd rotate streams replied error %d", reply
.ret_code
);
1298 DBG("Relayd rotated streams successfully");
1302 lttng_dynamic_buffer_reset(&payload
);
1306 int relayd_create_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1307 struct lttng_trace_chunk
*chunk
)
1310 enum lttng_trace_chunk_status status
;
1311 struct lttcomm_relayd_create_trace_chunk msg
= {};
1312 struct lttcomm_relayd_generic_reply reply
= {};
1313 struct lttng_dynamic_buffer payload
;
1315 time_t creation_timestamp
;
1316 const char *chunk_name
;
1317 size_t chunk_name_length
;
1318 bool overridden_name
;
1320 lttng_dynamic_buffer_init(&payload
);
1322 if (!relayd_supports_chunks(sock
)) {
1323 DBG("Refusing to create remote trace chunk: relayd does not support chunks");
1327 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1328 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1333 status
= lttng_trace_chunk_get_creation_timestamp(
1334 chunk
, &creation_timestamp
);
1335 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1340 status
= lttng_trace_chunk_get_name(
1341 chunk
, &chunk_name
, &overridden_name
);
1342 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1343 status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1348 chunk_name_length
= overridden_name
? (strlen(chunk_name
) + 1) : 0;
1349 msg
.chunk_id
= htobe64(chunk_id
);
1350 msg
.creation_timestamp
= htobe64((uint64_t) creation_timestamp
);
1351 msg
.override_name_length
= htobe32((uint32_t) chunk_name_length
);
1353 ret
= lttng_dynamic_buffer_append(&payload
, &msg
, sizeof(msg
));
1357 if (chunk_name_length
) {
1358 ret
= lttng_dynamic_buffer_append(
1359 &payload
, chunk_name
, chunk_name_length
);
1365 ret
= send_command(sock
, RELAYD_CREATE_TRACE_CHUNK
, payload
.data
,
1368 ERR("Failed to send trace chunk creation command to relay daemon");
1372 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1374 ERR("Failed to receive relay daemon trace chunk creation command reply");
1378 reply
.ret_code
= be32toh(reply
.ret_code
);
1379 if (reply
.ret_code
!= LTTNG_OK
) {
1381 ERR("Relayd trace chunk create replied error %d",
1385 DBG("Relayd successfully created trace chunk: chunk_id = %" PRIu64
,
1390 lttng_dynamic_buffer_reset(&payload
);
1394 int relayd_close_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1395 struct lttng_trace_chunk
*chunk
,
1399 enum lttng_trace_chunk_status status
;
1400 struct lttcomm_relayd_close_trace_chunk msg
= {};
1401 struct lttcomm_relayd_close_trace_chunk_reply reply
= {};
1403 time_t close_timestamp
;
1404 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
1406 if (!relayd_supports_chunks(sock
)) {
1407 DBG("Refusing to close remote trace chunk: relayd does not support chunks");
1411 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1412 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1413 ERR("Failed to get trace chunk id");
1418 status
= lttng_trace_chunk_get_close_timestamp(chunk
, &close_timestamp
);
1419 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1420 ERR("Failed to get trace chunk close timestamp");
1425 status
= lttng_trace_chunk_get_close_command(chunk
,
1426 &close_command
.value
);
1428 case LTTNG_TRACE_CHUNK_STATUS_OK
:
1429 close_command
.is_set
= 1;
1431 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
1434 ERR("Failed to get trace chunk close command");
1439 msg
= (typeof(msg
)){
1440 .chunk_id
= htobe64(chunk_id
),
1441 .close_timestamp
= htobe64((uint64_t) close_timestamp
),
1443 .is_set
= close_command
.is_set
,
1444 .value
= htobe32((uint32_t) close_command
.value
),
1448 ret
= send_command(sock
, RELAYD_CLOSE_TRACE_CHUNK
, &msg
, sizeof(msg
),
1451 ERR("Failed to send trace chunk close command to relay daemon");
1455 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1457 ERR("Failed to receive relay daemon trace chunk close command reply");
1461 reply
.path_length
= be32toh(reply
.path_length
);
1462 if (reply
.path_length
>= LTTNG_PATH_MAX
) {
1463 ERR("Chunk path too long");
1468 ret
= recv_reply(sock
, path
, reply
.path_length
);
1470 ERR("Failed to receive relay daemon trace chunk close command reply");
1473 if (path
[reply
.path_length
- 1] != '\0') {
1474 ERR("Invalid trace chunk path returned by relay daemon (not null-terminated)");
1479 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1480 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1482 ERR("Relayd trace chunk close replied error %d",
1483 reply
.generic
.ret_code
);
1486 DBG("Relayd successfully closed trace chunk: chunk_id = %" PRIu64
,
1493 int relayd_trace_chunk_exists(struct lttcomm_relayd_sock
*sock
,
1494 uint64_t chunk_id
, bool *chunk_exists
)
1497 struct lttcomm_relayd_trace_chunk_exists msg
= {};
1498 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
1500 if (!relayd_supports_chunks(sock
)) {
1501 DBG("Refusing to check for trace chunk existence: relayd does not support chunks");
1502 /* The chunk will never exist */
1503 *chunk_exists
= false;
1507 msg
= (typeof(msg
)){
1508 .chunk_id
= htobe64(chunk_id
),
1511 ret
= send_command(sock
, RELAYD_TRACE_CHUNK_EXISTS
, &msg
, sizeof(msg
),
1514 ERR("Failed to send trace chunk exists command to relay daemon");
1518 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1520 ERR("Failed to receive relay daemon trace chunk close command reply");
1524 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1525 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1527 ERR("Relayd trace chunk close replied error %d",
1528 reply
.generic
.ret_code
);
1531 DBG("Relayd successfully checked trace chunk existence: chunk_id = %" PRIu64
1532 ", exists = %s", chunk_id
,
1533 reply
.trace_chunk_exists
? "true" : "false");
1534 *chunk_exists
= !!reply
.trace_chunk_exists
;
1540 int relayd_get_configuration(struct lttcomm_relayd_sock
*sock
,
1541 uint64_t query_flags
,
1542 uint64_t *result_flags
)
1545 struct lttcomm_relayd_get_configuration msg
= (typeof(msg
)) {
1546 .query_flags
= htobe64(query_flags
),
1548 struct lttcomm_relayd_get_configuration_reply reply
= {};
1550 if (!relayd_supports_get_configuration(sock
)) {
1551 DBG("Refusing to get relayd configuration (unsupported by relayd)");
1560 ret
= send_command(sock
, RELAYD_GET_CONFIGURATION
, &msg
, sizeof(msg
),
1563 ERR("Failed to send get configuration command to relay daemon");
1567 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1569 ERR("Failed to receive relay daemon get configuration command reply");
1573 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1574 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1576 ERR("Relayd get configuration replied error %d",
1577 reply
.generic
.ret_code
);
1579 reply
.relayd_configuration_flags
=
1580 be64toh(reply
.relayd_configuration_flags
);
1582 DBG("Relayd successfully got configuration: query_flags = %" PRIu64
1583 ", results_flags = %" PRIu64
, query_flags
,
1584 reply
.relayd_configuration_flags
);
1585 *result_flags
= reply
.relayd_configuration_flags
;