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>
37 * Send command. Fill up the header and append the data.
39 static int send_command(struct lttcomm_relayd_sock
*rsock
,
40 enum lttcomm_relayd_command cmd
, const void *data
, size_t size
,
44 struct lttcomm_relayd_hdr header
;
46 uint64_t buf_size
= sizeof(header
);
48 if (rsock
->sock
.fd
< 0) {
56 buf
= zmalloc(buf_size
);
58 PERROR("zmalloc relayd send command buf");
63 memset(&header
, 0, sizeof(header
));
64 header
.cmd
= htobe32(cmd
);
65 header
.data_size
= htobe64(size
);
67 /* Zeroed for now since not used. */
68 header
.cmd_version
= 0;
69 header
.circuit_id
= 0;
71 /* Prepare buffer to send. */
72 memcpy(buf
, &header
, sizeof(header
));
74 memcpy(buf
+ sizeof(header
), data
, size
);
77 DBG3("Relayd sending command %d of size %" PRIu64
, (int) cmd
, buf_size
);
78 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
80 PERROR("Failed to send command %d of size %" PRIu64
,
92 * Receive reply data on socket. This MUST be call after send_command or else
93 * could result in unexpected behavior(s).
95 static int recv_reply(struct lttcomm_relayd_sock
*rsock
, void *data
, size_t size
)
99 if (rsock
->sock
.fd
< 0) {
103 DBG3("Relayd waiting for reply of size %zu", size
);
105 ret
= rsock
->sock
.ops
->recvmsg(&rsock
->sock
, data
, size
, 0);
106 if (ret
<= 0 || ret
!= size
) {
108 /* Orderly shutdown. */
109 DBG("Socket %d has performed an orderly shutdown", rsock
->sock
.fd
);
111 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
112 rsock
->sock
.fd
, size
, ret
);
114 /* Always return -1 here and the caller can use errno. */
124 * Starting from 2.11, RELAYD_CREATE_SESSION payload (session_name & hostname)
125 * have no length restriction on the sender side.
126 * Length for both payloads is stored in the msg struct. A new dynamic size
127 * payload size is introduced.
129 static int relayd_create_session_2_11(struct lttcomm_relayd_sock
*rsock
,
130 const char *session_name
, const char *hostname
,
131 int session_live_timer
, unsigned int snapshot
,
132 uint64_t sessiond_session_id
, const lttng_uuid sessiond_uuid
,
133 const uint64_t *current_chunk_id
)
136 struct lttcomm_relayd_create_session_2_11
*msg
= NULL
;
137 size_t session_name_len
;
141 /* The two names are sent with a '\0' delimiter between them. */
142 session_name_len
= strlen(session_name
) + 1;
143 hostname_len
= strlen(hostname
) + 1;
145 msg_length
= sizeof(*msg
) + session_name_len
+ hostname_len
;
146 msg
= zmalloc(msg_length
);
148 PERROR("zmalloc create_session_2_11 command message");
153 assert(session_name_len
<= UINT32_MAX
);
154 msg
->session_name_len
= htobe32(session_name_len
);
156 assert(hostname_len
<= UINT32_MAX
);
157 msg
->hostname_len
= htobe32(hostname_len
);
159 if (lttng_strncpy(msg
->names
, session_name
, session_name_len
)) {
163 if (lttng_strncpy(msg
->names
+ session_name_len
, hostname
, hostname_len
)) {
168 msg
->live_timer
= htobe32(session_live_timer
);
169 msg
->snapshot
= !!snapshot
;
171 lttng_uuid_copy(msg
->sessiond_uuid
, sessiond_uuid
);
172 msg
->session_id
= htobe64(sessiond_session_id
);
174 if (current_chunk_id
) {
175 LTTNG_OPTIONAL_SET(&msg
->current_chunk_id
,
176 htobe64(*current_chunk_id
));
180 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, msg
, msg_length
, 0);
189 * From 2.4 to 2.10, RELAYD_CREATE_SESSION takes additional parameters to
190 * support the live reading capability.
192 static int relayd_create_session_2_4(struct lttcomm_relayd_sock
*rsock
,
193 const char *session_name
, const char *hostname
,
194 int session_live_timer
, unsigned int snapshot
)
197 struct lttcomm_relayd_create_session_2_4 msg
;
199 if (lttng_strncpy(msg
.session_name
, session_name
,
200 sizeof(msg
.session_name
))) {
204 if (lttng_strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
))) {
208 msg
.live_timer
= htobe32(session_live_timer
);
209 msg
.snapshot
= htobe32(snapshot
);
212 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, &msg
, sizeof(msg
), 0);
222 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
224 static int relayd_create_session_2_1(struct lttcomm_relayd_sock
*rsock
)
229 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
239 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
240 * set session_id of the relayd if we have a successful reply from the relayd.
242 * On success, return 0 else a negative value which is either an errno error or
243 * a lttng error code from the relayd.
245 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
,
246 uint64_t *relayd_session_id
,
247 const char *session_name
, const char *hostname
,
248 int session_live_timer
,
249 unsigned int snapshot
, uint64_t sessiond_session_id
,
250 const lttng_uuid sessiond_uuid
,
251 const uint64_t *current_chunk_id
)
254 struct lttcomm_relayd_status_session reply
;
257 assert(relayd_session_id
);
259 DBG("Relayd create session");
261 if (rsock
->minor
< 4) {
262 /* From 2.1 to 2.3 */
263 ret
= relayd_create_session_2_1(rsock
);
264 } else if (rsock
->minor
>= 4 && rsock
->minor
< 11) {
265 /* From 2.4 to 2.10 */
266 ret
= relayd_create_session_2_4(rsock
, session_name
,
267 hostname
, session_live_timer
, snapshot
);
269 /* From 2.11 to ... */
270 ret
= relayd_create_session_2_11(rsock
, session_name
,
271 hostname
, session_live_timer
, snapshot
,
272 sessiond_session_id
, sessiond_uuid
,
280 /* Receive response */
281 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
286 reply
.session_id
= be64toh(reply
.session_id
);
287 reply
.ret_code
= be32toh(reply
.ret_code
);
289 /* Return session id or negative ret code. */
290 if (reply
.ret_code
!= LTTNG_OK
) {
292 ERR("Relayd create session replied error %d", reply
.ret_code
);
296 *relayd_session_id
= reply
.session_id
;
299 DBG("Relayd session created with id %" PRIu64
, reply
.session_id
);
305 static int relayd_add_stream_2_1(struct lttcomm_relayd_sock
*rsock
,
306 const char *channel_name
, const char *pathname
)
309 struct lttcomm_relayd_add_stream msg
;
311 memset(&msg
, 0, sizeof(msg
));
312 if (lttng_strncpy(msg
.channel_name
, channel_name
,
313 sizeof(msg
.channel_name
))) {
318 if (lttng_strncpy(msg
.pathname
, pathname
,
319 sizeof(msg
.pathname
))) {
325 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
335 static int relayd_add_stream_2_2(struct lttcomm_relayd_sock
*rsock
,
336 const char *channel_name
, const char *pathname
,
337 uint64_t tracefile_size
, uint64_t tracefile_count
)
340 struct lttcomm_relayd_add_stream_2_2 msg
;
342 memset(&msg
, 0, sizeof(msg
));
343 /* Compat with relayd 2.2 to 2.10 */
344 if (lttng_strncpy(msg
.channel_name
, channel_name
,
345 sizeof(msg
.channel_name
))) {
349 if (lttng_strncpy(msg
.pathname
, pathname
,
350 sizeof(msg
.pathname
))) {
354 msg
.tracefile_size
= htobe64(tracefile_size
);
355 msg
.tracefile_count
= htobe64(tracefile_count
);
358 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
367 static int relayd_add_stream_2_11(struct lttcomm_relayd_sock
*rsock
,
368 const char *channel_name
, const char *pathname
,
369 uint64_t tracefile_size
, uint64_t tracefile_count
,
370 uint64_t trace_archive_id
)
373 struct lttcomm_relayd_add_stream_2_11
*msg
= NULL
;
374 size_t channel_name_len
;
378 /* The two names are sent with a '\0' delimiter between them. */
379 channel_name_len
= strlen(channel_name
) + 1;
380 pathname_len
= strlen(pathname
) + 1;
382 msg_length
= sizeof(*msg
) + channel_name_len
+ pathname_len
;
383 msg
= zmalloc(msg_length
);
385 PERROR("zmalloc add_stream_2_11 command message");
390 assert(channel_name_len
<= UINT32_MAX
);
391 msg
->channel_name_len
= htobe32(channel_name_len
);
393 assert(pathname_len
<= UINT32_MAX
);
394 msg
->pathname_len
= htobe32(pathname_len
);
396 if (lttng_strncpy(msg
->names
, channel_name
, channel_name_len
)) {
400 if (lttng_strncpy(msg
->names
+ channel_name_len
, pathname
, pathname_len
)) {
405 msg
->tracefile_size
= htobe64(tracefile_size
);
406 msg
->tracefile_count
= htobe64(tracefile_count
);
407 msg
->trace_archive_id
= htobe64(trace_archive_id
);
410 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) msg
, msg_length
, 0);
421 * Add stream on the relayd and assign stream handle to the stream_id argument.
423 * On success return 0 else return ret_code negative value.
425 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
426 const char *pathname
, uint64_t *stream_id
,
427 uint64_t tracefile_size
, uint64_t tracefile_count
,
428 struct lttng_trace_chunk
*trace_chunk
)
431 struct lttcomm_relayd_status_stream reply
;
433 /* Code flow error. Safety net. */
435 assert(channel_name
);
438 DBG("Relayd adding stream for channel name %s", channel_name
);
440 /* Compat with relayd 2.1 */
441 if (rsock
->minor
== 1) {
443 assert(!trace_chunk
);
444 ret
= relayd_add_stream_2_1(rsock
, channel_name
, pathname
);
446 } else if (rsock
->minor
> 1 && rsock
->minor
< 11) {
447 /* From 2.2 to 2.10 */
448 assert(!trace_chunk
);
449 ret
= relayd_add_stream_2_2(rsock
, channel_name
, pathname
,
450 tracefile_size
, tracefile_count
);
452 enum lttng_trace_chunk_status chunk_status
;
456 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
,
458 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
460 /* From 2.11 to ...*/
461 ret
= relayd_add_stream_2_11(rsock
, channel_name
, pathname
,
462 tracefile_size
, tracefile_count
,
471 /* Waiting for reply */
472 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
477 /* Back to host bytes order. */
478 reply
.handle
= be64toh(reply
.handle
);
479 reply
.ret_code
= be32toh(reply
.ret_code
);
481 /* Return session id or negative ret code. */
482 if (reply
.ret_code
!= LTTNG_OK
) {
484 ERR("Relayd add stream replied error %d", reply
.ret_code
);
488 *stream_id
= reply
.handle
;
491 DBG("Relayd stream added successfully with handle %" PRIu64
,
499 * Inform the relay that all the streams for the current channel has been sent.
501 * On success return 0 else return ret_code negative value.
503 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
506 struct lttcomm_relayd_generic_reply reply
;
508 /* Code flow error. Safety net. */
511 DBG("Relayd sending streams sent.");
513 /* This feature was introduced in 2.4, ignore it for earlier versions. */
514 if (rsock
->minor
< 4) {
520 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, NULL
, 0, 0);
525 /* Waiting for reply */
526 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
531 /* Back to host bytes order. */
532 reply
.ret_code
= be32toh(reply
.ret_code
);
534 /* Return session id or negative ret code. */
535 if (reply
.ret_code
!= LTTNG_OK
) {
537 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
544 DBG("Relayd streams sent success");
552 * Check version numbers on the relayd.
553 * If major versions are compatible, we assign minor_to_use to the
554 * minor version of the procotol we are going to use for this session.
556 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
557 * otherwise, or a negative value on network errors.
559 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
562 struct lttcomm_relayd_version msg
;
564 /* Code flow error. Safety net. */
567 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
570 memset(&msg
, 0, sizeof(msg
));
571 /* Prepare network byte order before transmission. */
572 msg
.major
= htobe32(rsock
->major
);
573 msg
.minor
= htobe32(rsock
->minor
);
576 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
581 /* Receive response */
582 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
587 /* Set back to host bytes order */
588 msg
.major
= be32toh(msg
.major
);
589 msg
.minor
= be32toh(msg
.minor
);
592 * Only validate the major version. If the other side is higher,
593 * communication is not possible. Only major version equal can talk to each
594 * other. If the minor version differs, the lowest version is used by both
597 if (msg
.major
!= rsock
->major
) {
599 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
600 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
601 msg
.major
, rsock
->major
);
606 * If the relayd's minor version is higher, it will adapt to our version so
607 * we can continue to use the latest relayd communication data structure.
608 * If the received minor version is higher, the relayd should adapt to us.
610 if (rsock
->minor
> msg
.minor
) {
611 rsock
->minor
= msg
.minor
;
614 /* Version number compatible */
615 DBG2("Relayd version is compatible, using protocol version %u.%u",
616 rsock
->major
, rsock
->minor
);
624 * Add stream on the relayd and assign stream handle to the stream_id argument.
626 * On success return 0 else return ret_code negative value.
628 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
632 /* Code flow error. Safety net. */
635 DBG("Relayd sending metadata of size %zu", len
);
638 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
643 DBG2("Relayd metadata added successfully");
646 * After that call, the metadata data MUST be sent to the relayd so the
647 * receive size on the other end matches the len of the metadata packet
648 * header. This is why we don't wait for a reply here.
656 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
658 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
660 /* Code flow error. Safety net. */
663 if (!rsock
->sock
.ops
) {
665 * Attempting a connect on a non-initialized socket.
670 DBG3("Relayd connect ...");
672 return rsock
->sock
.ops
->connect(&rsock
->sock
);
676 * Close relayd socket with an allocated lttcomm_relayd_sock.
678 * If no socket operations are found, simply return 0 meaning that everything
679 * is fine. Without operations, the socket can not possibly be opened or used.
680 * This is possible if the socket was allocated but not created. However, the
681 * caller could simply use it to store a valid file descriptor for instance
682 * passed over a Unix socket and call this to cleanup but still without a valid
685 * Return the close returned value. On error, a negative value is usually
686 * returned back from close(2).
688 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
692 /* Code flow error. Safety net. */
695 /* An invalid fd is fine, return success. */
696 if (rsock
->sock
.fd
< 0) {
701 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
703 if (rsock
->sock
.ops
) {
704 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
706 /* Default call if no specific ops found. */
707 ret
= close(rsock
->sock
.fd
);
709 PERROR("relayd_close default close");
719 * Send data header structure to the relayd.
721 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
722 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
726 /* Code flow error. Safety net. */
730 if (rsock
->sock
.fd
< 0) {
734 DBG3("Relayd sending data header of size %zu", size
);
736 /* Again, safety net */
738 size
= sizeof(struct lttcomm_relayd_data_hdr
);
741 /* Only send data header. */
742 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
749 * The data MUST be sent right after that command for the receive on the
750 * other end to match the size in the header.
758 * Send close stream command to the relayd.
760 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
761 uint64_t last_net_seq_num
)
764 struct lttcomm_relayd_close_stream msg
;
765 struct lttcomm_relayd_generic_reply reply
;
767 /* Code flow error. Safety net. */
770 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
772 memset(&msg
, 0, sizeof(msg
));
773 msg
.stream_id
= htobe64(stream_id
);
774 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
777 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
782 /* Receive response */
783 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
788 reply
.ret_code
= be32toh(reply
.ret_code
);
790 /* Return session id or negative ret code. */
791 if (reply
.ret_code
!= LTTNG_OK
) {
793 ERR("Relayd close stream replied error %d", reply
.ret_code
);
799 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
806 * Check for data availability for a given stream id.
808 * Return 0 if NOT pending, 1 if so and a negative value on error.
810 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
811 uint64_t last_net_seq_num
)
814 struct lttcomm_relayd_data_pending msg
;
815 struct lttcomm_relayd_generic_reply reply
;
817 /* Code flow error. Safety net. */
820 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
822 memset(&msg
, 0, sizeof(msg
));
823 msg
.stream_id
= htobe64(stream_id
);
824 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
827 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
833 /* Receive response */
834 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
839 reply
.ret_code
= be32toh(reply
.ret_code
);
841 /* Return session id or negative ret code. */
842 if (reply
.ret_code
>= LTTNG_OK
) {
843 ERR("Relayd data pending replied error %d", reply
.ret_code
);
846 /* At this point, the ret code is either 1 or 0 */
847 ret
= reply
.ret_code
;
849 DBG("Relayd data is %s pending for stream id %" PRIu64
,
850 ret
== 1 ? "" : "NOT", stream_id
);
857 * Check on the relayd side for a quiescent state on the control socket.
859 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
860 uint64_t metadata_stream_id
)
863 struct lttcomm_relayd_quiescent_control msg
;
864 struct lttcomm_relayd_generic_reply reply
;
866 /* Code flow error. Safety net. */
869 DBG("Relayd checking quiescent control state");
871 memset(&msg
, 0, sizeof(msg
));
872 msg
.stream_id
= htobe64(metadata_stream_id
);
875 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
880 /* Receive response */
881 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
886 reply
.ret_code
= be32toh(reply
.ret_code
);
888 /* Return session id or negative ret code. */
889 if (reply
.ret_code
!= LTTNG_OK
) {
891 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
895 /* Control socket is quiescent */
903 * Begin a data pending command for a specific session id.
905 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
908 struct lttcomm_relayd_begin_data_pending msg
;
909 struct lttcomm_relayd_generic_reply reply
;
911 /* Code flow error. Safety net. */
914 DBG("Relayd begin data pending");
916 memset(&msg
, 0, sizeof(msg
));
917 msg
.session_id
= htobe64(id
);
920 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
925 /* Receive response */
926 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
931 reply
.ret_code
= be32toh(reply
.ret_code
);
933 /* Return session id or negative ret code. */
934 if (reply
.ret_code
!= LTTNG_OK
) {
936 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
947 * End a data pending command for a specific session id.
949 * Return 0 on success and set is_data_inflight to 0 if no data is being
950 * streamed or 1 if it is the case.
952 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
953 unsigned int *is_data_inflight
)
956 struct lttcomm_relayd_end_data_pending msg
;
957 struct lttcomm_relayd_generic_reply reply
;
959 /* Code flow error. Safety net. */
962 DBG("Relayd end data pending");
964 memset(&msg
, 0, sizeof(msg
));
965 msg
.session_id
= htobe64(id
);
968 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
973 /* Receive response */
974 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
979 recv_ret
= be32toh(reply
.ret_code
);
985 *is_data_inflight
= recv_ret
;
987 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
996 * Send index to the relayd.
998 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
999 struct ctf_packet_index
*index
, uint64_t relay_stream_id
,
1000 uint64_t net_seq_num
)
1003 struct lttcomm_relayd_index msg
;
1004 struct lttcomm_relayd_generic_reply reply
;
1006 /* Code flow error. Safety net. */
1009 if (rsock
->minor
< 4) {
1010 DBG("Not sending indexes before protocol 2.4");
1015 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
1017 memset(&msg
, 0, sizeof(msg
));
1018 msg
.relay_stream_id
= htobe64(relay_stream_id
);
1019 msg
.net_seq_num
= htobe64(net_seq_num
);
1021 /* The index is already in big endian. */
1022 msg
.packet_size
= index
->packet_size
;
1023 msg
.content_size
= index
->content_size
;
1024 msg
.timestamp_begin
= index
->timestamp_begin
;
1025 msg
.timestamp_end
= index
->timestamp_end
;
1026 msg
.events_discarded
= index
->events_discarded
;
1027 msg
.stream_id
= index
->stream_id
;
1029 if (rsock
->minor
>= 8) {
1030 msg
.stream_instance_id
= index
->stream_instance_id
;
1031 msg
.packet_seq_num
= index
->packet_seq_num
;
1035 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
,
1036 lttcomm_relayd_index_len(lttng_to_index_major(rsock
->major
,
1038 lttng_to_index_minor(rsock
->major
, rsock
->minor
)),
1044 /* Receive response */
1045 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1050 reply
.ret_code
= be32toh(reply
.ret_code
);
1052 /* Return session id or negative ret code. */
1053 if (reply
.ret_code
!= LTTNG_OK
) {
1055 ERR("Relayd send index replied error %d", reply
.ret_code
);
1066 * Ask the relay to reset the metadata trace file (regeneration).
1068 int relayd_reset_metadata(struct lttcomm_relayd_sock
*rsock
,
1069 uint64_t stream_id
, uint64_t version
)
1072 struct lttcomm_relayd_reset_metadata msg
;
1073 struct lttcomm_relayd_generic_reply reply
;
1075 /* Code flow error. Safety net. */
1078 /* Should have been prevented by the sessiond. */
1079 if (rsock
->minor
< 8) {
1080 ERR("Metadata regeneration unsupported before 2.8");
1085 DBG("Relayd reset metadata stream id %" PRIu64
, stream_id
);
1087 memset(&msg
, 0, sizeof(msg
));
1088 msg
.stream_id
= htobe64(stream_id
);
1089 msg
.version
= htobe64(version
);
1092 ret
= send_command(rsock
, RELAYD_RESET_METADATA
, (void *) &msg
, sizeof(msg
), 0);
1097 /* Receive response */
1098 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1103 reply
.ret_code
= be32toh(reply
.ret_code
);
1105 /* Return session id or negative ret code. */
1106 if (reply
.ret_code
!= LTTNG_OK
) {
1108 ERR("Relayd reset metadata replied error %d", reply
.ret_code
);
1114 DBG("Relayd reset metadata stream id %" PRIu64
" successfully", stream_id
);
1120 int relayd_rotate_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
1121 uint64_t new_chunk_id
, uint64_t seq_num
)
1124 struct lttcomm_relayd_rotate_stream
*msg
= NULL
;
1125 struct lttcomm_relayd_generic_reply reply
;
1129 char *new_pathname
= NULL
;
1131 /* Code flow error. Safety net. */
1134 DBG("Sending rotate stream id %" PRIu64
" command to relayd", stream_id
);
1136 /* Account for the trailing NULL. */
1137 len
= lttng_strnlen(new_pathname
, LTTNG_PATH_MAX
) + 1;
1138 if (len
> LTTNG_PATH_MAX
) {
1139 ERR("Path used in relayd rotate stream command exceeds the maximal allowed length");
1144 msg_len
= offsetof(struct lttcomm_relayd_rotate_stream
, new_pathname
) + len
;
1145 msg
= zmalloc(msg_len
);
1147 PERROR("Failed to allocate relayd rotate stream command of %d bytes",
1153 if (lttng_strncpy(msg
->new_pathname
, new_pathname
, len
)) {
1155 ERR("Failed to copy relayd rotate stream command's new path name");
1159 msg
->pathname_length
= htobe32(len
);
1160 msg
->stream_id
= htobe64(stream_id
);
1161 msg
->new_chunk_id
= htobe64(new_chunk_id
);
1163 * The seq_num is invalid for metadata streams, but it is ignored on
1166 msg
->rotate_at_seq_num
= htobe64(seq_num
);
1169 ret
= send_command(rsock
, RELAYD_ROTATE_STREAM
, (void *) msg
, msg_len
, 0);
1171 ERR("Send rotate command");
1175 /* Receive response. */
1176 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1178 ERR("Receive rotate reply");
1182 reply
.ret_code
= be32toh(reply
.ret_code
);
1184 /* Return session id or negative ret code. */
1185 if (reply
.ret_code
!= LTTNG_OK
) {
1187 ERR("Relayd rotate stream replied error %d", reply
.ret_code
);
1191 DBG("Relayd rotated stream id %" PRIu64
" successfully", stream_id
);
1199 int relayd_create_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1200 struct lttng_trace_chunk
*chunk
)
1203 enum lttng_trace_chunk_status status
;
1204 struct lttcomm_relayd_create_trace_chunk msg
= {};
1205 struct lttcomm_relayd_generic_reply reply
= {};
1206 struct lttng_dynamic_buffer payload
;
1208 time_t creation_timestamp
;
1209 const char *chunk_name
;
1210 size_t chunk_name_length
;
1211 bool overriden_name
;
1213 lttng_dynamic_buffer_init(&payload
);
1215 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1216 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1221 status
= lttng_trace_chunk_get_creation_timestamp(
1222 chunk
, &creation_timestamp
);
1223 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1228 status
= lttng_trace_chunk_get_name(
1229 chunk
, &chunk_name
, &overriden_name
);
1230 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1231 status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1236 chunk_name_length
= overriden_name
? (strlen(chunk_name
) + 1) : 0;
1237 msg
= (typeof(msg
)){
1238 .chunk_id
= htobe64(chunk_id
),
1239 .creation_timestamp
= htobe64((uint64_t) creation_timestamp
),
1240 .override_name_length
= htobe32((uint32_t) chunk_name_length
),
1243 ret
= lttng_dynamic_buffer_append(&payload
, &msg
, sizeof(msg
));
1247 if (chunk_name_length
) {
1248 ret
= lttng_dynamic_buffer_append(
1249 &payload
, chunk_name
, chunk_name_length
);
1255 ret
= send_command(sock
, RELAYD_CREATE_TRACE_CHUNK
, payload
.data
,
1258 ERR("Failed to send trace chunk creation command to relay daemon");
1262 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1264 ERR("Failed to receive relay daemon trace chunk creation command reply");
1268 reply
.ret_code
= be32toh(reply
.ret_code
);
1269 if (reply
.ret_code
!= LTTNG_OK
) {
1271 ERR("Relayd trace chunk create replied error %d",
1275 DBG("Relayd successfully created trace chunk: chunk_id = %" PRIu64
,
1280 lttng_dynamic_buffer_reset(&payload
);
1284 int relayd_close_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1285 struct lttng_trace_chunk
*chunk
)
1288 enum lttng_trace_chunk_status status
;
1289 struct lttcomm_relayd_close_trace_chunk msg
= {};
1290 struct lttcomm_relayd_generic_reply reply
= {};
1292 time_t close_timestamp
;
1293 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
1295 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1296 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1297 ERR("Failed to get trace chunk id");
1302 status
= lttng_trace_chunk_get_close_timestamp(chunk
, &close_timestamp
);
1303 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1304 ERR("Failed to get trace chunk close timestamp");
1309 status
= lttng_trace_chunk_get_close_command(chunk
,
1310 &close_command
.value
);
1312 case LTTNG_TRACE_CHUNK_STATUS_OK
:
1313 close_command
.is_set
= 1;
1315 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
1318 ERR("Failed to get trace chunk close command");
1323 msg
= (typeof(msg
)){
1324 .chunk_id
= htobe64(chunk_id
),
1325 .close_timestamp
= htobe64((uint64_t) close_timestamp
),
1327 .value
= htobe32((uint32_t) close_command
.value
),
1328 .is_set
= close_command
.is_set
,
1332 ret
= send_command(sock
, RELAYD_CLOSE_TRACE_CHUNK
, &msg
, sizeof(msg
),
1335 ERR("Failed to send trace chunk close command to relay daemon");
1339 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1341 ERR("Failed to receive relay daemon trace chunk close command reply");
1345 reply
.ret_code
= be32toh(reply
.ret_code
);
1346 if (reply
.ret_code
!= LTTNG_OK
) {
1348 ERR("Relayd trace chunk close replied error %d",
1352 DBG("Relayd successfully closed trace chunk: chunk_id = %" PRIu64
,