2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <common/common.h>
27 #include <common/defaults.h>
28 #include <common/compat/endian.h>
29 #include <common/compat/string.h>
30 #include <common/sessiond-comm/relayd.h>
31 #include <common/index/ctf-index.h>
32 #include <common/trace-chunk.h>
33 #include <common/string-utils/format.h>
38 bool relayd_supports_chunks(const struct lttcomm_relayd_sock
*sock
)
40 if (sock
->major
> 2) {
42 } else if (sock
->major
== 2 && sock
->minor
>= 11) {
49 * 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
= zmalloc(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 %d of size %" PRIu64
, (int) cmd
, buf_size
);
90 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
92 PERROR("Failed to send command %d of size %" PRIu64
,
104 * Receive reply data on socket. This MUST be call after send_command or else
105 * could result in unexpected behavior(s).
107 static int recv_reply(struct lttcomm_relayd_sock
*rsock
, void *data
, size_t size
)
111 if (rsock
->sock
.fd
< 0) {
115 DBG3("Relayd waiting for reply of size %zu", size
);
117 ret
= rsock
->sock
.ops
->recvmsg(&rsock
->sock
, data
, size
, 0);
118 if (ret
<= 0 || ret
!= size
) {
120 /* Orderly shutdown. */
121 DBG("Socket %d has performed an orderly shutdown", rsock
->sock
.fd
);
123 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
124 rsock
->sock
.fd
, size
, ret
);
126 /* Always return -1 here and the caller can use errno. */
136 * Starting from 2.11, RELAYD_CREATE_SESSION payload (session_name,
137 * hostname, and base_path) have no length restriction on the sender side.
138 * Length for both payloads is stored in the msg struct. A new dynamic size
139 * payload size is introduced.
141 static int relayd_create_session_2_11(struct lttcomm_relayd_sock
*rsock
,
142 const char *session_name
, const char *hostname
,
143 const char *base_path
, int session_live_timer
,
144 unsigned int snapshot
, uint64_t sessiond_session_id
,
145 const lttng_uuid sessiond_uuid
, const uint64_t *current_chunk_id
,
146 time_t creation_time
, bool session_name_contains_creation_time
)
149 struct lttcomm_relayd_create_session_2_11
*msg
= NULL
;
150 size_t session_name_len
;
152 size_t base_path_len
;
159 /* The three names are sent with a '\0' delimiter between them. */
160 session_name_len
= strlen(session_name
) + 1;
161 hostname_len
= strlen(hostname
) + 1;
162 base_path_len
= base_path
? strlen(base_path
) + 1 : 0;
164 msg_length
= sizeof(*msg
) + session_name_len
+ hostname_len
+ base_path_len
;
165 msg
= zmalloc(msg_length
);
167 PERROR("zmalloc create_session_2_11 command message");
172 assert(session_name_len
<= UINT32_MAX
);
173 msg
->session_name_len
= htobe32(session_name_len
);
175 assert(hostname_len
<= UINT32_MAX
);
176 msg
->hostname_len
= htobe32(hostname_len
);
178 assert(base_path_len
<= UINT32_MAX
);
179 msg
->base_path_len
= htobe32(base_path_len
);
182 if (lttng_strncpy(dst
, session_name
, session_name_len
)) {
186 dst
+= session_name_len
;
187 if (lttng_strncpy(dst
, hostname
, hostname_len
)) {
192 if (base_path
&& lttng_strncpy(dst
, base_path
, base_path_len
)) {
197 msg
->live_timer
= htobe32(session_live_timer
);
198 msg
->snapshot
= !!snapshot
;
200 lttng_uuid_copy(msg
->sessiond_uuid
, sessiond_uuid
);
201 msg
->session_id
= htobe64(sessiond_session_id
);
202 msg
->session_name_contains_creation_time
= session_name_contains_creation_time
;
203 if (current_chunk_id
) {
204 LTTNG_OPTIONAL_SET(&msg
->current_chunk_id
,
205 htobe64(*current_chunk_id
));
208 msg
->creation_time
= htobe64((uint64_t) creation_time
);
211 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, msg
, msg_length
, 0);
220 * From 2.4 to 2.10, RELAYD_CREATE_SESSION takes additional parameters to
221 * support the live reading capability.
223 static int relayd_create_session_2_4(struct lttcomm_relayd_sock
*rsock
,
224 const char *session_name
, const char *hostname
,
225 int session_live_timer
, unsigned int snapshot
)
228 struct lttcomm_relayd_create_session_2_4 msg
;
230 if (lttng_strncpy(msg
.session_name
, session_name
,
231 sizeof(msg
.session_name
))) {
235 if (lttng_strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
))) {
239 msg
.live_timer
= htobe32(session_live_timer
);
240 msg
.snapshot
= htobe32(snapshot
);
243 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, &msg
, sizeof(msg
), 0);
253 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
255 static int relayd_create_session_2_1(struct lttcomm_relayd_sock
*rsock
)
260 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
270 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
271 * set session_id of the relayd if we have a successful reply from the relayd.
273 * On success, return 0 else a negative value which is either an errno error or
274 * a lttng error code from the relayd.
276 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
,
277 uint64_t *relayd_session_id
,
278 const char *session_name
, const char *hostname
,
279 const char *base_path
, int session_live_timer
,
280 unsigned int snapshot
, uint64_t sessiond_session_id
,
281 const lttng_uuid sessiond_uuid
,
282 const uint64_t *current_chunk_id
,
283 time_t creation_time
, bool session_name_contains_creation_time
)
286 struct lttcomm_relayd_status_session reply
;
289 assert(relayd_session_id
);
291 DBG("Relayd create session");
293 if (rsock
->minor
< 4) {
294 /* From 2.1 to 2.3 */
295 ret
= relayd_create_session_2_1(rsock
);
296 } else if (rsock
->minor
>= 4 && rsock
->minor
< 11) {
297 /* From 2.4 to 2.10 */
298 ret
= relayd_create_session_2_4(rsock
, session_name
,
299 hostname
, session_live_timer
, snapshot
);
301 /* From 2.11 to ... */
302 ret
= relayd_create_session_2_11(rsock
, session_name
,
303 hostname
, base_path
, session_live_timer
, snapshot
,
304 sessiond_session_id
, sessiond_uuid
,
305 current_chunk_id
, creation_time
,
306 session_name_contains_creation_time
);
313 /* Receive response */
314 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
319 reply
.session_id
= be64toh(reply
.session_id
);
320 reply
.ret_code
= be32toh(reply
.ret_code
);
322 /* Return session id or negative ret code. */
323 if (reply
.ret_code
!= LTTNG_OK
) {
325 ERR("Relayd create session replied error %d", reply
.ret_code
);
329 *relayd_session_id
= reply
.session_id
;
332 DBG("Relayd session created with id %" PRIu64
, reply
.session_id
);
338 static int relayd_add_stream_2_1(struct lttcomm_relayd_sock
*rsock
,
339 const char *channel_name
, const char *pathname
)
342 struct lttcomm_relayd_add_stream msg
;
344 memset(&msg
, 0, sizeof(msg
));
345 if (lttng_strncpy(msg
.channel_name
, channel_name
,
346 sizeof(msg
.channel_name
))) {
351 if (lttng_strncpy(msg
.pathname
, pathname
,
352 sizeof(msg
.pathname
))) {
358 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
368 static int relayd_add_stream_2_2(struct lttcomm_relayd_sock
*rsock
,
369 const char *channel_name
, const char *pathname
,
370 uint64_t tracefile_size
, uint64_t tracefile_count
)
373 struct lttcomm_relayd_add_stream_2_2 msg
;
375 memset(&msg
, 0, sizeof(msg
));
376 /* Compat with relayd 2.2 to 2.10 */
377 if (lttng_strncpy(msg
.channel_name
, channel_name
,
378 sizeof(msg
.channel_name
))) {
382 if (lttng_strncpy(msg
.pathname
, pathname
,
383 sizeof(msg
.pathname
))) {
387 msg
.tracefile_size
= htobe64(tracefile_size
);
388 msg
.tracefile_count
= htobe64(tracefile_count
);
391 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
400 static int relayd_add_stream_2_11(struct lttcomm_relayd_sock
*rsock
,
401 const char *channel_name
, const char *pathname
,
402 uint64_t tracefile_size
, uint64_t tracefile_count
,
403 uint64_t trace_archive_id
)
406 struct lttcomm_relayd_add_stream_2_11
*msg
= NULL
;
407 size_t channel_name_len
;
411 /* The two names are sent with a '\0' delimiter between them. */
412 channel_name_len
= strlen(channel_name
) + 1;
413 pathname_len
= strlen(pathname
) + 1;
415 msg_length
= sizeof(*msg
) + channel_name_len
+ pathname_len
;
416 msg
= zmalloc(msg_length
);
418 PERROR("zmalloc add_stream_2_11 command message");
423 assert(channel_name_len
<= UINT32_MAX
);
424 msg
->channel_name_len
= htobe32(channel_name_len
);
426 assert(pathname_len
<= UINT32_MAX
);
427 msg
->pathname_len
= htobe32(pathname_len
);
429 if (lttng_strncpy(msg
->names
, channel_name
, channel_name_len
)) {
433 if (lttng_strncpy(msg
->names
+ channel_name_len
, pathname
, pathname_len
)) {
438 msg
->tracefile_size
= htobe64(tracefile_size
);
439 msg
->tracefile_count
= htobe64(tracefile_count
);
440 msg
->trace_chunk_id
= htobe64(trace_archive_id
);
443 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) msg
, msg_length
, 0);
454 * Add stream on the relayd and assign stream handle to the stream_id argument.
456 * Chunks are not supported by relayd prior to 2.11, but are used to
457 * internally between session daemon and consumer daemon to keep track
458 * of the channel and stream output path.
460 * On success return 0 else return ret_code negative value.
462 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
463 const char *pathname
, uint64_t *stream_id
,
464 uint64_t tracefile_size
, uint64_t tracefile_count
,
465 struct lttng_trace_chunk
*trace_chunk
)
468 struct lttcomm_relayd_status_stream reply
;
470 /* Code flow error. Safety net. */
472 assert(channel_name
);
476 DBG("Relayd adding stream for channel name %s", channel_name
);
478 /* Compat with relayd 2.1 */
479 if (rsock
->minor
== 1) {
481 ret
= relayd_add_stream_2_1(rsock
, channel_name
, pathname
);
483 } else if (rsock
->minor
> 1 && rsock
->minor
< 11) {
484 /* From 2.2 to 2.10 */
485 ret
= relayd_add_stream_2_2(rsock
, channel_name
, pathname
,
486 tracefile_size
, tracefile_count
);
488 enum lttng_trace_chunk_status chunk_status
;
491 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
,
493 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
495 /* From 2.11 to ...*/
496 ret
= relayd_add_stream_2_11(rsock
, channel_name
, pathname
,
497 tracefile_size
, tracefile_count
,
506 /* Waiting for reply */
507 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
512 /* Back to host bytes order. */
513 reply
.handle
= be64toh(reply
.handle
);
514 reply
.ret_code
= be32toh(reply
.ret_code
);
516 /* Return session id or negative ret code. */
517 if (reply
.ret_code
!= LTTNG_OK
) {
519 ERR("Relayd add stream replied error %d", reply
.ret_code
);
523 *stream_id
= reply
.handle
;
526 DBG("Relayd stream added successfully with handle %" PRIu64
,
534 * Inform the relay that all the streams for the current channel has been sent.
536 * On success return 0 else return ret_code negative value.
538 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
541 struct lttcomm_relayd_generic_reply reply
;
543 /* Code flow error. Safety net. */
546 DBG("Relayd sending streams sent.");
548 /* This feature was introduced in 2.4, ignore it for earlier versions. */
549 if (rsock
->minor
< 4) {
555 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, NULL
, 0, 0);
560 /* Waiting for reply */
561 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
566 /* Back to host bytes order. */
567 reply
.ret_code
= be32toh(reply
.ret_code
);
569 /* Return session id or negative ret code. */
570 if (reply
.ret_code
!= LTTNG_OK
) {
572 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
579 DBG("Relayd streams sent success");
587 * Check version numbers on the relayd.
588 * If major versions are compatible, we assign minor_to_use to the
589 * minor version of the procotol we are going to use for this session.
591 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
592 * otherwise, or a negative value on network errors.
594 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
597 struct lttcomm_relayd_version msg
;
599 /* Code flow error. Safety net. */
602 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
605 memset(&msg
, 0, sizeof(msg
));
606 /* Prepare network byte order before transmission. */
607 msg
.major
= htobe32(rsock
->major
);
608 msg
.minor
= htobe32(rsock
->minor
);
611 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
616 /* Receive response */
617 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
622 /* Set back to host bytes order */
623 msg
.major
= be32toh(msg
.major
);
624 msg
.minor
= be32toh(msg
.minor
);
627 * Only validate the major version. If the other side is higher,
628 * communication is not possible. Only major version equal can talk to each
629 * other. If the minor version differs, the lowest version is used by both
632 if (msg
.major
!= rsock
->major
) {
634 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
635 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
636 msg
.major
, rsock
->major
);
641 * If the relayd's minor version is higher, it will adapt to our version so
642 * we can continue to use the latest relayd communication data structure.
643 * If the received minor version is higher, the relayd should adapt to us.
645 if (rsock
->minor
> msg
.minor
) {
646 rsock
->minor
= msg
.minor
;
649 /* Version number compatible */
650 DBG2("Relayd version is compatible, using protocol version %u.%u",
651 rsock
->major
, rsock
->minor
);
659 * Add stream on the relayd and assign stream handle to the stream_id argument.
661 * On success return 0 else return ret_code negative value.
663 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
667 /* Code flow error. Safety net. */
670 DBG("Relayd sending metadata of size %zu", len
);
673 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
678 DBG2("Relayd metadata added successfully");
681 * After that call, the metadata data MUST be sent to the relayd so the
682 * receive size on the other end matches the len of the metadata packet
683 * header. This is why we don't wait for a reply here.
691 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
693 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
695 /* Code flow error. Safety net. */
698 if (!rsock
->sock
.ops
) {
700 * Attempting a connect on a non-initialized socket.
705 DBG3("Relayd connect ...");
707 return rsock
->sock
.ops
->connect(&rsock
->sock
);
711 * Close relayd socket with an allocated lttcomm_relayd_sock.
713 * If no socket operations are found, simply return 0 meaning that everything
714 * is fine. Without operations, the socket can not possibly be opened or used.
715 * This is possible if the socket was allocated but not created. However, the
716 * caller could simply use it to store a valid file descriptor for instance
717 * passed over a Unix socket and call this to cleanup but still without a valid
720 * Return the close returned value. On error, a negative value is usually
721 * returned back from close(2).
723 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
727 /* Code flow error. Safety net. */
730 /* An invalid fd is fine, return success. */
731 if (rsock
->sock
.fd
< 0) {
736 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
738 if (rsock
->sock
.ops
) {
739 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
741 /* Default call if no specific ops found. */
742 ret
= close(rsock
->sock
.fd
);
744 PERROR("relayd_close default close");
754 * Send data header structure to the relayd.
756 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
757 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
761 /* Code flow error. Safety net. */
765 if (rsock
->sock
.fd
< 0) {
769 DBG3("Relayd sending data header of size %zu", size
);
771 /* Again, safety net */
773 size
= sizeof(struct lttcomm_relayd_data_hdr
);
776 /* Only send data header. */
777 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
784 * The data MUST be sent right after that command for the receive on the
785 * other end to match the size in the header.
793 * Send close stream command to the relayd.
795 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
796 uint64_t last_net_seq_num
)
799 struct lttcomm_relayd_close_stream msg
;
800 struct lttcomm_relayd_generic_reply reply
;
802 /* Code flow error. Safety net. */
805 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
807 memset(&msg
, 0, sizeof(msg
));
808 msg
.stream_id
= htobe64(stream_id
);
809 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
812 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
817 /* Receive response */
818 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
823 reply
.ret_code
= be32toh(reply
.ret_code
);
825 /* Return session id or negative ret code. */
826 if (reply
.ret_code
!= LTTNG_OK
) {
828 ERR("Relayd close stream replied error %d", reply
.ret_code
);
834 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
841 * Check for data availability for a given stream id.
843 * Return 0 if NOT pending, 1 if so and a negative value on error.
845 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
846 uint64_t last_net_seq_num
)
849 struct lttcomm_relayd_data_pending msg
;
850 struct lttcomm_relayd_generic_reply reply
;
852 /* Code flow error. Safety net. */
855 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
857 memset(&msg
, 0, sizeof(msg
));
858 msg
.stream_id
= htobe64(stream_id
);
859 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
862 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
868 /* Receive response */
869 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
874 reply
.ret_code
= be32toh(reply
.ret_code
);
876 /* Return session id or negative ret code. */
877 if (reply
.ret_code
>= LTTNG_OK
) {
878 ERR("Relayd data pending replied error %d", reply
.ret_code
);
881 /* At this point, the ret code is either 1 or 0 */
882 ret
= reply
.ret_code
;
884 DBG("Relayd data is %s pending for stream id %" PRIu64
,
885 ret
== 1 ? "" : "NOT", stream_id
);
892 * Check on the relayd side for a quiescent state on the control socket.
894 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
895 uint64_t metadata_stream_id
)
898 struct lttcomm_relayd_quiescent_control msg
;
899 struct lttcomm_relayd_generic_reply reply
;
901 /* Code flow error. Safety net. */
904 DBG("Relayd checking quiescent control state");
906 memset(&msg
, 0, sizeof(msg
));
907 msg
.stream_id
= htobe64(metadata_stream_id
);
910 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
915 /* Receive response */
916 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
921 reply
.ret_code
= be32toh(reply
.ret_code
);
923 /* Return session id or negative ret code. */
924 if (reply
.ret_code
!= LTTNG_OK
) {
926 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
930 /* Control socket is quiescent */
938 * Begin a data pending command for a specific session id.
940 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
943 struct lttcomm_relayd_begin_data_pending msg
;
944 struct lttcomm_relayd_generic_reply reply
;
946 /* Code flow error. Safety net. */
949 DBG("Relayd begin data pending");
951 memset(&msg
, 0, sizeof(msg
));
952 msg
.session_id
= htobe64(id
);
955 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
960 /* Receive response */
961 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
966 reply
.ret_code
= be32toh(reply
.ret_code
);
968 /* Return session id or negative ret code. */
969 if (reply
.ret_code
!= LTTNG_OK
) {
971 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
982 * End a data pending command for a specific session id.
984 * Return 0 on success and set is_data_inflight to 0 if no data is being
985 * streamed or 1 if it is the case.
987 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
988 unsigned int *is_data_inflight
)
991 struct lttcomm_relayd_end_data_pending msg
;
992 struct lttcomm_relayd_generic_reply reply
;
994 /* Code flow error. Safety net. */
997 DBG("Relayd end data pending");
999 memset(&msg
, 0, sizeof(msg
));
1000 msg
.session_id
= htobe64(id
);
1003 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
1008 /* Receive response */
1009 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1014 recv_ret
= be32toh(reply
.ret_code
);
1020 *is_data_inflight
= recv_ret
;
1022 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
1031 * Send index to the relayd.
1033 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
1034 struct ctf_packet_index
*index
, uint64_t relay_stream_id
,
1035 uint64_t net_seq_num
)
1038 struct lttcomm_relayd_index msg
;
1039 struct lttcomm_relayd_generic_reply reply
;
1041 /* Code flow error. Safety net. */
1044 if (rsock
->minor
< 4) {
1045 DBG("Not sending indexes before protocol 2.4");
1050 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
1052 memset(&msg
, 0, sizeof(msg
));
1053 msg
.relay_stream_id
= htobe64(relay_stream_id
);
1054 msg
.net_seq_num
= htobe64(net_seq_num
);
1056 /* The index is already in big endian. */
1057 msg
.packet_size
= index
->packet_size
;
1058 msg
.content_size
= index
->content_size
;
1059 msg
.timestamp_begin
= index
->timestamp_begin
;
1060 msg
.timestamp_end
= index
->timestamp_end
;
1061 msg
.events_discarded
= index
->events_discarded
;
1062 msg
.stream_id
= index
->stream_id
;
1064 if (rsock
->minor
>= 8) {
1065 msg
.stream_instance_id
= index
->stream_instance_id
;
1066 msg
.packet_seq_num
= index
->packet_seq_num
;
1070 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
,
1071 lttcomm_relayd_index_len(lttng_to_index_major(rsock
->major
,
1073 lttng_to_index_minor(rsock
->major
, rsock
->minor
)),
1079 /* Receive response */
1080 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1085 reply
.ret_code
= be32toh(reply
.ret_code
);
1087 /* Return session id or negative ret code. */
1088 if (reply
.ret_code
!= LTTNG_OK
) {
1090 ERR("Relayd send index replied error %d", reply
.ret_code
);
1101 * Ask the relay to reset the metadata trace file (regeneration).
1103 int relayd_reset_metadata(struct lttcomm_relayd_sock
*rsock
,
1104 uint64_t stream_id
, uint64_t version
)
1107 struct lttcomm_relayd_reset_metadata msg
;
1108 struct lttcomm_relayd_generic_reply reply
;
1110 /* Code flow error. Safety net. */
1113 /* Should have been prevented by the sessiond. */
1114 if (rsock
->minor
< 8) {
1115 ERR("Metadata regeneration unsupported before 2.8");
1120 DBG("Relayd reset metadata stream id %" PRIu64
, stream_id
);
1122 memset(&msg
, 0, sizeof(msg
));
1123 msg
.stream_id
= htobe64(stream_id
);
1124 msg
.version
= htobe64(version
);
1127 ret
= send_command(rsock
, RELAYD_RESET_METADATA
, (void *) &msg
, sizeof(msg
), 0);
1132 /* Receive response */
1133 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1138 reply
.ret_code
= be32toh(reply
.ret_code
);
1140 /* Return session id or negative ret code. */
1141 if (reply
.ret_code
!= LTTNG_OK
) {
1143 ERR("Relayd reset metadata replied error %d", reply
.ret_code
);
1149 DBG("Relayd reset metadata stream id %" PRIu64
" successfully", stream_id
);
1155 int relayd_rotate_streams(struct lttcomm_relayd_sock
*sock
,
1156 unsigned int stream_count
, const uint64_t *new_chunk_id
,
1157 const struct relayd_stream_rotation_position
*positions
)
1161 struct lttng_dynamic_buffer payload
;
1162 struct lttcomm_relayd_generic_reply reply
= {};
1163 const struct lttcomm_relayd_rotate_streams msg
= {
1164 .stream_count
= htobe32((uint32_t) stream_count
),
1165 .new_chunk_id
= (typeof(msg
.new_chunk_id
)) {
1166 .is_set
= !!new_chunk_id
,
1167 .value
= htobe64(new_chunk_id
? *new_chunk_id
: 0),
1170 char new_chunk_id_buf
[MAX_INT_DEC_LEN(*new_chunk_id
)] = {};
1171 const char *new_chunk_id_str
;
1173 if (!relayd_supports_chunks(sock
)) {
1174 DBG("Refusing to rotate remote streams: relayd does not support chunks");
1178 lttng_dynamic_buffer_init(&payload
);
1180 /* Code flow error. Safety net. */
1184 ret
= snprintf(new_chunk_id_buf
, sizeof(new_chunk_id_buf
),
1185 "%" PRIu64
, *new_chunk_id
);
1186 if (ret
== -1 || ret
>= sizeof(new_chunk_id_buf
)) {
1187 new_chunk_id_str
= "formatting error";
1189 new_chunk_id_str
= new_chunk_id_buf
;
1192 new_chunk_id_str
= "none";
1195 DBG("Preparing \"rotate streams\" command payload: new_chunk_id = %s, stream_count = %u",
1196 new_chunk_id_str
, stream_count
);
1198 ret
= lttng_dynamic_buffer_append(&payload
, &msg
, sizeof(msg
));
1200 ERR("Failed to allocate \"rotate streams\" command payload");
1204 for (i
= 0; i
< stream_count
; i
++) {
1205 const struct relayd_stream_rotation_position
*position
=
1207 const struct lttcomm_relayd_stream_rotation_position comm_position
= {
1208 .stream_id
= htobe64(position
->stream_id
),
1209 .rotate_at_seq_num
= htobe64(
1210 position
->rotate_at_seq_num
),
1213 DBG("Rotate stream %" PRIu64
"at sequence number %" PRIu64
,
1214 position
->stream_id
,
1215 position
->rotate_at_seq_num
);
1216 ret
= lttng_dynamic_buffer_append(&payload
, &comm_position
,
1217 sizeof(comm_position
));
1219 ERR("Failed to allocate \"rotate streams\" command payload");
1225 ret
= send_command(sock
, RELAYD_ROTATE_STREAMS
, payload
.data
,
1228 ERR("Failed to send \"rotate stream\" command");
1232 /* Receive response. */
1233 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1235 ERR("Failed to receive \"rotate streams\" command reply");
1239 reply
.ret_code
= be32toh(reply
.ret_code
);
1240 if (reply
.ret_code
!= LTTNG_OK
) {
1242 ERR("Relayd rotate streams replied error %d", reply
.ret_code
);
1246 DBG("Relayd rotated streams successfully");
1250 lttng_dynamic_buffer_reset(&payload
);
1254 int relayd_create_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1255 struct lttng_trace_chunk
*chunk
)
1258 enum lttng_trace_chunk_status status
;
1259 struct lttcomm_relayd_create_trace_chunk msg
= {};
1260 struct lttcomm_relayd_generic_reply reply
= {};
1261 struct lttng_dynamic_buffer payload
;
1263 time_t creation_timestamp
;
1264 const char *chunk_name
;
1265 size_t chunk_name_length
;
1266 bool overridden_name
;
1268 lttng_dynamic_buffer_init(&payload
);
1270 if (!relayd_supports_chunks(sock
)) {
1271 DBG("Refusing to create remote trace chunk: relayd does not support chunks");
1275 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1276 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1281 status
= lttng_trace_chunk_get_creation_timestamp(
1282 chunk
, &creation_timestamp
);
1283 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1288 status
= lttng_trace_chunk_get_name(
1289 chunk
, &chunk_name
, &overridden_name
);
1290 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1291 status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1296 chunk_name_length
= overridden_name
? (strlen(chunk_name
) + 1) : 0;
1297 msg
= (typeof(msg
)){
1298 .chunk_id
= htobe64(chunk_id
),
1299 .creation_timestamp
= htobe64((uint64_t) creation_timestamp
),
1300 .override_name_length
= htobe32((uint32_t) chunk_name_length
),
1303 ret
= lttng_dynamic_buffer_append(&payload
, &msg
, sizeof(msg
));
1307 if (chunk_name_length
) {
1308 ret
= lttng_dynamic_buffer_append(
1309 &payload
, chunk_name
, chunk_name_length
);
1315 ret
= send_command(sock
, RELAYD_CREATE_TRACE_CHUNK
, payload
.data
,
1318 ERR("Failed to send trace chunk creation command to relay daemon");
1322 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1324 ERR("Failed to receive relay daemon trace chunk creation command reply");
1328 reply
.ret_code
= be32toh(reply
.ret_code
);
1329 if (reply
.ret_code
!= LTTNG_OK
) {
1331 ERR("Relayd trace chunk create replied error %d",
1335 DBG("Relayd successfully created trace chunk: chunk_id = %" PRIu64
,
1340 lttng_dynamic_buffer_reset(&payload
);
1344 int relayd_close_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1345 struct lttng_trace_chunk
*chunk
)
1348 enum lttng_trace_chunk_status status
;
1349 struct lttcomm_relayd_close_trace_chunk msg
= {};
1350 struct lttcomm_relayd_generic_reply reply
= {};
1352 time_t close_timestamp
;
1353 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
1355 if (!relayd_supports_chunks(sock
)) {
1356 DBG("Refusing to close remote trace chunk: relayd does not support chunks");
1360 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1361 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1362 ERR("Failed to get trace chunk id");
1367 status
= lttng_trace_chunk_get_close_timestamp(chunk
, &close_timestamp
);
1368 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1369 ERR("Failed to get trace chunk close timestamp");
1374 status
= lttng_trace_chunk_get_close_command(chunk
,
1375 &close_command
.value
);
1377 case LTTNG_TRACE_CHUNK_STATUS_OK
:
1378 close_command
.is_set
= 1;
1380 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
1383 ERR("Failed to get trace chunk close command");
1388 msg
= (typeof(msg
)){
1389 .chunk_id
= htobe64(chunk_id
),
1390 .close_timestamp
= htobe64((uint64_t) close_timestamp
),
1392 .value
= htobe32((uint32_t) close_command
.value
),
1393 .is_set
= close_command
.is_set
,
1397 ret
= send_command(sock
, RELAYD_CLOSE_TRACE_CHUNK
, &msg
, sizeof(msg
),
1400 ERR("Failed to send trace chunk close command to relay daemon");
1404 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1406 ERR("Failed to receive relay daemon trace chunk close command reply");
1410 reply
.ret_code
= be32toh(reply
.ret_code
);
1411 if (reply
.ret_code
!= LTTNG_OK
) {
1413 ERR("Relayd trace chunk close replied error %d",
1417 DBG("Relayd successfully closed trace chunk: chunk_id = %" PRIu64
,
1424 int relayd_trace_chunk_exists(struct lttcomm_relayd_sock
*sock
,
1425 uint64_t chunk_id
, bool *chunk_exists
)
1428 struct lttcomm_relayd_trace_chunk_exists msg
= {};
1429 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
1431 if (!relayd_supports_chunks(sock
)) {
1432 DBG("Refusing to check for trace chunk existence: relayd does not support chunks");
1436 msg
= (typeof(msg
)){
1437 .chunk_id
= htobe64(chunk_id
),
1440 ret
= send_command(sock
, RELAYD_TRACE_CHUNK_EXISTS
, &msg
, sizeof(msg
),
1443 ERR("Failed to send trace chunk exists command to relay daemon");
1447 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1449 ERR("Failed to receive relay daemon trace chunk close command reply");
1453 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1454 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1456 ERR("Relayd trace chunk close replied error %d",
1457 reply
.generic
.ret_code
);
1460 DBG("Relayd successfully checked trace chunk existence: chunk_id = %" PRIu64
1461 ", exists = %s", chunk_id
,
1462 reply
.trace_chunk_exists
? "true" : "false");
1463 *chunk_exists
= !!reply
.trace_chunk_exists
;