2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
16 #include <common/common.hpp>
17 #include <common/compat/endian.hpp>
18 #include <common/compat/string.hpp>
19 #include <common/defaults.hpp>
20 #include <common/index/ctf-index.hpp>
21 #include <common/sessiond-comm/relayd.hpp>
22 #include <common/string-utils/format.hpp>
23 #include <common/trace-chunk.hpp>
28 bool relayd_supports_chunks(const struct lttcomm_relayd_sock
*sock
)
30 if (sock
->major
> 2) {
32 } else if (sock
->major
== 2 && sock
->minor
>= 11) {
39 bool relayd_supports_get_configuration(const struct lttcomm_relayd_sock
*sock
)
41 if (sock
->major
> 2) {
43 } else if (sock
->major
== 2 && sock
->minor
>= 12) {
49 static bool relayd_supports_get_configuration_trace_format(const struct lttcomm_relayd_sock
*sock
)
51 if (sock
->major
> 2) {
53 } else if (sock
->major
== 2 && sock
->minor
>= 15) {
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
= calloc
<char>(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 %s of size %" PRIu64
,
101 lttcomm_relayd_command_str(cmd
), buf_size
);
102 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
104 PERROR("Failed to send command %s of size %" PRIu64
,
105 lttcomm_relayd_command_str(cmd
), buf_size
);
116 * Receive reply data on socket. This MUST be call after send_command or else
117 * could result in unexpected behavior(s).
119 static int recv_reply(struct lttcomm_relayd_sock
*rsock
, void *data
, size_t size
)
123 if (rsock
->sock
.fd
< 0) {
127 DBG3("Relayd waiting for reply of size %zu", size
);
129 ret
= rsock
->sock
.ops
->recvmsg(&rsock
->sock
, data
, size
, 0);
130 if (ret
<= 0 || ret
!= size
) {
132 /* Orderly shutdown. */
133 DBG("Socket %d has performed an orderly shutdown", rsock
->sock
.fd
);
135 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
136 rsock
->sock
.fd
, size
, ret
);
138 /* Always return -1 here and the caller can use errno. */
148 * Starting from 2.11, RELAYD_CREATE_SESSION payload (session_name,
149 * hostname, and base_path) have no length restriction on the sender side.
150 * Length for both payloads is stored in the msg struct. A new dynamic size
151 * payload size is introduced.
153 static int relayd_create_session_2_11(struct lttcomm_relayd_sock
*rsock
,
154 const char *session_name
, const char *hostname
,
155 const char *base_path
, int session_live_timer
,
156 unsigned int snapshot
, uint64_t sessiond_session_id
,
157 const lttng_uuid
& sessiond_uuid
, const uint64_t *current_chunk_id
,
158 time_t creation_time
, bool session_name_contains_creation_time
,
159 struct lttcomm_relayd_create_session_reply_2_11
*reply
,
163 struct lttcomm_relayd_create_session_2_11
*msg
= NULL
;
164 size_t session_name_len
;
166 size_t base_path_len
;
173 /* The three names are sent with a '\0' delimiter between them. */
174 session_name_len
= strlen(session_name
) + 1;
175 hostname_len
= strlen(hostname
) + 1;
176 base_path_len
= strlen(base_path
) + 1;
178 msg_length
= sizeof(*msg
) + session_name_len
+ hostname_len
+ base_path_len
;
179 msg
= zmalloc
<lttcomm_relayd_create_session_2_11
>(msg_length
);
181 PERROR("zmalloc create_session_2_11 command message");
186 LTTNG_ASSERT(session_name_len
<= UINT32_MAX
);
187 msg
->session_name_len
= htobe32(session_name_len
);
189 LTTNG_ASSERT(hostname_len
<= UINT32_MAX
);
190 msg
->hostname_len
= htobe32(hostname_len
);
192 LTTNG_ASSERT(base_path_len
<= UINT32_MAX
);
193 msg
->base_path_len
= htobe32(base_path_len
);
196 if (lttng_strncpy(dst
, session_name
, session_name_len
)) {
200 dst
+= session_name_len
;
201 if (lttng_strncpy(dst
, hostname
, hostname_len
)) {
206 if (lttng_strncpy(dst
, base_path
, base_path_len
)) {
211 msg
->live_timer
= htobe32(session_live_timer
);
212 msg
->snapshot
= !!snapshot
;
214 std::copy(sessiond_uuid
.begin(), sessiond_uuid
.end(), msg
->sessiond_uuid
);
215 msg
->session_id
= htobe64(sessiond_session_id
);
216 msg
->session_name_contains_creation_time
= session_name_contains_creation_time
;
217 if (current_chunk_id
) {
218 LTTNG_OPTIONAL_SET(&msg
->current_chunk_id
,
219 htobe64(*current_chunk_id
));
222 msg
->creation_time
= htobe64((uint64_t) creation_time
);
225 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, msg
, msg_length
, 0);
229 /* Receive response */
230 ret
= recv_reply(rsock
, reply
, sizeof(*reply
));
234 reply
->generic
.session_id
= be64toh(reply
->generic
.session_id
);
235 reply
->generic
.ret_code
= be32toh(reply
->generic
.ret_code
);
236 reply
->output_path_length
= be32toh(reply
->output_path_length
);
237 if (reply
->output_path_length
>= LTTNG_PATH_MAX
) {
238 ERR("Invalid session output path length in reply (%" PRIu32
" bytes) exceeds maximal allowed length (%d bytes)",
239 reply
->output_path_length
, LTTNG_PATH_MAX
);
243 ret
= recv_reply(rsock
, output_path
, reply
->output_path_length
);
252 * From 2.4 to 2.10, RELAYD_CREATE_SESSION takes additional parameters to
253 * support the live reading capability.
255 static int relayd_create_session_2_4(struct lttcomm_relayd_sock
*rsock
,
256 const char *session_name
, const char *hostname
,
257 int session_live_timer
, unsigned int snapshot
,
258 struct lttcomm_relayd_status_session
*reply
)
261 struct lttcomm_relayd_create_session_2_4 msg
;
263 if (lttng_strncpy(msg
.session_name
, session_name
,
264 sizeof(msg
.session_name
))) {
268 if (lttng_strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
))) {
272 msg
.live_timer
= htobe32(session_live_timer
);
273 msg
.snapshot
= htobe32(snapshot
);
276 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, &msg
, sizeof(msg
), 0);
281 /* Receive response */
282 ret
= recv_reply(rsock
, reply
, sizeof(*reply
));
286 reply
->session_id
= be64toh(reply
->session_id
);
287 reply
->ret_code
= be32toh(reply
->ret_code
);
293 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
295 static int relayd_create_session_2_1(struct lttcomm_relayd_sock
*rsock
,
296 struct lttcomm_relayd_status_session
*reply
)
301 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
306 /* Receive response */
307 ret
= recv_reply(rsock
, reply
, sizeof(*reply
));
311 reply
->session_id
= be64toh(reply
->session_id
);
312 reply
->ret_code
= be32toh(reply
->ret_code
);
318 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
319 * set session_id of the relayd if we have a successful reply from the relayd.
321 * On success, return 0 else a negative value which is either an errno error or
322 * a lttng error code from the relayd.
324 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
,
325 uint64_t *relayd_session_id
,
326 const char *session_name
, const char *hostname
,
327 const char *base_path
, int session_live_timer
,
328 unsigned int snapshot
, uint64_t sessiond_session_id
,
329 const lttng_uuid
& sessiond_uuid
,
330 const uint64_t *current_chunk_id
,
331 time_t creation_time
, bool session_name_contains_creation_time
,
335 struct lttcomm_relayd_create_session_reply_2_11 reply
= {};
338 LTTNG_ASSERT(relayd_session_id
);
340 DBG("Relayd create session");
342 if (rsock
->minor
< 4) {
343 /* From 2.1 to 2.3 */
344 ret
= relayd_create_session_2_1(rsock
, &reply
.generic
);
345 } else if (rsock
->minor
>= 4 && rsock
->minor
< 11) {
346 /* From 2.4 to 2.10 */
347 ret
= relayd_create_session_2_4(rsock
, session_name
,
348 hostname
, session_live_timer
, snapshot
,
351 /* From 2.11 to ... */
352 ret
= relayd_create_session_2_11(rsock
, session_name
,
353 hostname
, base_path
, session_live_timer
, snapshot
,
354 sessiond_session_id
, sessiond_uuid
,
355 current_chunk_id
, creation_time
,
356 session_name_contains_creation_time
,
357 &reply
, output_path
);
364 /* Return session id or negative ret code. */
365 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
367 ERR("Relayd create session replied error %d",
368 reply
.generic
.ret_code
);
372 *relayd_session_id
= reply
.generic
.session_id
;
375 DBG("Relayd session created with id %" PRIu64
, reply
.generic
.session_id
);
381 static int relayd_add_stream_2_1(struct lttcomm_relayd_sock
*rsock
,
382 const char *channel_name
, const char *pathname
)
385 struct lttcomm_relayd_add_stream msg
;
387 memset(&msg
, 0, sizeof(msg
));
388 if (lttng_strncpy(msg
.channel_name
, channel_name
,
389 sizeof(msg
.channel_name
))) {
394 if (lttng_strncpy(msg
.pathname
, pathname
,
395 sizeof(msg
.pathname
))) {
401 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
411 static int relayd_add_stream_2_2(struct lttcomm_relayd_sock
*rsock
,
412 const char *channel_name
, const char *pathname
,
413 uint64_t tracefile_size
, uint64_t tracefile_count
)
416 struct lttcomm_relayd_add_stream_2_2 msg
;
418 memset(&msg
, 0, sizeof(msg
));
419 /* Compat with relayd 2.2 to 2.10 */
420 if (lttng_strncpy(msg
.channel_name
, channel_name
,
421 sizeof(msg
.channel_name
))) {
425 if (lttng_strncpy(msg
.pathname
, pathname
,
426 sizeof(msg
.pathname
))) {
430 msg
.tracefile_size
= htobe64(tracefile_size
);
431 msg
.tracefile_count
= htobe64(tracefile_count
);
434 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
443 static int relayd_add_stream_2_11(struct lttcomm_relayd_sock
*rsock
,
444 const char *channel_name
, const char *pathname
,
445 uint64_t tracefile_size
, uint64_t tracefile_count
,
446 uint64_t trace_archive_id
)
449 struct lttcomm_relayd_add_stream_2_11
*msg
= NULL
;
450 size_t channel_name_len
;
454 /* The two names are sent with a '\0' delimiter between them. */
455 channel_name_len
= strlen(channel_name
) + 1;
456 pathname_len
= strlen(pathname
) + 1;
458 msg_length
= sizeof(*msg
) + channel_name_len
+ pathname_len
;
459 msg
= zmalloc
<lttcomm_relayd_add_stream_2_11
>(msg_length
);
461 PERROR("zmalloc add_stream_2_11 command message");
466 LTTNG_ASSERT(channel_name_len
<= UINT32_MAX
);
467 msg
->channel_name_len
= htobe32(channel_name_len
);
469 LTTNG_ASSERT(pathname_len
<= UINT32_MAX
);
470 msg
->pathname_len
= htobe32(pathname_len
);
472 if (lttng_strncpy(msg
->names
, channel_name
, channel_name_len
)) {
476 if (lttng_strncpy(msg
->names
+ channel_name_len
, pathname
, pathname_len
)) {
481 msg
->tracefile_size
= htobe64(tracefile_size
);
482 msg
->tracefile_count
= htobe64(tracefile_count
);
483 msg
->trace_chunk_id
= htobe64(trace_archive_id
);
486 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) msg
, msg_length
, 0);
497 * Add stream on the relayd and assign stream handle to the stream_id argument.
499 * Chunks are not supported by relayd prior to 2.11, but are used to
500 * internally between session daemon and consumer daemon to keep track
501 * of the channel and stream output path.
503 * On success return 0 else return ret_code negative value.
505 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
506 const char *domain_name
, const char *_pathname
, uint64_t *stream_id
,
507 uint64_t tracefile_size
, uint64_t tracefile_count
,
508 struct lttng_trace_chunk
*trace_chunk
)
511 struct lttcomm_relayd_status_stream reply
;
512 char pathname
[RELAYD_COMM_LTTNG_PATH_MAX
];
514 /* Code flow error. Safety net. */
516 LTTNG_ASSERT(channel_name
);
517 LTTNG_ASSERT(domain_name
);
518 LTTNG_ASSERT(_pathname
);
519 LTTNG_ASSERT(trace_chunk
);
521 DBG("Relayd adding stream for channel name %s", channel_name
);
523 /* Compat with relayd 2.1 */
524 if (rsock
->minor
== 1) {
526 ret
= relayd_add_stream_2_1(rsock
, channel_name
, _pathname
);
528 } else if (rsock
->minor
> 1 && rsock
->minor
< 11) {
529 /* From 2.2 to 2.10 */
530 ret
= relayd_add_stream_2_2(rsock
, channel_name
, _pathname
,
531 tracefile_size
, tracefile_count
);
533 const char *separator
;
534 enum lttng_trace_chunk_status chunk_status
;
537 if (_pathname
[0] == '\0') {
543 ret
= snprintf(pathname
, RELAYD_COMM_LTTNG_PATH_MAX
, "%s%s%s",
544 domain_name
, separator
, _pathname
);
545 if (ret
<= 0 || ret
>= RELAYD_COMM_LTTNG_PATH_MAX
) {
546 ERR("Failed to format stream path: %s",
547 ret
<= 0 ? "formatting error" :
548 "path exceeds maximal allowed length");
553 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
,
555 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
557 /* From 2.11 to ...*/
558 ret
= relayd_add_stream_2_11(rsock
, channel_name
, pathname
,
559 tracefile_size
, tracefile_count
,
568 /* Waiting for reply */
569 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
574 /* Back to host bytes order. */
575 reply
.handle
= be64toh(reply
.handle
);
576 reply
.ret_code
= be32toh(reply
.ret_code
);
578 /* Return session id or negative ret code. */
579 if (reply
.ret_code
!= LTTNG_OK
) {
581 ERR("Relayd add stream replied error %d", reply
.ret_code
);
585 *stream_id
= reply
.handle
;
588 DBG("Relayd stream added successfully with handle %" PRIu64
,
596 * Inform the relay that all the streams for the current channel has been sent.
598 * On success return 0 else return ret_code negative value.
600 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
603 struct lttcomm_relayd_generic_reply reply
;
605 /* Code flow error. Safety net. */
608 DBG("Relayd sending streams sent.");
610 /* This feature was introduced in 2.4, ignore it for earlier versions. */
611 if (rsock
->minor
< 4) {
617 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, NULL
, 0, 0);
622 /* Waiting for reply */
623 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
628 /* Back to host bytes order. */
629 reply
.ret_code
= be32toh(reply
.ret_code
);
631 /* Return session id or negative ret code. */
632 if (reply
.ret_code
!= LTTNG_OK
) {
634 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
641 DBG("Relayd streams sent success");
649 * Check version numbers on the relayd.
650 * If major versions are compatible, we assign minor_to_use to the
651 * minor version of the procotol we are going to use for this session.
653 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
654 * otherwise, or a negative value on network errors.
656 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
659 struct lttcomm_relayd_version msg
;
661 /* Code flow error. Safety net. */
664 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
667 memset(&msg
, 0, sizeof(msg
));
668 /* Prepare network byte order before transmission. */
669 msg
.major
= htobe32(rsock
->major
);
670 msg
.minor
= htobe32(rsock
->minor
);
673 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
678 /* Receive response */
679 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
684 /* Set back to host bytes order */
685 msg
.major
= be32toh(msg
.major
);
686 msg
.minor
= be32toh(msg
.minor
);
689 * Only validate the major version. If the other side is higher,
690 * communication is not possible. Only major version equal can talk to each
691 * other. If the minor version differs, the lowest version is used by both
694 if (msg
.major
!= rsock
->major
) {
696 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
697 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
698 msg
.major
, rsock
->major
);
703 * If the relayd's minor version is higher, it will adapt to our version so
704 * we can continue to use the latest relayd communication data structure.
705 * If the received minor version is higher, the relayd should adapt to us.
707 if (rsock
->minor
> msg
.minor
) {
708 rsock
->minor
= msg
.minor
;
711 /* Version number compatible */
712 DBG2("Relayd version is compatible, using protocol version %u.%u",
713 rsock
->major
, rsock
->minor
);
721 * Add stream on the relayd and assign stream handle to the stream_id argument.
723 * On success return 0 else return ret_code negative value.
725 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
729 /* Code flow error. Safety net. */
732 DBG("Relayd sending metadata of size %zu", len
);
735 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
740 DBG2("Relayd metadata added successfully");
743 * After that call, the metadata data MUST be sent to the relayd so the
744 * receive size on the other end matches the len of the metadata packet
745 * header. This is why we don't wait for a reply here.
753 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
755 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
757 /* Code flow error. Safety net. */
760 if (!rsock
->sock
.ops
) {
762 * Attempting a connect on a non-initialized socket.
767 DBG3("Relayd connect ...");
769 return rsock
->sock
.ops
->connect(&rsock
->sock
);
773 * Close relayd socket with an allocated lttcomm_relayd_sock.
775 * If no socket operations are found, simply return 0 meaning that everything
776 * is fine. Without operations, the socket can not possibly be opened or used.
777 * This is possible if the socket was allocated but not created. However, the
778 * caller could simply use it to store a valid file descriptor for instance
779 * passed over a Unix socket and call this to cleanup but still without a valid
782 * Return the close returned value. On error, a negative value is usually
783 * returned back from close(2).
785 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
789 /* Code flow error. Safety net. */
792 /* An invalid fd is fine, return success. */
793 if (rsock
->sock
.fd
< 0) {
798 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
800 if (rsock
->sock
.ops
) {
801 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
803 /* Default call if no specific ops found. */
804 ret
= close(rsock
->sock
.fd
);
806 PERROR("relayd_close default close");
816 * Send data header structure to the relayd.
818 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
819 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
823 /* Code flow error. Safety net. */
827 if (rsock
->sock
.fd
< 0) {
831 DBG3("Relayd sending data header of size %zu", size
);
833 /* Again, safety net */
835 size
= sizeof(struct lttcomm_relayd_data_hdr
);
838 /* Only send data header. */
839 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
846 * The data MUST be sent right after that command for the receive on the
847 * other end to match the size in the header.
855 * Send close stream command to the relayd.
857 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
858 uint64_t last_net_seq_num
)
861 struct lttcomm_relayd_close_stream msg
;
862 struct lttcomm_relayd_generic_reply reply
;
864 /* Code flow error. Safety net. */
867 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
869 memset(&msg
, 0, sizeof(msg
));
870 msg
.stream_id
= htobe64(stream_id
);
871 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
874 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
879 /* Receive response */
880 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
885 reply
.ret_code
= be32toh(reply
.ret_code
);
887 /* Return session id or negative ret code. */
888 if (reply
.ret_code
!= LTTNG_OK
) {
890 ERR("Relayd close stream replied error %d", reply
.ret_code
);
896 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
903 * Check for data availability for a given stream id.
905 * Return 0 if NOT pending, 1 if so and a negative value on error.
907 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
908 uint64_t last_net_seq_num
)
911 struct lttcomm_relayd_data_pending msg
;
912 struct lttcomm_relayd_generic_reply reply
;
914 /* Code flow error. Safety net. */
917 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
919 memset(&msg
, 0, sizeof(msg
));
920 msg
.stream_id
= htobe64(stream_id
);
921 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
924 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
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
) {
940 ERR("Relayd data pending replied error %d", reply
.ret_code
);
943 /* At this point, the ret code is either 1 or 0 */
944 ret
= reply
.ret_code
;
946 DBG("Relayd data is %s pending for stream id %" PRIu64
,
947 ret
== 1 ? "" : "NOT", stream_id
);
954 * Check on the relayd side for a quiescent state on the control socket.
956 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
957 uint64_t metadata_stream_id
)
960 struct lttcomm_relayd_quiescent_control msg
;
961 struct lttcomm_relayd_generic_reply reply
;
963 /* Code flow error. Safety net. */
966 DBG("Relayd checking quiescent control state");
968 memset(&msg
, 0, sizeof(msg
));
969 msg
.stream_id
= htobe64(metadata_stream_id
);
972 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
977 /* Receive response */
978 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
983 reply
.ret_code
= be32toh(reply
.ret_code
);
985 /* Return session id or negative ret code. */
986 if (reply
.ret_code
!= LTTNG_OK
) {
988 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
992 /* Control socket is quiescent */
1000 * Begin a data pending command for a specific session id.
1002 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
1005 struct lttcomm_relayd_begin_data_pending msg
;
1006 struct lttcomm_relayd_generic_reply reply
;
1008 /* Code flow error. Safety net. */
1009 LTTNG_ASSERT(rsock
);
1011 DBG("Relayd begin data pending");
1013 memset(&msg
, 0, sizeof(msg
));
1014 msg
.session_id
= htobe64(id
);
1017 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
1022 /* Receive response */
1023 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1028 reply
.ret_code
= be32toh(reply
.ret_code
);
1030 /* Return session id or negative ret code. */
1031 if (reply
.ret_code
!= LTTNG_OK
) {
1033 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
1044 * End a data pending command for a specific session id.
1046 * Return 0 on success and set is_data_inflight to 0 if no data is being
1047 * streamed or 1 if it is the case.
1049 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
1050 unsigned int *is_data_inflight
)
1053 struct lttcomm_relayd_end_data_pending msg
;
1054 struct lttcomm_relayd_generic_reply reply
;
1056 /* Code flow error. Safety net. */
1057 LTTNG_ASSERT(rsock
);
1059 DBG("Relayd end data pending");
1061 memset(&msg
, 0, sizeof(msg
));
1062 msg
.session_id
= htobe64(id
);
1065 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
1070 /* Receive response */
1071 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1076 recv_ret
= be32toh(reply
.ret_code
);
1082 *is_data_inflight
= recv_ret
;
1084 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
1093 * Send index to the relayd.
1095 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
1096 struct ctf_packet_index
*index
, uint64_t relay_stream_id
,
1097 uint64_t net_seq_num
)
1100 struct lttcomm_relayd_index msg
;
1101 struct lttcomm_relayd_generic_reply reply
;
1103 /* Code flow error. Safety net. */
1104 LTTNG_ASSERT(rsock
);
1106 if (rsock
->minor
< 4) {
1107 DBG("Not sending indexes before protocol 2.4");
1112 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
1114 memset(&msg
, 0, sizeof(msg
));
1115 msg
.relay_stream_id
= htobe64(relay_stream_id
);
1116 msg
.net_seq_num
= htobe64(net_seq_num
);
1118 /* The index is already in big endian. */
1119 msg
.packet_size
= index
->packet_size
;
1120 msg
.content_size
= index
->content_size
;
1121 msg
.timestamp_begin
= index
->timestamp_begin
;
1122 msg
.timestamp_end
= index
->timestamp_end
;
1123 msg
.events_discarded
= index
->events_discarded
;
1124 msg
.stream_id
= index
->stream_id
;
1126 if (rsock
->minor
>= 8) {
1127 msg
.stream_instance_id
= index
->stream_instance_id
;
1128 msg
.packet_seq_num
= index
->packet_seq_num
;
1132 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
,
1133 lttcomm_relayd_index_len(lttng_to_index_major(rsock
->major
,
1135 lttng_to_index_minor(rsock
->major
, rsock
->minor
)),
1141 /* Receive response */
1142 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1147 reply
.ret_code
= be32toh(reply
.ret_code
);
1149 /* Return session id or negative ret code. */
1150 if (reply
.ret_code
!= LTTNG_OK
) {
1152 ERR("Relayd send index replied error %d", reply
.ret_code
);
1163 * Ask the relay to reset the metadata trace file (regeneration).
1165 int relayd_reset_metadata(struct lttcomm_relayd_sock
*rsock
,
1166 uint64_t stream_id
, uint64_t version
)
1169 struct lttcomm_relayd_reset_metadata msg
;
1170 struct lttcomm_relayd_generic_reply reply
;
1172 /* Code flow error. Safety net. */
1173 LTTNG_ASSERT(rsock
);
1175 /* Should have been prevented by the sessiond. */
1176 if (rsock
->minor
< 8) {
1177 ERR("Metadata regeneration unsupported before 2.8");
1182 DBG("Relayd reset metadata stream id %" PRIu64
, stream_id
);
1184 memset(&msg
, 0, sizeof(msg
));
1185 msg
.stream_id
= htobe64(stream_id
);
1186 msg
.version
= htobe64(version
);
1189 ret
= send_command(rsock
, RELAYD_RESET_METADATA
, (void *) &msg
, sizeof(msg
), 0);
1194 /* Receive response */
1195 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1200 reply
.ret_code
= be32toh(reply
.ret_code
);
1202 /* Return session id or negative ret code. */
1203 if (reply
.ret_code
!= LTTNG_OK
) {
1205 ERR("Relayd reset metadata replied error %d", reply
.ret_code
);
1211 DBG("Relayd reset metadata stream id %" PRIu64
" successfully", stream_id
);
1217 int relayd_rotate_streams(struct lttcomm_relayd_sock
*sock
,
1218 unsigned int stream_count
, const uint64_t *new_chunk_id
,
1219 const struct relayd_stream_rotation_position
*positions
)
1223 struct lttng_dynamic_buffer payload
;
1224 struct lttcomm_relayd_generic_reply reply
= {};
1225 struct lttcomm_relayd_rotate_streams msg
;
1226 char new_chunk_id_buf
[MAX_INT_DEC_LEN(*new_chunk_id
)] = {};
1227 const char *new_chunk_id_str
;
1229 msg
.stream_count
= htobe32((uint32_t) stream_count
);
1230 msg
.new_chunk_id
= (typeof(msg
.new_chunk_id
)){
1231 .is_set
= !!new_chunk_id
,
1232 .value
= htobe64(new_chunk_id
? *new_chunk_id
: 0),
1235 if (!relayd_supports_chunks(sock
)) {
1236 DBG("Refusing to rotate remote streams: relayd does not support chunks");
1240 lttng_dynamic_buffer_init(&payload
);
1242 /* Code flow error. Safety net. */
1246 ret
= snprintf(new_chunk_id_buf
, sizeof(new_chunk_id_buf
),
1247 "%" PRIu64
, *new_chunk_id
);
1248 if (ret
== -1 || ret
>= sizeof(new_chunk_id_buf
)) {
1249 new_chunk_id_str
= "formatting error";
1251 new_chunk_id_str
= new_chunk_id_buf
;
1254 new_chunk_id_str
= "none";
1257 DBG("Preparing \"rotate streams\" command payload: new_chunk_id = %s, stream_count = %u",
1258 new_chunk_id_str
, stream_count
);
1260 ret
= lttng_dynamic_buffer_append(&payload
, &msg
, sizeof(msg
));
1262 ERR("Failed to allocate \"rotate streams\" command payload");
1266 for (i
= 0; i
< stream_count
; i
++) {
1267 const struct relayd_stream_rotation_position
*position
=
1269 const struct lttcomm_relayd_stream_rotation_position comm_position
= {
1270 .stream_id
= htobe64(position
->stream_id
),
1271 .rotate_at_seq_num
= htobe64(
1272 position
->rotate_at_seq_num
),
1275 DBG("Rotate stream %" PRIu64
" at sequence number %" PRIu64
,
1276 position
->stream_id
,
1277 position
->rotate_at_seq_num
);
1278 ret
= lttng_dynamic_buffer_append(&payload
, &comm_position
,
1279 sizeof(comm_position
));
1281 ERR("Failed to allocate \"rotate streams\" command payload");
1287 ret
= send_command(sock
, RELAYD_ROTATE_STREAMS
, payload
.data
,
1290 ERR("Failed to send \"rotate stream\" command");
1294 /* Receive response. */
1295 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1297 ERR("Failed to receive \"rotate streams\" command reply");
1301 reply
.ret_code
= be32toh(reply
.ret_code
);
1302 if (reply
.ret_code
!= LTTNG_OK
) {
1304 ERR("Relayd rotate streams replied error %d", reply
.ret_code
);
1308 DBG("Relayd rotated streams successfully");
1312 lttng_dynamic_buffer_reset(&payload
);
1316 int relayd_create_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1317 struct lttng_trace_chunk
*chunk
)
1320 enum lttng_trace_chunk_status status
;
1321 struct lttcomm_relayd_create_trace_chunk msg
= {};
1322 struct lttcomm_relayd_generic_reply reply
= {};
1323 struct lttng_dynamic_buffer payload
;
1325 time_t creation_timestamp
;
1326 const char *chunk_name
;
1327 size_t chunk_name_length
;
1328 bool overridden_name
;
1330 lttng_dynamic_buffer_init(&payload
);
1332 if (!relayd_supports_chunks(sock
)) {
1333 DBG("Refusing to create remote trace chunk: relayd does not support chunks");
1337 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1338 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1343 status
= lttng_trace_chunk_get_creation_timestamp(
1344 chunk
, &creation_timestamp
);
1345 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1350 status
= lttng_trace_chunk_get_name(
1351 chunk
, &chunk_name
, &overridden_name
);
1352 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1353 status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1358 chunk_name_length
= overridden_name
? (strlen(chunk_name
) + 1) : 0;
1359 msg
.chunk_id
= htobe64(chunk_id
);
1360 msg
.creation_timestamp
= htobe64((uint64_t) creation_timestamp
);
1361 msg
.override_name_length
= htobe32((uint32_t) chunk_name_length
);
1363 ret
= lttng_dynamic_buffer_append(&payload
, &msg
, sizeof(msg
));
1367 if (chunk_name_length
) {
1368 ret
= lttng_dynamic_buffer_append(
1369 &payload
, chunk_name
, chunk_name_length
);
1375 ret
= send_command(sock
, RELAYD_CREATE_TRACE_CHUNK
, payload
.data
,
1378 ERR("Failed to send trace chunk creation command to relay daemon");
1382 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1384 ERR("Failed to receive relay daemon trace chunk creation command reply");
1388 reply
.ret_code
= be32toh(reply
.ret_code
);
1389 if (reply
.ret_code
!= LTTNG_OK
) {
1391 ERR("Relayd trace chunk create replied error %d",
1395 DBG("Relayd successfully created trace chunk: chunk_id = %" PRIu64
,
1400 lttng_dynamic_buffer_reset(&payload
);
1404 int relayd_close_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1405 struct lttng_trace_chunk
*chunk
,
1409 enum lttng_trace_chunk_status status
;
1410 struct lttcomm_relayd_close_trace_chunk msg
= {};
1411 struct lttcomm_relayd_close_trace_chunk_reply reply
= {};
1413 time_t close_timestamp
;
1414 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
1416 if (!relayd_supports_chunks(sock
)) {
1417 DBG("Refusing to close remote trace chunk: relayd does not support chunks");
1421 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1422 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1423 ERR("Failed to get trace chunk id");
1428 status
= lttng_trace_chunk_get_close_timestamp(chunk
, &close_timestamp
);
1429 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1430 ERR("Failed to get trace chunk close timestamp");
1435 status
= lttng_trace_chunk_get_close_command(chunk
,
1436 &close_command
.value
);
1438 case LTTNG_TRACE_CHUNK_STATUS_OK
:
1439 close_command
.is_set
= 1;
1441 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
1444 ERR("Failed to get trace chunk close command");
1449 msg
= (typeof(msg
)){
1450 .chunk_id
= htobe64(chunk_id
),
1451 .close_timestamp
= htobe64((uint64_t) close_timestamp
),
1453 .is_set
= close_command
.is_set
,
1454 .value
= htobe32((uint32_t) close_command
.value
),
1458 ret
= send_command(sock
, RELAYD_CLOSE_TRACE_CHUNK
, &msg
, sizeof(msg
),
1461 ERR("Failed to send trace chunk close command to relay daemon");
1465 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1467 ERR("Failed to receive relay daemon trace chunk close command reply");
1471 reply
.path_length
= be32toh(reply
.path_length
);
1472 if (reply
.path_length
>= LTTNG_PATH_MAX
) {
1473 ERR("Chunk path too long");
1478 ret
= recv_reply(sock
, path
, reply
.path_length
);
1480 ERR("Failed to receive relay daemon trace chunk close command reply");
1483 if (path
[reply
.path_length
- 1] != '\0') {
1484 ERR("Invalid trace chunk path returned by relay daemon (not null-terminated)");
1489 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1490 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1492 ERR("Relayd trace chunk close replied error %d",
1493 reply
.generic
.ret_code
);
1496 DBG("Relayd successfully closed trace chunk: chunk_id = %" PRIu64
,
1503 int relayd_trace_chunk_exists(struct lttcomm_relayd_sock
*sock
,
1504 uint64_t chunk_id
, bool *chunk_exists
)
1507 struct lttcomm_relayd_trace_chunk_exists msg
= {};
1508 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
1510 if (!relayd_supports_chunks(sock
)) {
1511 DBG("Refusing to check for trace chunk existence: relayd does not support chunks");
1512 /* The chunk will never exist */
1513 *chunk_exists
= false;
1517 msg
= (typeof(msg
)){
1518 .chunk_id
= htobe64(chunk_id
),
1521 ret
= send_command(sock
, RELAYD_TRACE_CHUNK_EXISTS
, &msg
, sizeof(msg
),
1524 ERR("Failed to send trace chunk exists command to relay daemon");
1528 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1530 ERR("Failed to receive relay daemon trace chunk close command reply");
1534 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1535 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1537 ERR("Relayd trace chunk close replied error %d",
1538 reply
.generic
.ret_code
);
1541 DBG("Relayd successfully checked trace chunk existence: chunk_id = %" PRIu64
1542 ", exists = %s", chunk_id
,
1543 reply
.trace_chunk_exists
? "true" : "false");
1544 *chunk_exists
= !!reply
.trace_chunk_exists
;
1550 enum lttng_error_code
relayd_get_configuration(struct lttcomm_relayd_sock
*sock
,
1551 uint64_t query_flags
,
1552 uint64_t& result_flags
,
1553 uint64_t *trace_format_query_results
)
1556 enum lttng_error_code ret_code
= LTTNG_OK
;
1557 struct lttcomm_relayd_get_configuration msg
= {};
1558 struct lttcomm_relayd_get_configuration_reply reply
= {};
1559 lttng_dynamic_buffer buffer
;
1560 bool requesting_trace_format
= query_flags
&
1561 LTTCOMM_RELAYD_CONFIGURATION_QUERY_FLAG_SUPPORTED_TRACE_FORMAT
;
1563 lttng_dynamic_buffer_init(&buffer
);
1565 assert(!(query_flags
& ~LTTCOMM_RELAYD_CONFIGURATION_QUERY_FLAG_MASK
));
1566 assert(!(requesting_trace_format
&& !trace_format_query_results
));
1568 if (!relayd_supports_get_configuration(sock
)) {
1569 DBG("Refusing to get relayd configuration (unsupported by relayd)");
1571 if (trace_format_query_results
) {
1572 *trace_format_query_results
=
1573 LTTCOMM_RELAYD_CONFIGURATION_TRACE_FORMAT_SUPPORTED_CTF1
;
1578 if (requesting_trace_format
&& !relayd_supports_get_configuration_trace_format(sock
)) {
1580 * Provide default value for that query since lttng-relayd does
1581 * not know this query type.
1583 if (trace_format_query_results
) {
1584 *trace_format_query_results
=
1585 LTTCOMM_RELAYD_CONFIGURATION_TRACE_FORMAT_SUPPORTED_CTF1
;
1587 /* Remove from the query set. */
1588 query_flags
&= ~LTTCOMM_RELAYD_CONFIGURATION_QUERY_FLAG_SUPPORTED_TRACE_FORMAT
;
1589 requesting_trace_format
= false;
1592 msg
.query_flags
= htobe64(query_flags
);
1594 ret
= send_command(sock
, RELAYD_GET_CONFIGURATION
, &msg
, sizeof(msg
), 0);
1596 ERR("Failed to send get configuration command to relay daemon");
1597 ret_code
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1601 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1603 ERR("Failed to receive relay daemon get configuration command reply");
1604 ret_code
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1608 ret_code
= static_cast<enum lttng_error_code
>(be32toh(reply
.generic
.ret_code
));
1609 if (ret_code
!= LTTNG_OK
) {
1610 ERR("Relayd get configuration replied error %d", ret_code
);
1614 result_flags
= be64toh(reply
.relayd_configuration_flags
);
1615 DBG("Relayd successfully got configuration: query_flags = %" PRIu64
1616 ", results_flags = %" PRIu64
,
1617 query_flags
, result_flags
);
1619 if (!requesting_trace_format
) {
1620 ret_code
= LTTNG_OK
;
1624 /* Receive trace formats */
1626 lttcomm_relayd_get_configuration_specialized_query_reply query_flag_reply
= {};
1627 ret
= recv_reply(sock
, &query_flag_reply
, sizeof(query_flag_reply
));
1629 ERR("Failed to receive relay daemon get configuration query flag data");
1630 ret_code
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1634 query_flag_reply
.query_flag
= be64toh(query_flag_reply
.query_flag
);
1635 LTTNG_ASSERT(query_flag_reply
.query_flag
&
1636 LTTCOMM_RELAYD_CONFIGURATION_QUERY_FLAG_SUPPORTED_TRACE_FORMAT
);
1638 query_flag_reply
.payload_len
= be64toh(query_flag_reply
.payload_len
);
1639 LTTNG_ASSERT(query_flag_reply
.payload_len
== sizeof(uint64_t));
1641 lttng_dynamic_buffer_set_size(&buffer
, query_flag_reply
.payload_len
);
1643 ret
= recv_reply(sock
, buffer
.data
, query_flag_reply
.payload_len
);
1645 ret_code
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1646 ERR("Failed to receive configuration dynamic payload for flag TODO");
1650 *trace_format_query_results
= be64toh(*(uint64_t *) buffer
.data
);
1653 ret_code
= LTTNG_OK
;
1656 lttng_dynamic_buffer_reset(&buffer
);