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 * Send command. Fill up the header and append the data.
40 static int send_command(struct lttcomm_relayd_sock
*rsock
,
41 enum lttcomm_relayd_command cmd
, const void *data
, size_t size
,
45 struct lttcomm_relayd_hdr header
;
47 uint64_t buf_size
= sizeof(header
);
49 if (rsock
->sock
.fd
< 0) {
57 buf
= zmalloc(buf_size
);
59 PERROR("zmalloc relayd send command buf");
64 memset(&header
, 0, sizeof(header
));
65 header
.cmd
= htobe32(cmd
);
66 header
.data_size
= htobe64(size
);
68 /* Zeroed for now since not used. */
69 header
.cmd_version
= 0;
70 header
.circuit_id
= 0;
72 /* Prepare buffer to send. */
73 memcpy(buf
, &header
, sizeof(header
));
75 memcpy(buf
+ sizeof(header
), data
, size
);
78 DBG3("Relayd sending command %d of size %" PRIu64
, (int) cmd
, buf_size
);
79 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
81 PERROR("Failed to send command %d of size %" PRIu64
,
93 * Receive reply data on socket. This MUST be call after send_command or else
94 * could result in unexpected behavior(s).
96 static int recv_reply(struct lttcomm_relayd_sock
*rsock
, void *data
, size_t size
)
100 if (rsock
->sock
.fd
< 0) {
104 DBG3("Relayd waiting for reply of size %zu", size
);
106 ret
= rsock
->sock
.ops
->recvmsg(&rsock
->sock
, data
, size
, 0);
107 if (ret
<= 0 || ret
!= size
) {
109 /* Orderly shutdown. */
110 DBG("Socket %d has performed an orderly shutdown", rsock
->sock
.fd
);
112 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
113 rsock
->sock
.fd
, size
, ret
);
115 /* Always return -1 here and the caller can use errno. */
125 * Starting from 2.11, RELAYD_CREATE_SESSION payload (session_name,
126 * hostname, and base_path) have no length restriction on the sender side.
127 * Length for both payloads is stored in the msg struct. A new dynamic size
128 * payload size is introduced.
130 static int relayd_create_session_2_11(struct lttcomm_relayd_sock
*rsock
,
131 const char *session_name
, const char *hostname
,
132 const char *base_path
, int session_live_timer
,
133 unsigned int snapshot
, uint64_t sessiond_session_id
,
134 const lttng_uuid sessiond_uuid
, const uint64_t *current_chunk_id
,
135 time_t creation_time
)
138 struct lttcomm_relayd_create_session_2_11
*msg
= NULL
;
139 size_t session_name_len
;
141 size_t base_path_len
;
145 /* The two names are sent with a '\0' delimiter between them. */
146 session_name_len
= strlen(session_name
) + 1;
147 hostname_len
= strlen(hostname
) + 1;
148 base_path_len
= base_path
? strlen(base_path
) + 1 : 0;
150 msg_length
= sizeof(*msg
) + session_name_len
+ hostname_len
+ base_path_len
;
151 msg
= zmalloc(msg_length
);
153 PERROR("zmalloc create_session_2_11 command message");
158 assert(session_name_len
<= UINT32_MAX
);
159 msg
->session_name_len
= htobe32(session_name_len
);
161 assert(hostname_len
<= UINT32_MAX
);
162 msg
->hostname_len
= htobe32(hostname_len
);
164 assert(base_path_len
<= UINT32_MAX
);
165 msg
->base_path_len
= htobe32(base_path_len
);
168 if (lttng_strncpy(dst
, session_name
, session_name_len
)) {
172 dst
+= session_name_len
;
173 if (lttng_strncpy(dst
, hostname
, hostname_len
)) {
178 if (base_path
&& lttng_strncpy(dst
, base_path
, base_path_len
)) {
183 msg
->live_timer
= htobe32(session_live_timer
);
184 msg
->snapshot
= !!snapshot
;
186 lttng_uuid_copy(msg
->sessiond_uuid
, sessiond_uuid
);
187 msg
->session_id
= htobe64(sessiond_session_id
);
189 if (current_chunk_id
) {
190 LTTNG_OPTIONAL_SET(&msg
->current_chunk_id
,
191 htobe64(*current_chunk_id
));
194 msg
->creation_time
= htobe64((uint64_t) creation_time
);
197 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, msg
, msg_length
, 0);
206 * From 2.4 to 2.10, RELAYD_CREATE_SESSION takes additional parameters to
207 * support the live reading capability.
209 static int relayd_create_session_2_4(struct lttcomm_relayd_sock
*rsock
,
210 const char *session_name
, const char *hostname
,
211 int session_live_timer
, unsigned int snapshot
)
214 struct lttcomm_relayd_create_session_2_4 msg
;
216 if (lttng_strncpy(msg
.session_name
, session_name
,
217 sizeof(msg
.session_name
))) {
221 if (lttng_strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
))) {
225 msg
.live_timer
= htobe32(session_live_timer
);
226 msg
.snapshot
= htobe32(snapshot
);
229 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, &msg
, sizeof(msg
), 0);
239 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
241 static int relayd_create_session_2_1(struct lttcomm_relayd_sock
*rsock
)
246 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
256 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
257 * set session_id of the relayd if we have a successful reply from the relayd.
259 * On success, return 0 else a negative value which is either an errno error or
260 * a lttng error code from the relayd.
262 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
,
263 uint64_t *relayd_session_id
,
264 const char *session_name
, const char *hostname
,
265 const char *base_path
, int session_live_timer
,
266 unsigned int snapshot
, uint64_t sessiond_session_id
,
267 const lttng_uuid sessiond_uuid
,
268 const uint64_t *current_chunk_id
,
269 time_t creation_time
)
272 struct lttcomm_relayd_status_session reply
;
275 assert(relayd_session_id
);
277 DBG("Relayd create session");
279 if (rsock
->minor
< 4) {
280 /* From 2.1 to 2.3 */
281 ret
= relayd_create_session_2_1(rsock
);
282 } else if (rsock
->minor
>= 4 && rsock
->minor
< 11) {
283 /* From 2.4 to 2.10 */
284 ret
= relayd_create_session_2_4(rsock
, session_name
,
285 hostname
, session_live_timer
, snapshot
);
287 /* From 2.11 to ... */
288 ret
= relayd_create_session_2_11(rsock
, session_name
,
289 hostname
, base_path
, session_live_timer
, snapshot
,
290 sessiond_session_id
, sessiond_uuid
,
291 current_chunk_id
, creation_time
);
298 /* Receive response */
299 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
304 reply
.session_id
= be64toh(reply
.session_id
);
305 reply
.ret_code
= be32toh(reply
.ret_code
);
307 /* Return session id or negative ret code. */
308 if (reply
.ret_code
!= LTTNG_OK
) {
310 ERR("Relayd create session replied error %d", reply
.ret_code
);
314 *relayd_session_id
= reply
.session_id
;
317 DBG("Relayd session created with id %" PRIu64
, reply
.session_id
);
323 static int relayd_add_stream_2_1(struct lttcomm_relayd_sock
*rsock
,
324 const char *channel_name
, const char *pathname
)
327 struct lttcomm_relayd_add_stream msg
;
329 memset(&msg
, 0, sizeof(msg
));
330 if (lttng_strncpy(msg
.channel_name
, channel_name
,
331 sizeof(msg
.channel_name
))) {
336 if (lttng_strncpy(msg
.pathname
, pathname
,
337 sizeof(msg
.pathname
))) {
343 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
353 static int relayd_add_stream_2_2(struct lttcomm_relayd_sock
*rsock
,
354 const char *channel_name
, const char *pathname
,
355 uint64_t tracefile_size
, uint64_t tracefile_count
)
358 struct lttcomm_relayd_add_stream_2_2 msg
;
360 memset(&msg
, 0, sizeof(msg
));
361 /* Compat with relayd 2.2 to 2.10 */
362 if (lttng_strncpy(msg
.channel_name
, channel_name
,
363 sizeof(msg
.channel_name
))) {
367 if (lttng_strncpy(msg
.pathname
, pathname
,
368 sizeof(msg
.pathname
))) {
372 msg
.tracefile_size
= htobe64(tracefile_size
);
373 msg
.tracefile_count
= htobe64(tracefile_count
);
376 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
385 static int relayd_add_stream_2_11(struct lttcomm_relayd_sock
*rsock
,
386 const char *channel_name
, const char *pathname
,
387 uint64_t tracefile_size
, uint64_t tracefile_count
,
388 uint64_t trace_archive_id
)
391 struct lttcomm_relayd_add_stream_2_11
*msg
= NULL
;
392 size_t channel_name_len
;
396 /* The two names are sent with a '\0' delimiter between them. */
397 channel_name_len
= strlen(channel_name
) + 1;
398 pathname_len
= strlen(pathname
) + 1;
400 msg_length
= sizeof(*msg
) + channel_name_len
+ pathname_len
;
401 msg
= zmalloc(msg_length
);
403 PERROR("zmalloc add_stream_2_11 command message");
408 assert(channel_name_len
<= UINT32_MAX
);
409 msg
->channel_name_len
= htobe32(channel_name_len
);
411 assert(pathname_len
<= UINT32_MAX
);
412 msg
->pathname_len
= htobe32(pathname_len
);
414 if (lttng_strncpy(msg
->names
, channel_name
, channel_name_len
)) {
418 if (lttng_strncpy(msg
->names
+ channel_name_len
, pathname
, pathname_len
)) {
423 msg
->tracefile_size
= htobe64(tracefile_size
);
424 msg
->tracefile_count
= htobe64(tracefile_count
);
425 msg
->trace_chunk_id
= htobe64(trace_archive_id
);
428 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) msg
, msg_length
, 0);
439 * Add stream on the relayd and assign stream handle to the stream_id argument.
441 * On success return 0 else return ret_code negative value.
443 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
444 const char *pathname
, uint64_t *stream_id
,
445 uint64_t tracefile_size
, uint64_t tracefile_count
,
446 struct lttng_trace_chunk
*trace_chunk
)
449 struct lttcomm_relayd_status_stream reply
;
451 /* Code flow error. Safety net. */
453 assert(channel_name
);
456 DBG("Relayd adding stream for channel name %s", channel_name
);
458 /* Compat with relayd 2.1 */
459 if (rsock
->minor
== 1) {
461 assert(!trace_chunk
);
462 ret
= relayd_add_stream_2_1(rsock
, channel_name
, pathname
);
464 } else if (rsock
->minor
> 1 && rsock
->minor
< 11) {
465 /* From 2.2 to 2.10 */
466 assert(!trace_chunk
);
467 ret
= relayd_add_stream_2_2(rsock
, channel_name
, pathname
,
468 tracefile_size
, tracefile_count
);
470 enum lttng_trace_chunk_status chunk_status
;
474 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
,
476 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
478 /* From 2.11 to ...*/
479 ret
= relayd_add_stream_2_11(rsock
, channel_name
, pathname
,
480 tracefile_size
, tracefile_count
,
489 /* Waiting for reply */
490 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
495 /* Back to host bytes order. */
496 reply
.handle
= be64toh(reply
.handle
);
497 reply
.ret_code
= be32toh(reply
.ret_code
);
499 /* Return session id or negative ret code. */
500 if (reply
.ret_code
!= LTTNG_OK
) {
502 ERR("Relayd add stream replied error %d", reply
.ret_code
);
506 *stream_id
= reply
.handle
;
509 DBG("Relayd stream added successfully with handle %" PRIu64
,
517 * Inform the relay that all the streams for the current channel has been sent.
519 * On success return 0 else return ret_code negative value.
521 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
524 struct lttcomm_relayd_generic_reply reply
;
526 /* Code flow error. Safety net. */
529 DBG("Relayd sending streams sent.");
531 /* This feature was introduced in 2.4, ignore it for earlier versions. */
532 if (rsock
->minor
< 4) {
538 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, NULL
, 0, 0);
543 /* Waiting for reply */
544 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
549 /* Back to host bytes order. */
550 reply
.ret_code
= be32toh(reply
.ret_code
);
552 /* Return session id or negative ret code. */
553 if (reply
.ret_code
!= LTTNG_OK
) {
555 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
562 DBG("Relayd streams sent success");
570 * Check version numbers on the relayd.
571 * If major versions are compatible, we assign minor_to_use to the
572 * minor version of the procotol we are going to use for this session.
574 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
575 * otherwise, or a negative value on network errors.
577 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
580 struct lttcomm_relayd_version msg
;
582 /* Code flow error. Safety net. */
585 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
588 memset(&msg
, 0, sizeof(msg
));
589 /* Prepare network byte order before transmission. */
590 msg
.major
= htobe32(rsock
->major
);
591 msg
.minor
= htobe32(rsock
->minor
);
594 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
599 /* Receive response */
600 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
605 /* Set back to host bytes order */
606 msg
.major
= be32toh(msg
.major
);
607 msg
.minor
= be32toh(msg
.minor
);
610 * Only validate the major version. If the other side is higher,
611 * communication is not possible. Only major version equal can talk to each
612 * other. If the minor version differs, the lowest version is used by both
615 if (msg
.major
!= rsock
->major
) {
617 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
618 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
619 msg
.major
, rsock
->major
);
624 * If the relayd's minor version is higher, it will adapt to our version so
625 * we can continue to use the latest relayd communication data structure.
626 * If the received minor version is higher, the relayd should adapt to us.
628 if (rsock
->minor
> msg
.minor
) {
629 rsock
->minor
= msg
.minor
;
632 /* Version number compatible */
633 DBG2("Relayd version is compatible, using protocol version %u.%u",
634 rsock
->major
, rsock
->minor
);
642 * Add stream on the relayd and assign stream handle to the stream_id argument.
644 * On success return 0 else return ret_code negative value.
646 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
650 /* Code flow error. Safety net. */
653 DBG("Relayd sending metadata of size %zu", len
);
656 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
661 DBG2("Relayd metadata added successfully");
664 * After that call, the metadata data MUST be sent to the relayd so the
665 * receive size on the other end matches the len of the metadata packet
666 * header. This is why we don't wait for a reply here.
674 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
676 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
678 /* Code flow error. Safety net. */
681 if (!rsock
->sock
.ops
) {
683 * Attempting a connect on a non-initialized socket.
688 DBG3("Relayd connect ...");
690 return rsock
->sock
.ops
->connect(&rsock
->sock
);
694 * Close relayd socket with an allocated lttcomm_relayd_sock.
696 * If no socket operations are found, simply return 0 meaning that everything
697 * is fine. Without operations, the socket can not possibly be opened or used.
698 * This is possible if the socket was allocated but not created. However, the
699 * caller could simply use it to store a valid file descriptor for instance
700 * passed over a Unix socket and call this to cleanup but still without a valid
703 * Return the close returned value. On error, a negative value is usually
704 * returned back from close(2).
706 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
710 /* Code flow error. Safety net. */
713 /* An invalid fd is fine, return success. */
714 if (rsock
->sock
.fd
< 0) {
719 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
721 if (rsock
->sock
.ops
) {
722 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
724 /* Default call if no specific ops found. */
725 ret
= close(rsock
->sock
.fd
);
727 PERROR("relayd_close default close");
737 * Send data header structure to the relayd.
739 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
740 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
744 /* Code flow error. Safety net. */
748 if (rsock
->sock
.fd
< 0) {
752 DBG3("Relayd sending data header of size %zu", size
);
754 /* Again, safety net */
756 size
= sizeof(struct lttcomm_relayd_data_hdr
);
759 /* Only send data header. */
760 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
767 * The data MUST be sent right after that command for the receive on the
768 * other end to match the size in the header.
776 * Send close stream command to the relayd.
778 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
779 uint64_t last_net_seq_num
)
782 struct lttcomm_relayd_close_stream msg
;
783 struct lttcomm_relayd_generic_reply reply
;
785 /* Code flow error. Safety net. */
788 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
790 memset(&msg
, 0, sizeof(msg
));
791 msg
.stream_id
= htobe64(stream_id
);
792 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
795 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
800 /* Receive response */
801 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
806 reply
.ret_code
= be32toh(reply
.ret_code
);
808 /* Return session id or negative ret code. */
809 if (reply
.ret_code
!= LTTNG_OK
) {
811 ERR("Relayd close stream replied error %d", reply
.ret_code
);
817 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
824 * Check for data availability for a given stream id.
826 * Return 0 if NOT pending, 1 if so and a negative value on error.
828 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
829 uint64_t last_net_seq_num
)
832 struct lttcomm_relayd_data_pending msg
;
833 struct lttcomm_relayd_generic_reply reply
;
835 /* Code flow error. Safety net. */
838 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
840 memset(&msg
, 0, sizeof(msg
));
841 msg
.stream_id
= htobe64(stream_id
);
842 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
845 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
851 /* Receive response */
852 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
857 reply
.ret_code
= be32toh(reply
.ret_code
);
859 /* Return session id or negative ret code. */
860 if (reply
.ret_code
>= LTTNG_OK
) {
861 ERR("Relayd data pending replied error %d", reply
.ret_code
);
864 /* At this point, the ret code is either 1 or 0 */
865 ret
= reply
.ret_code
;
867 DBG("Relayd data is %s pending for stream id %" PRIu64
,
868 ret
== 1 ? "" : "NOT", stream_id
);
875 * Check on the relayd side for a quiescent state on the control socket.
877 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
878 uint64_t metadata_stream_id
)
881 struct lttcomm_relayd_quiescent_control msg
;
882 struct lttcomm_relayd_generic_reply reply
;
884 /* Code flow error. Safety net. */
887 DBG("Relayd checking quiescent control state");
889 memset(&msg
, 0, sizeof(msg
));
890 msg
.stream_id
= htobe64(metadata_stream_id
);
893 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
898 /* Receive response */
899 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
904 reply
.ret_code
= be32toh(reply
.ret_code
);
906 /* Return session id or negative ret code. */
907 if (reply
.ret_code
!= LTTNG_OK
) {
909 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
913 /* Control socket is quiescent */
921 * Begin a data pending command for a specific session id.
923 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
926 struct lttcomm_relayd_begin_data_pending msg
;
927 struct lttcomm_relayd_generic_reply reply
;
929 /* Code flow error. Safety net. */
932 DBG("Relayd begin data pending");
934 memset(&msg
, 0, sizeof(msg
));
935 msg
.session_id
= htobe64(id
);
938 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
943 /* Receive response */
944 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
949 reply
.ret_code
= be32toh(reply
.ret_code
);
951 /* Return session id or negative ret code. */
952 if (reply
.ret_code
!= LTTNG_OK
) {
954 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
965 * End a data pending command for a specific session id.
967 * Return 0 on success and set is_data_inflight to 0 if no data is being
968 * streamed or 1 if it is the case.
970 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
971 unsigned int *is_data_inflight
)
974 struct lttcomm_relayd_end_data_pending msg
;
975 struct lttcomm_relayd_generic_reply reply
;
977 /* Code flow error. Safety net. */
980 DBG("Relayd end data pending");
982 memset(&msg
, 0, sizeof(msg
));
983 msg
.session_id
= htobe64(id
);
986 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
991 /* Receive response */
992 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
997 recv_ret
= be32toh(reply
.ret_code
);
1003 *is_data_inflight
= recv_ret
;
1005 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
1014 * Send index to the relayd.
1016 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
1017 struct ctf_packet_index
*index
, uint64_t relay_stream_id
,
1018 uint64_t net_seq_num
)
1021 struct lttcomm_relayd_index msg
;
1022 struct lttcomm_relayd_generic_reply reply
;
1024 /* Code flow error. Safety net. */
1027 if (rsock
->minor
< 4) {
1028 DBG("Not sending indexes before protocol 2.4");
1033 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
1035 memset(&msg
, 0, sizeof(msg
));
1036 msg
.relay_stream_id
= htobe64(relay_stream_id
);
1037 msg
.net_seq_num
= htobe64(net_seq_num
);
1039 /* The index is already in big endian. */
1040 msg
.packet_size
= index
->packet_size
;
1041 msg
.content_size
= index
->content_size
;
1042 msg
.timestamp_begin
= index
->timestamp_begin
;
1043 msg
.timestamp_end
= index
->timestamp_end
;
1044 msg
.events_discarded
= index
->events_discarded
;
1045 msg
.stream_id
= index
->stream_id
;
1047 if (rsock
->minor
>= 8) {
1048 msg
.stream_instance_id
= index
->stream_instance_id
;
1049 msg
.packet_seq_num
= index
->packet_seq_num
;
1053 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
,
1054 lttcomm_relayd_index_len(lttng_to_index_major(rsock
->major
,
1056 lttng_to_index_minor(rsock
->major
, rsock
->minor
)),
1062 /* Receive response */
1063 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1068 reply
.ret_code
= be32toh(reply
.ret_code
);
1070 /* Return session id or negative ret code. */
1071 if (reply
.ret_code
!= LTTNG_OK
) {
1073 ERR("Relayd send index replied error %d", reply
.ret_code
);
1084 * Ask the relay to reset the metadata trace file (regeneration).
1086 int relayd_reset_metadata(struct lttcomm_relayd_sock
*rsock
,
1087 uint64_t stream_id
, uint64_t version
)
1090 struct lttcomm_relayd_reset_metadata msg
;
1091 struct lttcomm_relayd_generic_reply reply
;
1093 /* Code flow error. Safety net. */
1096 /* Should have been prevented by the sessiond. */
1097 if (rsock
->minor
< 8) {
1098 ERR("Metadata regeneration unsupported before 2.8");
1103 DBG("Relayd reset metadata stream id %" PRIu64
, stream_id
);
1105 memset(&msg
, 0, sizeof(msg
));
1106 msg
.stream_id
= htobe64(stream_id
);
1107 msg
.version
= htobe64(version
);
1110 ret
= send_command(rsock
, RELAYD_RESET_METADATA
, (void *) &msg
, sizeof(msg
), 0);
1115 /* Receive response */
1116 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1121 reply
.ret_code
= be32toh(reply
.ret_code
);
1123 /* Return session id or negative ret code. */
1124 if (reply
.ret_code
!= LTTNG_OK
) {
1126 ERR("Relayd reset metadata replied error %d", reply
.ret_code
);
1132 DBG("Relayd reset metadata stream id %" PRIu64
" successfully", stream_id
);
1138 int relayd_rotate_streams(struct lttcomm_relayd_sock
*sock
,
1139 unsigned int stream_count
, const uint64_t *new_chunk_id
,
1140 const struct relayd_stream_rotation_position
*positions
)
1144 struct lttng_dynamic_buffer payload
;
1145 struct lttcomm_relayd_generic_reply reply
= {};
1146 const struct lttcomm_relayd_rotate_streams msg
= {
1147 .stream_count
= htobe32((uint32_t) stream_count
),
1148 .new_chunk_id
= (typeof(msg
.new_chunk_id
)) {
1149 .is_set
= !!new_chunk_id
,
1150 .value
= htobe64(new_chunk_id
? *new_chunk_id
: 0),
1153 char new_chunk_id_buf
[MAX_INT_DEC_LEN(*new_chunk_id
)] = {};
1154 const char *new_chunk_id_str
;
1156 lttng_dynamic_buffer_init(&payload
);
1158 /* Code flow error. Safety net. */
1162 ret
= snprintf(new_chunk_id_buf
, sizeof(new_chunk_id_buf
),
1163 "%" PRIu64
, *new_chunk_id
);
1164 if (ret
== -1 || ret
>= sizeof(new_chunk_id_buf
)) {
1165 new_chunk_id_str
= "formatting error";
1167 new_chunk_id_str
= new_chunk_id_buf
;
1170 new_chunk_id_str
= "none";
1173 DBG("Preparing \"rotate streams\" command payload: new_chunk_id = %s, stream_count = %u",
1174 new_chunk_id_str
, stream_count
);
1176 ret
= lttng_dynamic_buffer_append(&payload
, &msg
, sizeof(msg
));
1178 ERR("Failed to allocate \"rotate streams\" command payload");
1182 for (i
= 0; i
< stream_count
; i
++) {
1183 const struct relayd_stream_rotation_position
*position
=
1185 const struct lttcomm_relayd_stream_rotation_position comm_position
= {
1186 .stream_id
= htobe64(position
->stream_id
),
1187 .rotate_at_seq_num
= htobe64(
1188 position
->rotate_at_seq_num
),
1191 DBG("Rotate stream %" PRIu64
"at sequence number %" PRIu64
,
1192 position
->stream_id
,
1193 position
->rotate_at_seq_num
);
1194 ret
= lttng_dynamic_buffer_append(&payload
, &comm_position
,
1195 sizeof(comm_position
));
1197 ERR("Failed to allocate \"rotate streams\" command payload");
1203 ret
= send_command(sock
, RELAYD_ROTATE_STREAMS
, payload
.data
,
1206 ERR("Failed to send \"rotate stream\" command");
1210 /* Receive response. */
1211 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1213 ERR("Failed to receive \"rotate streams\" command reply");
1217 reply
.ret_code
= be32toh(reply
.ret_code
);
1218 if (reply
.ret_code
!= LTTNG_OK
) {
1220 ERR("Relayd rotate streams replied error %d", reply
.ret_code
);
1224 DBG("Relayd rotated streams successfully");
1228 lttng_dynamic_buffer_reset(&payload
);
1232 int relayd_create_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1233 struct lttng_trace_chunk
*chunk
)
1236 enum lttng_trace_chunk_status status
;
1237 struct lttcomm_relayd_create_trace_chunk msg
= {};
1238 struct lttcomm_relayd_generic_reply reply
= {};
1239 struct lttng_dynamic_buffer payload
;
1241 time_t creation_timestamp
;
1242 const char *chunk_name
;
1243 size_t chunk_name_length
;
1244 bool overridden_name
;
1246 lttng_dynamic_buffer_init(&payload
);
1248 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1249 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1254 status
= lttng_trace_chunk_get_creation_timestamp(
1255 chunk
, &creation_timestamp
);
1256 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1261 status
= lttng_trace_chunk_get_name(
1262 chunk
, &chunk_name
, &overridden_name
);
1263 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1264 status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1269 chunk_name_length
= overridden_name
? (strlen(chunk_name
) + 1) : 0;
1270 msg
= (typeof(msg
)){
1271 .chunk_id
= htobe64(chunk_id
),
1272 .creation_timestamp
= htobe64((uint64_t) creation_timestamp
),
1273 .override_name_length
= htobe32((uint32_t) chunk_name_length
),
1276 ret
= lttng_dynamic_buffer_append(&payload
, &msg
, sizeof(msg
));
1280 if (chunk_name_length
) {
1281 ret
= lttng_dynamic_buffer_append(
1282 &payload
, chunk_name
, chunk_name_length
);
1288 ret
= send_command(sock
, RELAYD_CREATE_TRACE_CHUNK
, payload
.data
,
1291 ERR("Failed to send trace chunk creation command to relay daemon");
1295 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1297 ERR("Failed to receive relay daemon trace chunk creation command reply");
1301 reply
.ret_code
= be32toh(reply
.ret_code
);
1302 if (reply
.ret_code
!= LTTNG_OK
) {
1304 ERR("Relayd trace chunk create replied error %d",
1308 DBG("Relayd successfully created trace chunk: chunk_id = %" PRIu64
,
1313 lttng_dynamic_buffer_reset(&payload
);
1317 int relayd_close_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1318 struct lttng_trace_chunk
*chunk
)
1321 enum lttng_trace_chunk_status status
;
1322 struct lttcomm_relayd_close_trace_chunk msg
= {};
1323 struct lttcomm_relayd_generic_reply reply
= {};
1325 time_t close_timestamp
;
1326 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
1328 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1329 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1330 ERR("Failed to get trace chunk id");
1335 status
= lttng_trace_chunk_get_close_timestamp(chunk
, &close_timestamp
);
1336 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1337 ERR("Failed to get trace chunk close timestamp");
1342 status
= lttng_trace_chunk_get_close_command(chunk
,
1343 &close_command
.value
);
1345 case LTTNG_TRACE_CHUNK_STATUS_OK
:
1346 close_command
.is_set
= 1;
1348 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
1351 ERR("Failed to get trace chunk close command");
1356 msg
= (typeof(msg
)){
1357 .chunk_id
= htobe64(chunk_id
),
1358 .close_timestamp
= htobe64((uint64_t) close_timestamp
),
1360 .value
= htobe32((uint32_t) close_command
.value
),
1361 .is_set
= close_command
.is_set
,
1365 ret
= send_command(sock
, RELAYD_CLOSE_TRACE_CHUNK
, &msg
, sizeof(msg
),
1368 ERR("Failed to send trace chunk close command to relay daemon");
1372 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1374 ERR("Failed to receive relay daemon trace chunk close command reply");
1378 reply
.ret_code
= be32toh(reply
.ret_code
);
1379 if (reply
.ret_code
!= LTTNG_OK
) {
1381 ERR("Relayd trace chunk close replied error %d",
1385 DBG("Relayd successfully closed trace chunk: chunk_id = %" PRIu64
,
1392 int relayd_trace_chunk_exists(struct lttcomm_relayd_sock
*sock
,
1393 uint64_t chunk_id
, bool *chunk_exists
)
1396 struct lttcomm_relayd_trace_chunk_exists msg
= {};
1397 struct lttcomm_relayd_trace_chunk_exists_reply reply
= {};
1399 msg
= (typeof(msg
)){
1400 .chunk_id
= htobe64(chunk_id
),
1403 ret
= send_command(sock
, RELAYD_TRACE_CHUNK_EXISTS
, &msg
, sizeof(msg
),
1406 ERR("Failed to send trace chunk exists command to relay daemon");
1410 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1412 ERR("Failed to receive relay daemon trace chunk close command reply");
1416 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1417 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1419 ERR("Relayd trace chunk close replied error %d",
1420 reply
.generic
.ret_code
);
1423 DBG("Relayd successfully checked trace chunk existence: chunk_id = %" PRIu64
1424 ", exists = %s", chunk_id
,
1425 reply
.trace_chunk_exists
? "true" : "false");
1426 *chunk_exists
= !!reply
.trace_chunk_exists
;