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
,
134 time_t creation_time
)
137 struct lttcomm_relayd_create_session_2_11
*msg
= NULL
;
138 size_t session_name_len
;
142 /* The two names are sent with a '\0' delimiter between them. */
143 session_name_len
= strlen(session_name
) + 1;
144 hostname_len
= strlen(hostname
) + 1;
146 msg_length
= sizeof(*msg
) + session_name_len
+ hostname_len
;
147 msg
= zmalloc(msg_length
);
149 PERROR("zmalloc create_session_2_11 command message");
154 assert(session_name_len
<= UINT32_MAX
);
155 msg
->session_name_len
= htobe32(session_name_len
);
157 assert(hostname_len
<= UINT32_MAX
);
158 msg
->hostname_len
= htobe32(hostname_len
);
160 if (lttng_strncpy(msg
->names
, session_name
, session_name_len
)) {
164 if (lttng_strncpy(msg
->names
+ session_name_len
, hostname
, hostname_len
)) {
169 msg
->live_timer
= htobe32(session_live_timer
);
170 msg
->snapshot
= !!snapshot
;
172 lttng_uuid_copy(msg
->sessiond_uuid
, sessiond_uuid
);
173 msg
->session_id
= htobe64(sessiond_session_id
);
175 if (current_chunk_id
) {
176 LTTNG_OPTIONAL_SET(&msg
->current_chunk_id
,
177 htobe64(*current_chunk_id
));
180 msg
->creation_time
= htobe64((uint64_t) creation_time
);
183 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, msg
, msg_length
, 0);
192 * From 2.4 to 2.10, RELAYD_CREATE_SESSION takes additional parameters to
193 * support the live reading capability.
195 static int relayd_create_session_2_4(struct lttcomm_relayd_sock
*rsock
,
196 const char *session_name
, const char *hostname
,
197 int session_live_timer
, unsigned int snapshot
)
200 struct lttcomm_relayd_create_session_2_4 msg
;
202 if (lttng_strncpy(msg
.session_name
, session_name
,
203 sizeof(msg
.session_name
))) {
207 if (lttng_strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
))) {
211 msg
.live_timer
= htobe32(session_live_timer
);
212 msg
.snapshot
= htobe32(snapshot
);
215 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, &msg
, sizeof(msg
), 0);
225 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
227 static int relayd_create_session_2_1(struct lttcomm_relayd_sock
*rsock
)
232 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
242 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
243 * set session_id of the relayd if we have a successful reply from the relayd.
245 * On success, return 0 else a negative value which is either an errno error or
246 * a lttng error code from the relayd.
248 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
,
249 uint64_t *relayd_session_id
,
250 const char *session_name
, const char *hostname
,
251 int session_live_timer
,
252 unsigned int snapshot
, uint64_t sessiond_session_id
,
253 const lttng_uuid sessiond_uuid
,
254 const uint64_t *current_chunk_id
,
255 time_t creation_time
)
258 struct lttcomm_relayd_status_session reply
;
261 assert(relayd_session_id
);
263 DBG("Relayd create session");
265 if (rsock
->minor
< 4) {
266 /* From 2.1 to 2.3 */
267 ret
= relayd_create_session_2_1(rsock
);
268 } else if (rsock
->minor
>= 4 && rsock
->minor
< 11) {
269 /* From 2.4 to 2.10 */
270 ret
= relayd_create_session_2_4(rsock
, session_name
,
271 hostname
, session_live_timer
, snapshot
);
273 /* From 2.11 to ... */
274 ret
= relayd_create_session_2_11(rsock
, session_name
,
275 hostname
, session_live_timer
, snapshot
,
276 sessiond_session_id
, sessiond_uuid
,
277 current_chunk_id
, creation_time
);
284 /* Receive response */
285 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
290 reply
.session_id
= be64toh(reply
.session_id
);
291 reply
.ret_code
= be32toh(reply
.ret_code
);
293 /* Return session id or negative ret code. */
294 if (reply
.ret_code
!= LTTNG_OK
) {
296 ERR("Relayd create session replied error %d", reply
.ret_code
);
300 *relayd_session_id
= reply
.session_id
;
303 DBG("Relayd session created with id %" PRIu64
, reply
.session_id
);
309 static int relayd_add_stream_2_1(struct lttcomm_relayd_sock
*rsock
,
310 const char *channel_name
, const char *pathname
)
313 struct lttcomm_relayd_add_stream msg
;
315 memset(&msg
, 0, sizeof(msg
));
316 if (lttng_strncpy(msg
.channel_name
, channel_name
,
317 sizeof(msg
.channel_name
))) {
322 if (lttng_strncpy(msg
.pathname
, pathname
,
323 sizeof(msg
.pathname
))) {
329 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
339 static int relayd_add_stream_2_2(struct lttcomm_relayd_sock
*rsock
,
340 const char *channel_name
, const char *pathname
,
341 uint64_t tracefile_size
, uint64_t tracefile_count
)
344 struct lttcomm_relayd_add_stream_2_2 msg
;
346 memset(&msg
, 0, sizeof(msg
));
347 /* Compat with relayd 2.2 to 2.10 */
348 if (lttng_strncpy(msg
.channel_name
, channel_name
,
349 sizeof(msg
.channel_name
))) {
353 if (lttng_strncpy(msg
.pathname
, pathname
,
354 sizeof(msg
.pathname
))) {
358 msg
.tracefile_size
= htobe64(tracefile_size
);
359 msg
.tracefile_count
= htobe64(tracefile_count
);
362 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
371 static int relayd_add_stream_2_11(struct lttcomm_relayd_sock
*rsock
,
372 const char *channel_name
, const char *pathname
,
373 uint64_t tracefile_size
, uint64_t tracefile_count
,
374 uint64_t trace_archive_id
)
377 struct lttcomm_relayd_add_stream_2_11
*msg
= NULL
;
378 size_t channel_name_len
;
382 /* The two names are sent with a '\0' delimiter between them. */
383 channel_name_len
= strlen(channel_name
) + 1;
384 pathname_len
= strlen(pathname
) + 1;
386 msg_length
= sizeof(*msg
) + channel_name_len
+ pathname_len
;
387 msg
= zmalloc(msg_length
);
389 PERROR("zmalloc add_stream_2_11 command message");
394 assert(channel_name_len
<= UINT32_MAX
);
395 msg
->channel_name_len
= htobe32(channel_name_len
);
397 assert(pathname_len
<= UINT32_MAX
);
398 msg
->pathname_len
= htobe32(pathname_len
);
400 if (lttng_strncpy(msg
->names
, channel_name
, channel_name_len
)) {
404 if (lttng_strncpy(msg
->names
+ channel_name_len
, pathname
, pathname_len
)) {
409 msg
->tracefile_size
= htobe64(tracefile_size
);
410 msg
->tracefile_count
= htobe64(tracefile_count
);
411 msg
->trace_archive_id
= htobe64(trace_archive_id
);
414 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) msg
, msg_length
, 0);
425 * Add stream on the relayd and assign stream handle to the stream_id argument.
427 * On success return 0 else return ret_code negative value.
429 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
430 const char *pathname
, uint64_t *stream_id
,
431 uint64_t tracefile_size
, uint64_t tracefile_count
,
432 struct lttng_trace_chunk
*trace_chunk
)
435 struct lttcomm_relayd_status_stream reply
;
437 /* Code flow error. Safety net. */
439 assert(channel_name
);
442 DBG("Relayd adding stream for channel name %s", channel_name
);
444 /* Compat with relayd 2.1 */
445 if (rsock
->minor
== 1) {
447 assert(!trace_chunk
);
448 ret
= relayd_add_stream_2_1(rsock
, channel_name
, pathname
);
450 } else if (rsock
->minor
> 1 && rsock
->minor
< 11) {
451 /* From 2.2 to 2.10 */
452 assert(!trace_chunk
);
453 ret
= relayd_add_stream_2_2(rsock
, channel_name
, pathname
,
454 tracefile_size
, tracefile_count
);
456 enum lttng_trace_chunk_status chunk_status
;
460 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
,
462 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
464 /* From 2.11 to ...*/
465 ret
= relayd_add_stream_2_11(rsock
, channel_name
, pathname
,
466 tracefile_size
, tracefile_count
,
475 /* Waiting for reply */
476 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
481 /* Back to host bytes order. */
482 reply
.handle
= be64toh(reply
.handle
);
483 reply
.ret_code
= be32toh(reply
.ret_code
);
485 /* Return session id or negative ret code. */
486 if (reply
.ret_code
!= LTTNG_OK
) {
488 ERR("Relayd add stream replied error %d", reply
.ret_code
);
492 *stream_id
= reply
.handle
;
495 DBG("Relayd stream added successfully with handle %" PRIu64
,
503 * Inform the relay that all the streams for the current channel has been sent.
505 * On success return 0 else return ret_code negative value.
507 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
510 struct lttcomm_relayd_generic_reply reply
;
512 /* Code flow error. Safety net. */
515 DBG("Relayd sending streams sent.");
517 /* This feature was introduced in 2.4, ignore it for earlier versions. */
518 if (rsock
->minor
< 4) {
524 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, NULL
, 0, 0);
529 /* Waiting for reply */
530 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
535 /* Back to host bytes order. */
536 reply
.ret_code
= be32toh(reply
.ret_code
);
538 /* Return session id or negative ret code. */
539 if (reply
.ret_code
!= LTTNG_OK
) {
541 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
548 DBG("Relayd streams sent success");
556 * Check version numbers on the relayd.
557 * If major versions are compatible, we assign minor_to_use to the
558 * minor version of the procotol we are going to use for this session.
560 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
561 * otherwise, or a negative value on network errors.
563 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
566 struct lttcomm_relayd_version msg
;
568 /* Code flow error. Safety net. */
571 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
574 memset(&msg
, 0, sizeof(msg
));
575 /* Prepare network byte order before transmission. */
576 msg
.major
= htobe32(rsock
->major
);
577 msg
.minor
= htobe32(rsock
->minor
);
580 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
585 /* Receive response */
586 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
591 /* Set back to host bytes order */
592 msg
.major
= be32toh(msg
.major
);
593 msg
.minor
= be32toh(msg
.minor
);
596 * Only validate the major version. If the other side is higher,
597 * communication is not possible. Only major version equal can talk to each
598 * other. If the minor version differs, the lowest version is used by both
601 if (msg
.major
!= rsock
->major
) {
603 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
604 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
605 msg
.major
, rsock
->major
);
610 * If the relayd's minor version is higher, it will adapt to our version so
611 * we can continue to use the latest relayd communication data structure.
612 * If the received minor version is higher, the relayd should adapt to us.
614 if (rsock
->minor
> msg
.minor
) {
615 rsock
->minor
= msg
.minor
;
618 /* Version number compatible */
619 DBG2("Relayd version is compatible, using protocol version %u.%u",
620 rsock
->major
, rsock
->minor
);
628 * Add stream on the relayd and assign stream handle to the stream_id argument.
630 * On success return 0 else return ret_code negative value.
632 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
636 /* Code flow error. Safety net. */
639 DBG("Relayd sending metadata of size %zu", len
);
642 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
647 DBG2("Relayd metadata added successfully");
650 * After that call, the metadata data MUST be sent to the relayd so the
651 * receive size on the other end matches the len of the metadata packet
652 * header. This is why we don't wait for a reply here.
660 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
662 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
664 /* Code flow error. Safety net. */
667 if (!rsock
->sock
.ops
) {
669 * Attempting a connect on a non-initialized socket.
674 DBG3("Relayd connect ...");
676 return rsock
->sock
.ops
->connect(&rsock
->sock
);
680 * Close relayd socket with an allocated lttcomm_relayd_sock.
682 * If no socket operations are found, simply return 0 meaning that everything
683 * is fine. Without operations, the socket can not possibly be opened or used.
684 * This is possible if the socket was allocated but not created. However, the
685 * caller could simply use it to store a valid file descriptor for instance
686 * passed over a Unix socket and call this to cleanup but still without a valid
689 * Return the close returned value. On error, a negative value is usually
690 * returned back from close(2).
692 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
696 /* Code flow error. Safety net. */
699 /* An invalid fd is fine, return success. */
700 if (rsock
->sock
.fd
< 0) {
705 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
707 if (rsock
->sock
.ops
) {
708 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
710 /* Default call if no specific ops found. */
711 ret
= close(rsock
->sock
.fd
);
713 PERROR("relayd_close default close");
723 * Send data header structure to the relayd.
725 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
726 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
730 /* Code flow error. Safety net. */
734 if (rsock
->sock
.fd
< 0) {
738 DBG3("Relayd sending data header of size %zu", size
);
740 /* Again, safety net */
742 size
= sizeof(struct lttcomm_relayd_data_hdr
);
745 /* Only send data header. */
746 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
753 * The data MUST be sent right after that command for the receive on the
754 * other end to match the size in the header.
762 * Send close stream command to the relayd.
764 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
765 uint64_t last_net_seq_num
)
768 struct lttcomm_relayd_close_stream msg
;
769 struct lttcomm_relayd_generic_reply reply
;
771 /* Code flow error. Safety net. */
774 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
776 memset(&msg
, 0, sizeof(msg
));
777 msg
.stream_id
= htobe64(stream_id
);
778 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
781 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
786 /* Receive response */
787 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
792 reply
.ret_code
= be32toh(reply
.ret_code
);
794 /* Return session id or negative ret code. */
795 if (reply
.ret_code
!= LTTNG_OK
) {
797 ERR("Relayd close stream replied error %d", reply
.ret_code
);
803 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
810 * Check for data availability for a given stream id.
812 * Return 0 if NOT pending, 1 if so and a negative value on error.
814 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
815 uint64_t last_net_seq_num
)
818 struct lttcomm_relayd_data_pending msg
;
819 struct lttcomm_relayd_generic_reply reply
;
821 /* Code flow error. Safety net. */
824 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
826 memset(&msg
, 0, sizeof(msg
));
827 msg
.stream_id
= htobe64(stream_id
);
828 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
831 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
837 /* Receive response */
838 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
843 reply
.ret_code
= be32toh(reply
.ret_code
);
845 /* Return session id or negative ret code. */
846 if (reply
.ret_code
>= LTTNG_OK
) {
847 ERR("Relayd data pending replied error %d", reply
.ret_code
);
850 /* At this point, the ret code is either 1 or 0 */
851 ret
= reply
.ret_code
;
853 DBG("Relayd data is %s pending for stream id %" PRIu64
,
854 ret
== 1 ? "" : "NOT", stream_id
);
861 * Check on the relayd side for a quiescent state on the control socket.
863 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
864 uint64_t metadata_stream_id
)
867 struct lttcomm_relayd_quiescent_control msg
;
868 struct lttcomm_relayd_generic_reply reply
;
870 /* Code flow error. Safety net. */
873 DBG("Relayd checking quiescent control state");
875 memset(&msg
, 0, sizeof(msg
));
876 msg
.stream_id
= htobe64(metadata_stream_id
);
879 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
884 /* Receive response */
885 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
890 reply
.ret_code
= be32toh(reply
.ret_code
);
892 /* Return session id or negative ret code. */
893 if (reply
.ret_code
!= LTTNG_OK
) {
895 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
899 /* Control socket is quiescent */
907 * Begin a data pending command for a specific session id.
909 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
912 struct lttcomm_relayd_begin_data_pending msg
;
913 struct lttcomm_relayd_generic_reply reply
;
915 /* Code flow error. Safety net. */
918 DBG("Relayd begin data pending");
920 memset(&msg
, 0, sizeof(msg
));
921 msg
.session_id
= htobe64(id
);
924 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
929 /* Receive response */
930 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
935 reply
.ret_code
= be32toh(reply
.ret_code
);
937 /* Return session id or negative ret code. */
938 if (reply
.ret_code
!= LTTNG_OK
) {
940 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
951 * End a data pending command for a specific session id.
953 * Return 0 on success and set is_data_inflight to 0 if no data is being
954 * streamed or 1 if it is the case.
956 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
957 unsigned int *is_data_inflight
)
960 struct lttcomm_relayd_end_data_pending msg
;
961 struct lttcomm_relayd_generic_reply reply
;
963 /* Code flow error. Safety net. */
966 DBG("Relayd end data pending");
968 memset(&msg
, 0, sizeof(msg
));
969 msg
.session_id
= htobe64(id
);
972 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
977 /* Receive response */
978 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
983 recv_ret
= be32toh(reply
.ret_code
);
989 *is_data_inflight
= recv_ret
;
991 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
1000 * Send index to the relayd.
1002 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
1003 struct ctf_packet_index
*index
, uint64_t relay_stream_id
,
1004 uint64_t net_seq_num
)
1007 struct lttcomm_relayd_index msg
;
1008 struct lttcomm_relayd_generic_reply reply
;
1010 /* Code flow error. Safety net. */
1013 if (rsock
->minor
< 4) {
1014 DBG("Not sending indexes before protocol 2.4");
1019 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
1021 memset(&msg
, 0, sizeof(msg
));
1022 msg
.relay_stream_id
= htobe64(relay_stream_id
);
1023 msg
.net_seq_num
= htobe64(net_seq_num
);
1025 /* The index is already in big endian. */
1026 msg
.packet_size
= index
->packet_size
;
1027 msg
.content_size
= index
->content_size
;
1028 msg
.timestamp_begin
= index
->timestamp_begin
;
1029 msg
.timestamp_end
= index
->timestamp_end
;
1030 msg
.events_discarded
= index
->events_discarded
;
1031 msg
.stream_id
= index
->stream_id
;
1033 if (rsock
->minor
>= 8) {
1034 msg
.stream_instance_id
= index
->stream_instance_id
;
1035 msg
.packet_seq_num
= index
->packet_seq_num
;
1039 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
,
1040 lttcomm_relayd_index_len(lttng_to_index_major(rsock
->major
,
1042 lttng_to_index_minor(rsock
->major
, rsock
->minor
)),
1048 /* Receive response */
1049 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1054 reply
.ret_code
= be32toh(reply
.ret_code
);
1056 /* Return session id or negative ret code. */
1057 if (reply
.ret_code
!= LTTNG_OK
) {
1059 ERR("Relayd send index replied error %d", reply
.ret_code
);
1070 * Ask the relay to reset the metadata trace file (regeneration).
1072 int relayd_reset_metadata(struct lttcomm_relayd_sock
*rsock
,
1073 uint64_t stream_id
, uint64_t version
)
1076 struct lttcomm_relayd_reset_metadata msg
;
1077 struct lttcomm_relayd_generic_reply reply
;
1079 /* Code flow error. Safety net. */
1082 /* Should have been prevented by the sessiond. */
1083 if (rsock
->minor
< 8) {
1084 ERR("Metadata regeneration unsupported before 2.8");
1089 DBG("Relayd reset metadata stream id %" PRIu64
, stream_id
);
1091 memset(&msg
, 0, sizeof(msg
));
1092 msg
.stream_id
= htobe64(stream_id
);
1093 msg
.version
= htobe64(version
);
1096 ret
= send_command(rsock
, RELAYD_RESET_METADATA
, (void *) &msg
, sizeof(msg
), 0);
1101 /* Receive response */
1102 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1107 reply
.ret_code
= be32toh(reply
.ret_code
);
1109 /* Return session id or negative ret code. */
1110 if (reply
.ret_code
!= LTTNG_OK
) {
1112 ERR("Relayd reset metadata replied error %d", reply
.ret_code
);
1118 DBG("Relayd reset metadata stream id %" PRIu64
" successfully", stream_id
);
1124 int relayd_rotate_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
1125 uint64_t new_chunk_id
, uint64_t seq_num
)
1128 struct lttcomm_relayd_rotate_stream
*msg
= NULL
;
1129 struct lttcomm_relayd_generic_reply reply
;
1133 char *new_pathname
= NULL
;
1135 /* Code flow error. Safety net. */
1138 DBG("Sending rotate stream id %" PRIu64
" command to relayd", stream_id
);
1140 /* Account for the trailing NULL. */
1141 len
= lttng_strnlen(new_pathname
, LTTNG_PATH_MAX
) + 1;
1142 if (len
> LTTNG_PATH_MAX
) {
1143 ERR("Path used in relayd rotate stream command exceeds the maximal allowed length");
1148 msg_len
= offsetof(struct lttcomm_relayd_rotate_stream
, new_pathname
) + len
;
1149 msg
= zmalloc(msg_len
);
1151 PERROR("Failed to allocate relayd rotate stream command of %d bytes",
1157 if (lttng_strncpy(msg
->new_pathname
, new_pathname
, len
)) {
1159 ERR("Failed to copy relayd rotate stream command's new path name");
1163 msg
->pathname_length
= htobe32(len
);
1164 msg
->stream_id
= htobe64(stream_id
);
1165 msg
->new_chunk_id
= htobe64(new_chunk_id
);
1167 * The seq_num is invalid for metadata streams, but it is ignored on
1170 msg
->rotate_at_seq_num
= htobe64(seq_num
);
1173 ret
= send_command(rsock
, RELAYD_ROTATE_STREAM
, (void *) msg
, msg_len
, 0);
1175 ERR("Send rotate command");
1179 /* Receive response. */
1180 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1182 ERR("Receive rotate reply");
1186 reply
.ret_code
= be32toh(reply
.ret_code
);
1188 /* Return session id or negative ret code. */
1189 if (reply
.ret_code
!= LTTNG_OK
) {
1191 ERR("Relayd rotate stream replied error %d", reply
.ret_code
);
1195 DBG("Relayd rotated stream id %" PRIu64
" successfully", stream_id
);
1203 int relayd_create_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1204 struct lttng_trace_chunk
*chunk
)
1207 enum lttng_trace_chunk_status status
;
1208 struct lttcomm_relayd_create_trace_chunk msg
= {};
1209 struct lttcomm_relayd_generic_reply reply
= {};
1210 struct lttng_dynamic_buffer payload
;
1212 time_t creation_timestamp
;
1213 const char *chunk_name
;
1214 size_t chunk_name_length
;
1215 bool overriden_name
;
1217 lttng_dynamic_buffer_init(&payload
);
1219 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1220 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1225 status
= lttng_trace_chunk_get_creation_timestamp(
1226 chunk
, &creation_timestamp
);
1227 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1232 status
= lttng_trace_chunk_get_name(
1233 chunk
, &chunk_name
, &overriden_name
);
1234 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
&&
1235 status
!= LTTNG_TRACE_CHUNK_STATUS_NONE
) {
1240 chunk_name_length
= overriden_name
? (strlen(chunk_name
) + 1) : 0;
1241 msg
= (typeof(msg
)){
1242 .chunk_id
= htobe64(chunk_id
),
1243 .creation_timestamp
= htobe64((uint64_t) creation_timestamp
),
1244 .override_name_length
= htobe32((uint32_t) chunk_name_length
),
1247 ret
= lttng_dynamic_buffer_append(&payload
, &msg
, sizeof(msg
));
1251 if (chunk_name_length
) {
1252 ret
= lttng_dynamic_buffer_append(
1253 &payload
, chunk_name
, chunk_name_length
);
1259 ret
= send_command(sock
, RELAYD_CREATE_TRACE_CHUNK
, payload
.data
,
1262 ERR("Failed to send trace chunk creation command to relay daemon");
1266 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1268 ERR("Failed to receive relay daemon trace chunk creation command reply");
1272 reply
.ret_code
= be32toh(reply
.ret_code
);
1273 if (reply
.ret_code
!= LTTNG_OK
) {
1275 ERR("Relayd trace chunk create replied error %d",
1279 DBG("Relayd successfully created trace chunk: chunk_id = %" PRIu64
,
1284 lttng_dynamic_buffer_reset(&payload
);
1288 int relayd_close_trace_chunk(struct lttcomm_relayd_sock
*sock
,
1289 struct lttng_trace_chunk
*chunk
)
1292 enum lttng_trace_chunk_status status
;
1293 struct lttcomm_relayd_close_trace_chunk msg
= {};
1294 struct lttcomm_relayd_generic_reply reply
= {};
1296 time_t close_timestamp
;
1297 LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type
) close_command
= {};
1299 status
= lttng_trace_chunk_get_id(chunk
, &chunk_id
);
1300 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1301 ERR("Failed to get trace chunk id");
1306 status
= lttng_trace_chunk_get_close_timestamp(chunk
, &close_timestamp
);
1307 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1308 ERR("Failed to get trace chunk close timestamp");
1313 status
= lttng_trace_chunk_get_close_command(chunk
,
1314 &close_command
.value
);
1316 case LTTNG_TRACE_CHUNK_STATUS_OK
:
1317 close_command
.is_set
= 1;
1319 case LTTNG_TRACE_CHUNK_STATUS_NONE
:
1322 ERR("Failed to get trace chunk close command");
1327 msg
= (typeof(msg
)){
1328 .chunk_id
= htobe64(chunk_id
),
1329 .close_timestamp
= htobe64((uint64_t) close_timestamp
),
1331 .value
= htobe32((uint32_t) close_command
.value
),
1332 .is_set
= close_command
.is_set
,
1336 ret
= send_command(sock
, RELAYD_CLOSE_TRACE_CHUNK
, &msg
, sizeof(msg
),
1339 ERR("Failed to send trace chunk close command to relay daemon");
1343 ret
= recv_reply(sock
, &reply
, sizeof(reply
));
1345 ERR("Failed to receive relay daemon trace chunk close command reply");
1349 reply
.ret_code
= be32toh(reply
.ret_code
);
1350 if (reply
.ret_code
!= LTTNG_OK
) {
1352 ERR("Relayd trace chunk close replied error %d",
1356 DBG("Relayd successfully closed trace chunk: chunk_id = %" PRIu64
,