2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/common.hpp>
12 #include <common/compat/endian.hpp>
13 #include <common/compat/string.hpp>
14 #include <common/defaults.hpp>
15 #include <common/index/ctf-index.hpp>
16 #include <common/sessiond-comm/relayd.hpp>
17 #include <common/string-utils/format.hpp>
18 #include <common/trace-chunk.hpp>
26 static bool relayd_supports_chunks(const struct lttcomm_relayd_sock
*sock
)
28 if (sock
->major
> 2) {
30 } else if (sock
->major
== 2 && sock
->minor
>= 11) {
36 static bool relayd_supports_get_configuration(const struct lttcomm_relayd_sock
*sock
)
38 if (sock
->major
> 2) {
40 } else if (sock
->major
== 2 && sock
->minor
>= 12) {
47 * Send command. Fill up the header and append the data.
49 static int send_command(struct lttcomm_relayd_sock
*rsock
,
50 enum lttcomm_relayd_command cmd
,
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
),
92 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
94 PERROR("Failed to send command %s of size %" PRIu64
,
95 lttcomm_relayd_command_str(cmd
),
97 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
108 * Receive reply data on socket. This MUST be call after send_command or else
109 * could result in unexpected behavior(s).
111 static int recv_reply(struct lttcomm_relayd_sock
*rsock
, void *data
, size_t size
)
115 if (rsock
->sock
.fd
< 0) {
119 DBG3("Relayd waiting for reply of size %zu", size
);
121 ret
= rsock
->sock
.ops
->recvmsg(&rsock
->sock
, data
, size
, 0);
122 if (ret
<= 0 || ret
!= size
) {
124 /* Orderly shutdown. */
125 DBG("Socket %d has performed an orderly shutdown", rsock
->sock
.fd
);
127 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
132 /* Always return -1 here and the caller can use errno. */
142 * Starting from 2.11, RELAYD_CREATE_SESSION payload (session_name,
143 * hostname, and base_path) have no length restriction on the sender side.
144 * Length for both payloads is stored in the msg struct. A new dynamic size
145 * payload size is introduced.
147 static int relayd_create_session_2_11(struct lttcomm_relayd_sock
*rsock
,
148 const char *session_name
,
149 const char *hostname
,
150 const char *base_path
,
151 int session_live_timer
,
152 unsigned int snapshot
,
153 uint64_t sessiond_session_id
,
154 const lttng_uuid
& sessiond_uuid
,
155 const uint64_t *current_chunk_id
,
156 time_t creation_time
,
157 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
= nullptr;
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
<lttcomm_relayd_create_session_2_11
>(msg_length
);
180 PERROR("zmalloc create_session_2_11 command message");
185 LTTNG_ASSERT(session_name_len
<= UINT32_MAX
);
186 msg
->session_name_len
= htobe32(session_name_len
);
188 LTTNG_ASSERT(hostname_len
<= UINT32_MAX
);
189 msg
->hostname_len
= htobe32(hostname_len
);
191 LTTNG_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 std::copy(sessiond_uuid
.begin(), sessiond_uuid
.end(), msg
->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
, htobe64(*current_chunk_id
));
220 msg
->creation_time
= htobe64((uint64_t) creation_time
);
223 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, msg
, msg_length
, 0);
227 /* Receive response */
228 ret
= recv_reply(rsock
, reply
, sizeof(*reply
));
232 reply
->generic
.session_id
= be64toh(reply
->generic
.session_id
);
233 reply
->generic
.ret_code
= be32toh(reply
->generic
.ret_code
);
234 reply
->output_path_length
= be32toh(reply
->output_path_length
);
235 if (reply
->output_path_length
>= LTTNG_PATH_MAX
) {
236 ERR("Invalid session output path length in reply (%" PRIu32
237 " bytes) exceeds maximal allowed length (%d bytes)",
238 reply
->output_path_length
,
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
,
257 const char *hostname
,
258 int session_live_timer
,
259 unsigned int snapshot
,
260 struct lttcomm_relayd_status_session
*reply
)
263 struct lttcomm_relayd_create_session_2_4 msg
;
265 if (lttng_strncpy(msg
.session_name
, session_name
, sizeof(msg
.session_name
))) {
269 if (lttng_strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
))) {
273 msg
.live_timer
= htobe32(session_live_timer
);
274 msg
.snapshot
= htobe32(snapshot
);
277 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, &msg
, sizeof(msg
), 0);
282 /* Receive response */
283 ret
= recv_reply(rsock
, reply
, sizeof(*reply
));
287 reply
->session_id
= be64toh(reply
->session_id
);
288 reply
->ret_code
= be32toh(reply
->ret_code
);
294 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
296 static int relayd_create_session_2_1(struct lttcomm_relayd_sock
*rsock
,
297 struct lttcomm_relayd_status_session
*reply
)
302 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, nullptr, 0, 0);
307 /* Receive response */
308 ret
= recv_reply(rsock
, reply
, sizeof(*reply
));
312 reply
->session_id
= be64toh(reply
->session_id
);
313 reply
->ret_code
= be32toh(reply
->ret_code
);
319 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
320 * set session_id of the relayd if we have a successful reply from the relayd.
322 * On success, return 0 else a negative value which is either an errno error or
323 * a lttng error code from the relayd.
325 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
,
326 uint64_t *relayd_session_id
,
327 const char *session_name
,
328 const char *hostname
,
329 const char *base_path
,
330 int session_live_timer
,
331 unsigned int snapshot
,
332 uint64_t sessiond_session_id
,
333 const lttng_uuid
& sessiond_uuid
,
334 const uint64_t *current_chunk_id
,
335 time_t creation_time
,
336 bool session_name_contains_creation_time
,
340 struct lttcomm_relayd_create_session_reply_2_11 reply
= {};
343 LTTNG_ASSERT(relayd_session_id
);
345 DBG("Relayd create session");
347 if (rsock
->minor
< 4) {
348 /* From 2.1 to 2.3 */
349 ret
= relayd_create_session_2_1(rsock
, &reply
.generic
);
350 } else if (rsock
->minor
>= 4 && rsock
->minor
< 11) {
351 /* From 2.4 to 2.10 */
352 ret
= relayd_create_session_2_4(
353 rsock
, session_name
, hostname
, session_live_timer
, snapshot
, &reply
.generic
);
355 /* From 2.11 to ... */
356 ret
= relayd_create_session_2_11(rsock
,
366 session_name_contains_creation_time
,
375 /* Return session id or negative ret code. */
376 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
378 ERR("Relayd create session replied error %d", reply
.generic
.ret_code
);
382 *relayd_session_id
= reply
.generic
.session_id
;
385 DBG("Relayd session created with id %" PRIu64
, reply
.generic
.session_id
);
391 static int relayd_add_stream_2_1(struct lttcomm_relayd_sock
*rsock
,
392 const char *channel_name
,
393 const char *pathname
)
396 struct lttcomm_relayd_add_stream msg
;
398 memset(&msg
, 0, sizeof(msg
));
399 if (lttng_strncpy(msg
.channel_name
, channel_name
, sizeof(msg
.channel_name
))) {
404 if (lttng_strncpy(msg
.pathname
, pathname
, sizeof(msg
.pathname
))) {
410 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
420 static int relayd_add_stream_2_2(struct lttcomm_relayd_sock
*rsock
,
421 const char *channel_name
,
422 const char *pathname
,
423 uint64_t tracefile_size
,
424 uint64_t tracefile_count
)
427 struct lttcomm_relayd_add_stream_2_2 msg
;
429 memset(&msg
, 0, sizeof(msg
));
430 /* Compat with relayd 2.2 to 2.10 */
431 if (lttng_strncpy(msg
.channel_name
, channel_name
, sizeof(msg
.channel_name
))) {
435 if (lttng_strncpy(msg
.pathname
, pathname
, sizeof(msg
.pathname
))) {
439 msg
.tracefile_size
= htobe64(tracefile_size
);
440 msg
.tracefile_count
= htobe64(tracefile_count
);
443 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
452 static int relayd_add_stream_2_11(struct lttcomm_relayd_sock
*rsock
,
453 const char *channel_name
,
454 const char *pathname
,
455 uint64_t tracefile_size
,
456 uint64_t tracefile_count
,
457 uint64_t trace_archive_id
)
460 struct lttcomm_relayd_add_stream_2_11
*msg
= nullptr;
461 size_t channel_name_len
;
465 /* The two names are sent with a '\0' delimiter between them. */
466 channel_name_len
= strlen(channel_name
) + 1;
467 pathname_len
= strlen(pathname
) + 1;
469 msg_length
= sizeof(*msg
) + channel_name_len
+ pathname_len
;
470 msg
= zmalloc
<lttcomm_relayd_add_stream_2_11
>(msg_length
);
472 PERROR("zmalloc add_stream_2_11 command message");
477 LTTNG_ASSERT(channel_name_len
<= UINT32_MAX
);
478 msg
->channel_name_len
= htobe32(channel_name_len
);
480 LTTNG_ASSERT(pathname_len
<= UINT32_MAX
);
481 msg
->pathname_len
= htobe32(pathname_len
);
483 if (lttng_strncpy(msg
->names
, channel_name
, channel_name_len
)) {
487 if (lttng_strncpy(msg
->names
+ channel_name_len
, pathname
, pathname_len
)) {
492 msg
->tracefile_size
= htobe64(tracefile_size
);
493 msg
->tracefile_count
= htobe64(tracefile_count
);
494 msg
->trace_chunk_id
= htobe64(trace_archive_id
);
497 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) msg
, msg_length
, 0);
508 * Add stream on the relayd and assign stream handle to the stream_id argument.
510 * Chunks are not supported by relayd prior to 2.11, but are used to
511 * internally between session daemon and consumer daemon to keep track
512 * of the channel and stream output path.
514 * On success return 0 else return ret_code negative value.
516 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
,
517 const char *channel_name
,
518 const char *domain_name
,
519 const char *_pathname
,
521 uint64_t tracefile_size
,
522 uint64_t tracefile_count
,
523 struct lttng_trace_chunk
*trace_chunk
)
526 struct lttcomm_relayd_status_stream reply
;
527 char pathname
[RELAYD_COMM_LTTNG_PATH_MAX
];
529 /* Code flow error. Safety net. */
531 LTTNG_ASSERT(channel_name
);
532 LTTNG_ASSERT(domain_name
);
533 LTTNG_ASSERT(_pathname
);
534 LTTNG_ASSERT(trace_chunk
);
536 DBG("Relayd adding stream for channel name %s", channel_name
);
538 /* Compat with relayd 2.1 */
539 if (rsock
->minor
== 1) {
541 ret
= relayd_add_stream_2_1(rsock
, channel_name
, _pathname
);
543 } else if (rsock
->minor
> 1 && rsock
->minor
< 11) {
544 /* From 2.2 to 2.10 */
545 ret
= relayd_add_stream_2_2(
546 rsock
, channel_name
, _pathname
, tracefile_size
, tracefile_count
);
548 const char *separator
;
549 enum lttng_trace_chunk_status chunk_status
;
552 if (_pathname
[0] == '\0') {
558 ret
= snprintf(pathname
,
559 RELAYD_COMM_LTTNG_PATH_MAX
,
564 if (ret
<= 0 || ret
>= RELAYD_COMM_LTTNG_PATH_MAX
) {
565 ERR("Failed to format stream path: %s",
566 ret
<= 0 ? "formatting error" : "path exceeds maximal allowed length");
571 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
, &chunk_id
);
572 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
574 /* From 2.11 to ...*/
575 ret
= relayd_add_stream_2_11(
576 rsock
, channel_name
, pathname
, tracefile_size
, tracefile_count
, chunk_id
);
584 /* Waiting for reply */
585 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
590 /* Back to host bytes order. */
591 reply
.handle
= be64toh(reply
.handle
);
592 reply
.ret_code
= be32toh(reply
.ret_code
);
594 /* Return session id or negative ret code. */
595 if (reply
.ret_code
!= LTTNG_OK
) {
597 ERR("Relayd add stream replied error %d", reply
.ret_code
);
601 *stream_id
= reply
.handle
;
604 DBG("Relayd stream added successfully with handle %" PRIu64
, reply
.handle
);
611 * Inform the relay that all the streams for the current channel has been sent.
613 * On success return 0 else return ret_code negative value.
615 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
618 struct lttcomm_relayd_generic_reply reply
;
620 /* Code flow error. Safety net. */
623 DBG("Relayd sending streams sent.");
625 /* This feature was introduced in 2.4, ignore it for earlier versions. */
626 if (rsock
->minor
< 4) {
632 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, nullptr, 0, 0);
637 /* Waiting for reply */
638 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
643 /* Back to host bytes order. */
644 reply
.ret_code
= be32toh(reply
.ret_code
);
646 /* Return session id or negative ret code. */
647 if (reply
.ret_code
!= LTTNG_OK
) {
649 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
656 DBG("Relayd streams sent success");
664 * Check version numbers on the relayd.
665 * If major versions are compatible, we assign minor_to_use to the
666 * minor version of the procotol we are going to use for this session.
668 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
669 * otherwise, or a negative value on network errors.
671 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
674 struct lttcomm_relayd_version msg
;
676 /* Code flow error. Safety net. */
679 DBG("Relayd version check for major.minor %u.%u", rsock
->major
, rsock
->minor
);
681 memset(&msg
, 0, sizeof(msg
));
682 /* Prepare network byte order before transmission. */
683 msg
.major
= htobe32(rsock
->major
);
684 msg
.minor
= htobe32(rsock
->minor
);
687 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
692 /* Receive response */
693 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
698 /* Set back to host bytes order */
699 msg
.major
= be32toh(msg
.major
);
700 msg
.minor
= be32toh(msg
.minor
);
703 * Only validate the major version. If the other side is higher,
704 * communication is not possible. Only major version equal can talk to each
705 * other. If the minor version differs, the lowest version is used by both
708 if (msg
.major
!= rsock
->major
) {
710 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
711 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
718 * If the relayd's minor version is higher, it will adapt to our version so
719 * we can continue to use the latest relayd communication data structure.
720 * If the received minor version is higher, the relayd should adapt to us.
722 if (rsock
->minor
> msg
.minor
) {
723 rsock
->minor
= msg
.minor
;
726 /* Version number compatible */
727 DBG2("Relayd version is compatible, using protocol version %u.%u",
737 * Add stream on the relayd and assign stream handle to the stream_id argument.
739 * On success return 0 else return ret_code negative value.
741 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
745 /* Code flow error. Safety net. */
748 DBG("Relayd sending metadata of size %zu", len
);
751 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, nullptr, len
, 0);
756 DBG2("Relayd metadata added successfully");
759 * After that call, the metadata data MUST be sent to the relayd so the
760 * receive size on the other end matches the len of the metadata packet
761 * header. This is why we don't wait for a reply here.
769 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
771 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
773 /* Code flow error. Safety net. */
776 if (!rsock
->sock
.ops
) {
778 * Attempting a connect on a non-initialized socket.
783 DBG3("Relayd connect ...");
785 return rsock
->sock
.ops
->connect(&rsock
->sock
);
789 * Close relayd socket with an allocated lttcomm_relayd_sock.
791 * If no socket operations are found, simply return 0 meaning that everything
792 * is fine. Without operations, the socket can not possibly be opened or used.
793 * This is possible if the socket was allocated but not created. However, the
794 * caller could simply use it to store a valid file descriptor for instance
795 * passed over a Unix socket and call this to cleanup but still without a valid
798 * Return the close returned value. On error, a negative value is usually
799 * returned back from close(2).
801 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
805 /* Code flow error. Safety net. */
808 /* An invalid fd is fine, return success. */
809 if (rsock
->sock
.fd
< 0) {
814 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
816 if (rsock
->sock
.ops
) {
817 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
819 /* Default call if no specific ops found. */
820 ret
= close(rsock
->sock
.fd
);
822 PERROR("relayd_close default close");
832 * Send data header structure to the relayd.
834 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
835 struct lttcomm_relayd_data_hdr
*hdr
,
840 /* Code flow error. Safety net. */
844 if (rsock
->sock
.fd
< 0) {
848 DBG3("Relayd sending data header of size %zu", size
);
850 /* Again, safety net */
852 size
= sizeof(struct lttcomm_relayd_data_hdr
);
855 /* Only send data header. */
856 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
863 * The data MUST be sent right after that command for the receive on the
864 * other end to match the size in the header.
872 * Send close stream command to the relayd.
874 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
,
876 uint64_t last_net_seq_num
)
879 struct lttcomm_relayd_close_stream msg
;
880 struct lttcomm_relayd_generic_reply reply
;
882 /* Code flow error. Safety net. */
885 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
887 memset(&msg
, 0, sizeof(msg
));
888 msg
.stream_id
= htobe64(stream_id
);
889 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
892 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
897 /* Receive response */
898 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
903 reply
.ret_code
= be32toh(reply
.ret_code
);
905 /* Return session id or negative ret code. */
906 if (reply
.ret_code
!= LTTNG_OK
) {
908 ERR("Relayd close stream replied error %d", reply
.ret_code
);
914 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
921 * Check for data availability for a given stream id.
923 * Return 0 if NOT pending, 1 if so and a negative value on error.
925 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
,
927 uint64_t last_net_seq_num
)
930 struct lttcomm_relayd_data_pending msg
;
931 struct lttcomm_relayd_generic_reply reply
;
933 /* Code flow error. Safety net. */
936 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
938 memset(&msg
, 0, sizeof(msg
));
939 msg
.stream_id
= htobe64(stream_id
);
940 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
943 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
, sizeof(msg
), 0);
948 /* Receive response */
949 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
954 reply
.ret_code
= be32toh(reply
.ret_code
);
956 /* Return session id or negative ret code. */
957 if (reply
.ret_code
>= LTTNG_OK
) {
958 ERR("Relayd data pending replied error %d", reply
.ret_code
);
961 /* At this point, the ret code is either 1 or 0 */
962 ret
= reply
.ret_code
;
964 DBG("Relayd data is %s pending for stream id %" PRIu64
, ret
== 1 ? "" : "NOT", stream_id
);
971 * Check on the relayd side for a quiescent state on the control socket.
973 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
, uint64_t metadata_stream_id
)
976 struct lttcomm_relayd_quiescent_control msg
;
977 struct lttcomm_relayd_generic_reply reply
;
979 /* Code flow error. Safety net. */
982 DBG("Relayd checking quiescent control state");
984 memset(&msg
, 0, sizeof(msg
));
985 msg
.stream_id
= htobe64(metadata_stream_id
);
988 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
993 /* Receive response */
994 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
999 reply
.ret_code
= be32toh(reply
.ret_code
);
1001 /* Return session id or negative ret code. */
1002 if (reply
.ret_code
!= LTTNG_OK
) {
1004 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
1008 /* Control socket is quiescent */
1016 * Begin a data pending command for a specific session id.
1018 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
1021 struct lttcomm_relayd_begin_data_pending msg
;
1022 struct lttcomm_relayd_generic_reply reply
;
1024 /* Code flow error. Safety net. */
1025 LTTNG_ASSERT(rsock
);
1027 DBG("Relayd begin data pending");
1029 memset(&msg
, 0, sizeof(msg
));
1030 msg
.session_id
= htobe64(id
);
1033 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
1038 /* Receive response */
1039 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1044 reply
.ret_code
= be32toh(reply
.ret_code
);
1046 /* Return session id or negative ret code. */
1047 if (reply
.ret_code
!= LTTNG_OK
) {
1049 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
1060 * End a data pending command for a specific session id.
1062 * Return 0 on success and set is_data_inflight to 0 if no data is being
1063 * streamed or 1 if it is the case.
1065 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
,
1067 unsigned int *is_data_inflight
)
1070 struct lttcomm_relayd_end_data_pending msg
;
1071 struct lttcomm_relayd_generic_reply reply
;
1073 /* Code flow error. Safety net. */
1074 LTTNG_ASSERT(rsock
);
1076 DBG("Relayd end data pending");
1078 memset(&msg
, 0, sizeof(msg
));
1079 msg
.session_id
= htobe64(id
);
1082 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
1087 /* Receive response */
1088 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1093 recv_ret
= be32toh(reply
.ret_code
);
1099 *is_data_inflight
= recv_ret
;
1101 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
1110 * Send index to the relayd.
1112 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
1113 struct ctf_packet_index
*index
,
1114 uint64_t relay_stream_id
,
1115 uint64_t net_seq_num
)
1118 struct lttcomm_relayd_index msg
;
1119 struct lttcomm_relayd_generic_reply reply
;
1121 /* Code flow error. Safety net. */
1122 LTTNG_ASSERT(rsock
);
1124 if (rsock
->minor
< 4) {
1125 DBG("Not sending indexes before protocol 2.4");
1130 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
1132 memset(&msg
, 0, sizeof(msg
));
1133 msg
.relay_stream_id
= htobe64(relay_stream_id
);
1134 msg
.net_seq_num
= htobe64(net_seq_num
);
1136 /* The index is already in big endian. */
1137 msg
.packet_size
= index
->packet_size
;
1138 msg
.content_size
= index
->content_size
;
1139 msg
.timestamp_begin
= index
->timestamp_begin
;
1140 msg
.timestamp_end
= index
->timestamp_end
;
1141 msg
.events_discarded
= index
->events_discarded
;
1142 msg
.stream_id
= index
->stream_id
;
1144 if (rsock
->minor
>= 8) {
1145 msg
.stream_instance_id
= index
->stream_instance_id
;
1146 msg
.packet_seq_num
= index
->packet_seq_num
;
1154 lttcomm_relayd_index_len(lttng_to_index_major(rsock
->major
, rsock
->minor
),
1155 lttng_to_index_minor(rsock
->major
, rsock
->minor
)),
1161 /* Receive response */
1162 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1167 reply
.ret_code
= be32toh(reply
.ret_code
);
1169 /* Return session id or negative ret code. */
1170 if (reply
.ret_code
!= LTTNG_OK
) {
1172 ERR("Relayd send index replied error %d", reply
.ret_code
);
1183 * Ask the relay to reset the metadata trace file (regeneration).
1185 int relayd_reset_metadata(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
, uint64_t version
)
1188 struct lttcomm_relayd_reset_metadata msg
;
1189 struct lttcomm_relayd_generic_reply reply
;
1191 /* Code flow error. Safety net. */
1192 LTTNG_ASSERT(rsock
);
1194 /* Should have been prevented by the sessiond. */
1195 if (rsock
->minor
< 8) {
1196 ERR("Metadata regeneration unsupported before 2.8");
1201 DBG("Relayd reset metadata stream id %" PRIu64
, stream_id
);
1203 memset(&msg
, 0, sizeof(msg
));
1204 msg
.stream_id
= htobe64(stream_id
);
1205 msg
.version
= htobe64(version
);
1208 ret
= send_command(rsock
, RELAYD_RESET_METADATA
, (void *) &msg
, sizeof(msg
), 0);
1213 /* Receive response */
1214 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1219 reply
.ret_code
= be32toh(reply
.ret_code
);
1221 /* Return session id or negative ret code. */
1222 if (reply
.ret_code
!= LTTNG_OK
) {
1224 ERR("Relayd reset metadata replied error %d", reply
.ret_code
);
1230 DBG("Relayd reset metadata stream id %" PRIu64
" successfully", stream_id
);
1236 int relayd_rotate_streams(struct lttcomm_relayd_sock
*sock
,
1237 unsigned int stream_count
,
1238 const uint64_t *new_chunk_id
,
1239 const struct relayd_stream_rotation_position
*positions
)
1243 struct lttng_dynamic_buffer payload
;
1244 struct lttcomm_relayd_generic_reply reply
= {};
1245 struct lttcomm_relayd_rotate_streams msg
;
1246 char new_chunk_id_buf
[MAX_INT_DEC_LEN(*new_chunk_id
)] = {};
1247 const char *new_chunk_id_str
;
1249 msg
.stream_count
= htobe32((uint32_t) stream_count
);
1250 msg
.new_chunk_id
= (typeof(msg
.new_chunk_id
)){
1251 .is_set
= !!new_chunk_id
,
1252 .value
= htobe64(new_chunk_id
? *new_chunk_id
: 0),
1255 if (!relayd_supports_chunks(sock
)) {
1256 DBG("Refusing to rotate remote streams: relayd does not support chunks");
1260 lttng_dynamic_buffer_init(&payload
);
1262 /* Code flow error. Safety net. */
1267 new_chunk_id_buf
, sizeof(new_chunk_id_buf
), "%" PRIu64
, *new_chunk_id
);
1268 if (ret
== -1 || ret
>= sizeof(new_chunk_id_buf
)) {
1269 new_chunk_id_str
= "formatting error";
1271 new_chunk_id_str
= new_chunk_id_buf
;
1274 new_chunk_id_str
= "none";
1277 DBG("Preparing \"rotate streams\" command payload: new_chunk_id = %s, stream_count = %u",
1281 ret
= lttng_dynamic_buffer_append(&payload
, &msg
, sizeof(msg
));
1283 ERR("Failed to allocate \"rotate streams\" command payload");
1287 for (i
= 0; i
< stream_count
; i
++) {
1288 const struct relayd_stream_rotation_position
*position
= &positions
[i
];
1289 const struct lttcomm_relayd_stream_rotation_position comm_position
= {
1290 .stream_id
= htobe64(position
->stream_id
),
1291 .rotate_at_seq_num
= htobe64(position
->rotate_at_seq_num
),
1294 DBG("Rotate stream %" PRIu64
" at sequence number %" PRIu64
,
1295 position
->stream_id
,
1296 position
->rotate_at_seq_num
);
1297 ret
= lttng_dynamic_buffer_append(&payload
, &comm_position
, sizeof(comm_position
));
1299 ERR("Failed to allocate \"rotate streams\" command payload");
1305 ret
= send_command(sock
, RELAYD_ROTATE_STREAMS
, payload
.data
, payload
.size
, 0);
1307 ERR("Failed to send \"rotate stream\" command");
1311 /* Receive response. */
1312 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1314 ERR("Failed to receive \"rotate streams\" command reply");
1318 reply
.ret_code
= be32toh(reply
.ret_code
);
1319 if (reply
.ret_code
!= LTTNG_OK
) {
1321 ERR("Relayd rotate streams replied error %d", reply
.ret_code
);
1325 DBG("Relayd rotated streams successfully");
1329 lttng_dynamic_buffer_reset(&payload
);
1333 int relayd_create_trace_chunk(struct lttcomm_relayd_sock
*sock
, struct lttng_trace_chunk
*chunk
)
1336 enum lttng_trace_chunk_status status
;
1337 struct lttcomm_relayd_create_trace_chunk msg
= {};
1338 struct lttcomm_relayd_generic_reply reply
= {};
1339 struct lttng_dynamic_buffer payload
;
1341 time_t creation_timestamp
;
1342 const char *chunk_name
;
1343 size_t chunk_name_length
;
1344 bool overridden_name
;
1346 lttng_dynamic_buffer_init(&payload
);
1348 if (!relayd_supports_chunks(sock
)) {
1349 DBG("Refusing to create remote trace chunk: relayd does not support chunks");
1353 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1354 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1359 status
= lttng_trace_chunk_get_creation_timestamp(chunk
, &creation_timestamp
);
1360 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1365 status
= lttng_trace_chunk_get_name(chunk
, &chunk_name
, &overridden_name
);
1366 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&& status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1371 chunk_name_length
= overridden_name
? (strlen(chunk_name
) + 1) : 0;
1372 msg
.chunk_id
= htobe64(chunk_id
);
1373 msg
.creation_timestamp
= htobe64((uint64_t) creation_timestamp
);
1374 msg
.override_name_length
= htobe32((uint32_t) chunk_name_length
);
1376 ret
= lttng_dynamic_buffer_append(&payload
, &msg
, sizeof(msg
));
1380 if (chunk_name_length
) {
1381 ret
= lttng_dynamic_buffer_append(&payload
, chunk_name
, chunk_name_length
);
1387 ret
= send_command(sock
, RELAYD_CREATE_TRACE_CHUNK
, payload
.data
, payload
.size
, 0);
1389 ERR("Failed to send trace chunk creation command to relay daemon");
1393 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1395 ERR("Failed to receive relay daemon trace chunk creation command reply");
1399 reply
.ret_code
= be32toh(reply
.ret_code
);
1400 if (reply
.ret_code
!= LTTNG_OK
) {
1402 ERR("Relayd trace chunk create replied error %d", reply
.ret_code
);
1405 DBG("Relayd successfully created trace chunk: chunk_id = %" PRIu64
, chunk_id
);
1409 lttng_dynamic_buffer_reset(&payload
);
1413 int relayd_close_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1414 struct lttng_trace_chunk
*chunk
,
1418 enum lttng_trace_chunk_status status
;
1419 struct lttcomm_relayd_close_trace_chunk msg
= {};
1420 struct lttcomm_relayd_close_trace_chunk_reply reply
= {};
1422 time_t close_timestamp
;
1423 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
1425 if (!relayd_supports_chunks(sock
)) {
1426 DBG("Refusing to close remote trace chunk: relayd does not support chunks");
1430 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1431 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1432 ERR("Failed to get trace chunk id");
1437 status
= lttng_trace_chunk_get_close_timestamp(chunk
, &close_timestamp
);
1438 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1439 ERR("Failed to get trace chunk close timestamp");
1444 status
= lttng_trace_chunk_get_close_command(chunk
, &close_command
.value
);
1446 case LTTNG_TRACE_CHUNK_STATUS_OK
:
1447 close_command
.is_set
= 1;
1449 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
1452 ERR("Failed to get trace chunk close command");
1457 msg
= (typeof(msg
)){
1458 .chunk_id
= htobe64(chunk_id
),
1459 .close_timestamp
= htobe64((uint64_t) close_timestamp
),
1461 .is_set
= close_command
.is_set
,
1462 .value
= htobe32((uint32_t) close_command
.value
),
1466 ret
= send_command(sock
, RELAYD_CLOSE_TRACE_CHUNK
, &msg
, sizeof(msg
), 0);
1468 ERR("Failed to send trace chunk close command to relay daemon");
1472 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1474 ERR("Failed to receive relay daemon trace chunk close command reply");
1478 reply
.path_length
= be32toh(reply
.path_length
);
1479 if (reply
.path_length
>= LTTNG_PATH_MAX
) {
1480 ERR("Chunk path too long");
1485 ret
= recv_reply(sock
, path
, reply
.path_length
);
1487 ERR("Failed to receive relay daemon trace chunk close command reply");
1490 if (path
[reply
.path_length
- 1] != '\0') {
1491 ERR("Invalid trace chunk path returned by relay daemon (not null-terminated)");
1496 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1497 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1499 ERR("Relayd trace chunk close replied error %d", reply
.generic
.ret_code
);
1502 DBG("Relayd successfully closed trace chunk: chunk_id = %" PRIu64
, chunk_id
);
1508 int relayd_trace_chunk_exists(struct lttcomm_relayd_sock
*sock
,
1513 struct lttcomm_relayd_trace_chunk_exists msg
= {};
1514 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
1516 if (!relayd_supports_chunks(sock
)) {
1517 DBG("Refusing to check for trace chunk existence: relayd does not support chunks");
1518 /* The chunk will never exist */
1519 *chunk_exists
= false;
1523 msg
= (typeof(msg
)){
1524 .chunk_id
= htobe64(chunk_id
),
1527 ret
= send_command(sock
, RELAYD_TRACE_CHUNK_EXISTS
, &msg
, sizeof(msg
), 0);
1529 ERR("Failed to send trace chunk exists command to relay daemon");
1533 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1535 ERR("Failed to receive relay daemon trace chunk close command reply");
1539 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1540 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1542 ERR("Relayd trace chunk close replied error %d", reply
.generic
.ret_code
);
1545 DBG("Relayd successfully checked trace chunk existence: chunk_id = %" PRIu64
1548 reply
.trace_chunk_exists
? "true" : "false");
1549 *chunk_exists
= !!reply
.trace_chunk_exists
;
1555 int relayd_get_configuration(struct lttcomm_relayd_sock
*sock
,
1556 uint64_t query_flags
,
1557 uint64_t *result_flags
)
1560 struct lttcomm_relayd_get_configuration msg
= (typeof(msg
)){
1561 .query_flags
= htobe64(query_flags
),
1563 struct lttcomm_relayd_get_configuration_reply reply
= {};
1565 if (!relayd_supports_get_configuration(sock
)) {
1566 DBG("Refusing to get relayd configuration (unsupported by relayd)");
1575 ret
= send_command(sock
, RELAYD_GET_CONFIGURATION
, &msg
, sizeof(msg
), 0);
1577 ERR("Failed to send get configuration command to relay daemon");
1581 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1583 ERR("Failed to receive relay daemon get configuration command reply");
1587 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1588 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1590 ERR("Relayd get configuration replied error %d", reply
.generic
.ret_code
);
1592 reply
.relayd_configuration_flags
= be64toh(reply
.relayd_configuration_flags
);
1594 DBG("Relayd successfully got configuration: query_flags = %" PRIu64
1595 ", results_flags = %" PRIu64
,
1597 reply
.relayd_configuration_flags
);
1598 *result_flags
= reply
.relayd_configuration_flags
;