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
)
135 struct lttcomm_relayd_create_session_2_11
*msg
= NULL
;
136 size_t session_name_len
;
140 /* The two names are sent with a '\0' delimiter between them. */
141 session_name_len
= strlen(session_name
) + 1;
142 hostname_len
= strlen(hostname
) + 1;
144 msg_length
= sizeof(*msg
) + session_name_len
+ hostname_len
;
145 msg
= zmalloc(msg_length
);
147 PERROR("zmalloc create_session_2_11 command message");
152 assert(session_name_len
<= UINT32_MAX
);
153 msg
->session_name_len
= htobe32(session_name_len
);
155 assert(hostname_len
<= UINT32_MAX
);
156 msg
->hostname_len
= htobe32(hostname_len
);
158 if (lttng_strncpy(msg
->names
, session_name
, session_name_len
)) {
162 if (lttng_strncpy(msg
->names
+ session_name_len
, hostname
, hostname_len
)) {
167 msg
->live_timer
= htobe32(session_live_timer
);
168 msg
->snapshot
= !!snapshot
;
170 lttng_uuid_copy(msg
->sessiond_uuid
, sessiond_uuid
);
171 msg
->session_id
= htobe64(sessiond_session_id
);
174 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, msg
, msg_length
, 0);
183 * From 2.4 to 2.10, RELAYD_CREATE_SESSION takes additional parameters to
184 * support the live reading capability.
186 static int relayd_create_session_2_4(struct lttcomm_relayd_sock
*rsock
,
187 const char *session_name
, const char *hostname
,
188 int session_live_timer
, unsigned int snapshot
)
191 struct lttcomm_relayd_create_session_2_4 msg
;
193 if (lttng_strncpy(msg
.session_name
, session_name
,
194 sizeof(msg
.session_name
))) {
198 if (lttng_strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
))) {
202 msg
.live_timer
= htobe32(session_live_timer
);
203 msg
.snapshot
= htobe32(snapshot
);
206 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, &msg
, sizeof(msg
), 0);
216 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
218 static int relayd_create_session_2_1(struct lttcomm_relayd_sock
*rsock
)
223 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
233 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
234 * set session_id of the relayd if we have a successful reply from the relayd.
236 * On success, return 0 else a negative value which is either an errno error or
237 * a lttng error code from the relayd.
239 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
,
240 uint64_t *relayd_session_id
,
241 const char *session_name
, const char *hostname
,
242 int session_live_timer
,
243 unsigned int snapshot
, uint64_t sessiond_session_id
,
244 const lttng_uuid sessiond_uuid
)
247 struct lttcomm_relayd_status_session reply
;
250 assert(relayd_session_id
);
252 DBG("Relayd create session");
254 if (rsock
->minor
< 4) {
255 /* From 2.1 to 2.3 */
256 ret
= relayd_create_session_2_1(rsock
);
257 } else if (rsock
->minor
>= 4 && rsock
->minor
< 11) {
258 /* From 2.4 to 2.10 */
259 ret
= relayd_create_session_2_4(rsock
, session_name
,
260 hostname
, session_live_timer
, snapshot
);
262 /* From 2.11 to ... */
263 ret
= relayd_create_session_2_11(rsock
, session_name
,
264 hostname
, session_live_timer
, snapshot
,
265 sessiond_session_id
, sessiond_uuid
);
272 /* Receive response */
273 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
278 reply
.session_id
= be64toh(reply
.session_id
);
279 reply
.ret_code
= be32toh(reply
.ret_code
);
281 /* Return session id or negative ret code. */
282 if (reply
.ret_code
!= LTTNG_OK
) {
284 ERR("Relayd create session replied error %d", reply
.ret_code
);
288 *relayd_session_id
= reply
.session_id
;
291 DBG("Relayd session created with id %" PRIu64
, reply
.session_id
);
297 static int relayd_add_stream_2_1(struct lttcomm_relayd_sock
*rsock
,
298 const char *channel_name
, const char *pathname
)
301 struct lttcomm_relayd_add_stream msg
;
303 memset(&msg
, 0, sizeof(msg
));
304 if (lttng_strncpy(msg
.channel_name
, channel_name
,
305 sizeof(msg
.channel_name
))) {
310 if (lttng_strncpy(msg
.pathname
, pathname
,
311 sizeof(msg
.pathname
))) {
317 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
327 static int relayd_add_stream_2_2(struct lttcomm_relayd_sock
*rsock
,
328 const char *channel_name
, const char *pathname
,
329 uint64_t tracefile_size
, uint64_t tracefile_count
)
332 struct lttcomm_relayd_add_stream_2_2 msg
;
334 memset(&msg
, 0, sizeof(msg
));
335 /* Compat with relayd 2.2 to 2.10 */
336 if (lttng_strncpy(msg
.channel_name
, channel_name
,
337 sizeof(msg
.channel_name
))) {
341 if (lttng_strncpy(msg
.pathname
, pathname
,
342 sizeof(msg
.pathname
))) {
346 msg
.tracefile_size
= htobe64(tracefile_size
);
347 msg
.tracefile_count
= htobe64(tracefile_count
);
350 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
359 static int relayd_add_stream_2_11(struct lttcomm_relayd_sock
*rsock
,
360 const char *channel_name
, const char *pathname
,
361 uint64_t tracefile_size
, uint64_t tracefile_count
,
362 uint64_t trace_archive_id
)
365 struct lttcomm_relayd_add_stream_2_11
*msg
= NULL
;
366 size_t channel_name_len
;
370 /* The two names are sent with a '\0' delimiter between them. */
371 channel_name_len
= strlen(channel_name
) + 1;
372 pathname_len
= strlen(pathname
) + 1;
374 msg_length
= sizeof(*msg
) + channel_name_len
+ pathname_len
;
375 msg
= zmalloc(msg_length
);
377 PERROR("zmalloc add_stream_2_11 command message");
382 assert(channel_name_len
<= UINT32_MAX
);
383 msg
->channel_name_len
= htobe32(channel_name_len
);
385 assert(pathname_len
<= UINT32_MAX
);
386 msg
->pathname_len
= htobe32(pathname_len
);
388 if (lttng_strncpy(msg
->names
, channel_name
, channel_name_len
)) {
392 if (lttng_strncpy(msg
->names
+ channel_name_len
, pathname
, pathname_len
)) {
397 msg
->tracefile_size
= htobe64(tracefile_size
);
398 msg
->tracefile_count
= htobe64(tracefile_count
);
399 msg
->trace_archive_id
= htobe64(trace_archive_id
);
402 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) msg
, msg_length
, 0);
413 * Add stream on the relayd and assign stream handle to the stream_id argument.
415 * On success return 0 else return ret_code negative value.
417 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
418 const char *pathname
, uint64_t *stream_id
,
419 uint64_t tracefile_size
, uint64_t tracefile_count
,
420 struct lttng_trace_chunk
*trace_chunk
)
423 struct lttcomm_relayd_status_stream reply
;
425 /* Code flow error. Safety net. */
427 assert(channel_name
);
430 DBG("Relayd adding stream for channel name %s", channel_name
);
432 /* Compat with relayd 2.1 */
433 if (rsock
->minor
== 1) {
435 assert(!trace_chunk
);
436 ret
= relayd_add_stream_2_1(rsock
, channel_name
, pathname
);
438 } else if (rsock
->minor
> 1 && rsock
->minor
< 11) {
439 /* From 2.2 to 2.10 */
440 assert(!trace_chunk
);
441 ret
= relayd_add_stream_2_2(rsock
, channel_name
, pathname
,
442 tracefile_size
, tracefile_count
);
444 enum lttng_trace_chunk_status chunk_status
;
448 chunk_status
= lttng_trace_chunk_get_id(trace_chunk
,
450 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
452 /* From 2.11 to ...*/
453 ret
= relayd_add_stream_2_11(rsock
, channel_name
, pathname
,
454 tracefile_size
, tracefile_count
,
463 /* Waiting for reply */
464 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
469 /* Back to host bytes order. */
470 reply
.handle
= be64toh(reply
.handle
);
471 reply
.ret_code
= be32toh(reply
.ret_code
);
473 /* Return session id or negative ret code. */
474 if (reply
.ret_code
!= LTTNG_OK
) {
476 ERR("Relayd add stream replied error %d", reply
.ret_code
);
480 *stream_id
= reply
.handle
;
483 DBG("Relayd stream added successfully with handle %" PRIu64
,
491 * Inform the relay that all the streams for the current channel has been sent.
493 * On success return 0 else return ret_code negative value.
495 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
498 struct lttcomm_relayd_generic_reply reply
;
500 /* Code flow error. Safety net. */
503 DBG("Relayd sending streams sent.");
505 /* This feature was introduced in 2.4, ignore it for earlier versions. */
506 if (rsock
->minor
< 4) {
512 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, NULL
, 0, 0);
517 /* Waiting for reply */
518 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
523 /* Back to host bytes order. */
524 reply
.ret_code
= be32toh(reply
.ret_code
);
526 /* Return session id or negative ret code. */
527 if (reply
.ret_code
!= LTTNG_OK
) {
529 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
536 DBG("Relayd streams sent success");
544 * Check version numbers on the relayd.
545 * If major versions are compatible, we assign minor_to_use to the
546 * minor version of the procotol we are going to use for this session.
548 * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL
549 * otherwise, or a negative value on network errors.
551 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
554 struct lttcomm_relayd_version msg
;
556 /* Code flow error. Safety net. */
559 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
562 memset(&msg
, 0, sizeof(msg
));
563 /* Prepare network byte order before transmission. */
564 msg
.major
= htobe32(rsock
->major
);
565 msg
.minor
= htobe32(rsock
->minor
);
568 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
573 /* Receive response */
574 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
579 /* Set back to host bytes order */
580 msg
.major
= be32toh(msg
.major
);
581 msg
.minor
= be32toh(msg
.minor
);
584 * Only validate the major version. If the other side is higher,
585 * communication is not possible. Only major version equal can talk to each
586 * other. If the minor version differs, the lowest version is used by both
589 if (msg
.major
!= rsock
->major
) {
591 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
592 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
593 msg
.major
, rsock
->major
);
598 * If the relayd's minor version is higher, it will adapt to our version so
599 * we can continue to use the latest relayd communication data structure.
600 * If the received minor version is higher, the relayd should adapt to us.
602 if (rsock
->minor
> msg
.minor
) {
603 rsock
->minor
= msg
.minor
;
606 /* Version number compatible */
607 DBG2("Relayd version is compatible, using protocol version %u.%u",
608 rsock
->major
, rsock
->minor
);
616 * Add stream on the relayd and assign stream handle to the stream_id argument.
618 * On success return 0 else return ret_code negative value.
620 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
624 /* Code flow error. Safety net. */
627 DBG("Relayd sending metadata of size %zu", len
);
630 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
635 DBG2("Relayd metadata added successfully");
638 * After that call, the metadata data MUST be sent to the relayd so the
639 * receive size on the other end matches the len of the metadata packet
640 * header. This is why we don't wait for a reply here.
648 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
650 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
652 /* Code flow error. Safety net. */
655 if (!rsock
->sock
.ops
) {
657 * Attempting a connect on a non-initialized socket.
662 DBG3("Relayd connect ...");
664 return rsock
->sock
.ops
->connect(&rsock
->sock
);
668 * Close relayd socket with an allocated lttcomm_relayd_sock.
670 * If no socket operations are found, simply return 0 meaning that everything
671 * is fine. Without operations, the socket can not possibly be opened or used.
672 * This is possible if the socket was allocated but not created. However, the
673 * caller could simply use it to store a valid file descriptor for instance
674 * passed over a Unix socket and call this to cleanup but still without a valid
677 * Return the close returned value. On error, a negative value is usually
678 * returned back from close(2).
680 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
684 /* Code flow error. Safety net. */
687 /* An invalid fd is fine, return success. */
688 if (rsock
->sock
.fd
< 0) {
693 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
695 if (rsock
->sock
.ops
) {
696 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
698 /* Default call if no specific ops found. */
699 ret
= close(rsock
->sock
.fd
);
701 PERROR("relayd_close default close");
711 * Send data header structure to the relayd.
713 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
714 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
718 /* Code flow error. Safety net. */
722 if (rsock
->sock
.fd
< 0) {
726 DBG3("Relayd sending data header of size %zu", size
);
728 /* Again, safety net */
730 size
= sizeof(struct lttcomm_relayd_data_hdr
);
733 /* Only send data header. */
734 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
741 * The data MUST be sent right after that command for the receive on the
742 * other end to match the size in the header.
750 * Send close stream command to the relayd.
752 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
753 uint64_t last_net_seq_num
)
756 struct lttcomm_relayd_close_stream msg
;
757 struct lttcomm_relayd_generic_reply reply
;
759 /* Code flow error. Safety net. */
762 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
764 memset(&msg
, 0, sizeof(msg
));
765 msg
.stream_id
= htobe64(stream_id
);
766 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
769 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
774 /* Receive response */
775 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
780 reply
.ret_code
= be32toh(reply
.ret_code
);
782 /* Return session id or negative ret code. */
783 if (reply
.ret_code
!= LTTNG_OK
) {
785 ERR("Relayd close stream replied error %d", reply
.ret_code
);
791 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
798 * Check for data availability for a given stream id.
800 * Return 0 if NOT pending, 1 if so and a negative value on error.
802 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
803 uint64_t last_net_seq_num
)
806 struct lttcomm_relayd_data_pending msg
;
807 struct lttcomm_relayd_generic_reply reply
;
809 /* Code flow error. Safety net. */
812 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
814 memset(&msg
, 0, sizeof(msg
));
815 msg
.stream_id
= htobe64(stream_id
);
816 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
819 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
825 /* Receive response */
826 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
831 reply
.ret_code
= be32toh(reply
.ret_code
);
833 /* Return session id or negative ret code. */
834 if (reply
.ret_code
>= LTTNG_OK
) {
835 ERR("Relayd data pending replied error %d", reply
.ret_code
);
838 /* At this point, the ret code is either 1 or 0 */
839 ret
= reply
.ret_code
;
841 DBG("Relayd data is %s pending for stream id %" PRIu64
,
842 ret
== 1 ? "" : "NOT", stream_id
);
849 * Check on the relayd side for a quiescent state on the control socket.
851 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
852 uint64_t metadata_stream_id
)
855 struct lttcomm_relayd_quiescent_control msg
;
856 struct lttcomm_relayd_generic_reply reply
;
858 /* Code flow error. Safety net. */
861 DBG("Relayd checking quiescent control state");
863 memset(&msg
, 0, sizeof(msg
));
864 msg
.stream_id
= htobe64(metadata_stream_id
);
867 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
872 /* Receive response */
873 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
878 reply
.ret_code
= be32toh(reply
.ret_code
);
880 /* Return session id or negative ret code. */
881 if (reply
.ret_code
!= LTTNG_OK
) {
883 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
887 /* Control socket is quiescent */
895 * Begin a data pending command for a specific session id.
897 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
900 struct lttcomm_relayd_begin_data_pending msg
;
901 struct lttcomm_relayd_generic_reply reply
;
903 /* Code flow error. Safety net. */
906 DBG("Relayd begin data pending");
908 memset(&msg
, 0, sizeof(msg
));
909 msg
.session_id
= htobe64(id
);
912 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
917 /* Receive response */
918 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
923 reply
.ret_code
= be32toh(reply
.ret_code
);
925 /* Return session id or negative ret code. */
926 if (reply
.ret_code
!= LTTNG_OK
) {
928 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
939 * End a data pending command for a specific session id.
941 * Return 0 on success and set is_data_inflight to 0 if no data is being
942 * streamed or 1 if it is the case.
944 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
945 unsigned int *is_data_inflight
)
948 struct lttcomm_relayd_end_data_pending msg
;
949 struct lttcomm_relayd_generic_reply reply
;
951 /* Code flow error. Safety net. */
954 DBG("Relayd end data pending");
956 memset(&msg
, 0, sizeof(msg
));
957 msg
.session_id
= htobe64(id
);
960 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
965 /* Receive response */
966 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
971 recv_ret
= be32toh(reply
.ret_code
);
977 *is_data_inflight
= recv_ret
;
979 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
988 * Send index to the relayd.
990 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
991 struct ctf_packet_index
*index
, uint64_t relay_stream_id
,
992 uint64_t net_seq_num
)
995 struct lttcomm_relayd_index msg
;
996 struct lttcomm_relayd_generic_reply reply
;
998 /* Code flow error. Safety net. */
1001 if (rsock
->minor
< 4) {
1002 DBG("Not sending indexes before protocol 2.4");
1007 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
1009 memset(&msg
, 0, sizeof(msg
));
1010 msg
.relay_stream_id
= htobe64(relay_stream_id
);
1011 msg
.net_seq_num
= htobe64(net_seq_num
);
1013 /* The index is already in big endian. */
1014 msg
.packet_size
= index
->packet_size
;
1015 msg
.content_size
= index
->content_size
;
1016 msg
.timestamp_begin
= index
->timestamp_begin
;
1017 msg
.timestamp_end
= index
->timestamp_end
;
1018 msg
.events_discarded
= index
->events_discarded
;
1019 msg
.stream_id
= index
->stream_id
;
1021 if (rsock
->minor
>= 8) {
1022 msg
.stream_instance_id
= index
->stream_instance_id
;
1023 msg
.packet_seq_num
= index
->packet_seq_num
;
1027 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
,
1028 lttcomm_relayd_index_len(lttng_to_index_major(rsock
->major
,
1030 lttng_to_index_minor(rsock
->major
, rsock
->minor
)),
1036 /* Receive response */
1037 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1042 reply
.ret_code
= be32toh(reply
.ret_code
);
1044 /* Return session id or negative ret code. */
1045 if (reply
.ret_code
!= LTTNG_OK
) {
1047 ERR("Relayd send index replied error %d", reply
.ret_code
);
1058 * Ask the relay to reset the metadata trace file (regeneration).
1060 int relayd_reset_metadata(struct lttcomm_relayd_sock
*rsock
,
1061 uint64_t stream_id
, uint64_t version
)
1064 struct lttcomm_relayd_reset_metadata msg
;
1065 struct lttcomm_relayd_generic_reply reply
;
1067 /* Code flow error. Safety net. */
1070 /* Should have been prevented by the sessiond. */
1071 if (rsock
->minor
< 8) {
1072 ERR("Metadata regeneration unsupported before 2.8");
1077 DBG("Relayd reset metadata stream id %" PRIu64
, stream_id
);
1079 memset(&msg
, 0, sizeof(msg
));
1080 msg
.stream_id
= htobe64(stream_id
);
1081 msg
.version
= htobe64(version
);
1084 ret
= send_command(rsock
, RELAYD_RESET_METADATA
, (void *) &msg
, sizeof(msg
), 0);
1089 /* Receive response */
1090 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1095 reply
.ret_code
= be32toh(reply
.ret_code
);
1097 /* Return session id or negative ret code. */
1098 if (reply
.ret_code
!= LTTNG_OK
) {
1100 ERR("Relayd reset metadata replied error %d", reply
.ret_code
);
1106 DBG("Relayd reset metadata stream id %" PRIu64
" successfully", stream_id
);
1112 int relayd_rotate_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
1113 uint64_t new_chunk_id
, uint64_t seq_num
)
1116 struct lttcomm_relayd_rotate_stream
*msg
= NULL
;
1117 struct lttcomm_relayd_generic_reply reply
;
1121 char *new_pathname
= NULL
;
1123 /* Code flow error. Safety net. */
1126 DBG("Sending rotate stream id %" PRIu64
" command to relayd", stream_id
);
1128 /* Account for the trailing NULL. */
1129 len
= lttng_strnlen(new_pathname
, LTTNG_PATH_MAX
) + 1;
1130 if (len
> LTTNG_PATH_MAX
) {
1131 ERR("Path used in relayd rotate stream command exceeds the maximal allowed length");
1136 msg_len
= offsetof(struct lttcomm_relayd_rotate_stream
, new_pathname
) + len
;
1137 msg
= zmalloc(msg_len
);
1139 PERROR("Failed to allocate relayd rotate stream command of %d bytes",
1145 if (lttng_strncpy(msg
->new_pathname
, new_pathname
, len
)) {
1147 ERR("Failed to copy relayd rotate stream command's new path name");
1151 msg
->pathname_length
= htobe32(len
);
1152 msg
->stream_id
= htobe64(stream_id
);
1153 msg
->new_chunk_id
= htobe64(new_chunk_id
);
1155 * The seq_num is invalid for metadata streams, but it is ignored on
1158 msg
->rotate_at_seq_num
= htobe64(seq_num
);
1161 ret
= send_command(rsock
, RELAYD_ROTATE_STREAM
, (void *) msg
, msg_len
, 0);
1163 ERR("Send rotate command");
1167 /* Receive response. */
1168 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1170 ERR("Receive rotate reply");
1174 reply
.ret_code
= be32toh(reply
.ret_code
);
1176 /* Return session id or negative ret code. */
1177 if (reply
.ret_code
!= LTTNG_OK
) {
1179 ERR("Relayd rotate stream replied error %d", reply
.ret_code
);
1183 DBG("Relayd rotated stream id %" PRIu64
" successfully", stream_id
);
1191 int relayd_rotate_rename(struct lttcomm_relayd_sock
*rsock
,
1192 const char *old_path
, const char *new_path
)
1195 struct lttcomm_relayd_rotate_rename
*msg
= NULL
;
1196 struct lttcomm_relayd_generic_reply reply
;
1197 size_t old_path_length
, new_path_length
;
1200 /* Code flow error. Safety net. */
1203 DBG("Relayd rename chunk %s to %s", old_path
, new_path
);
1205 /* The two paths are sent with a '\0' delimiter between them. */
1206 old_path_length
= strlen(old_path
) + 1;
1207 new_path_length
= strlen(new_path
) + 1;
1209 msg_length
= sizeof(*msg
) + old_path_length
+ new_path_length
;
1210 msg
= zmalloc(msg_length
);
1212 PERROR("zmalloc rotate-rename command message");
1217 assert(old_path_length
<= UINT32_MAX
);
1218 msg
->old_path_length
= htobe32(old_path_length
);
1220 assert(new_path_length
<= UINT32_MAX
);
1221 msg
->new_path_length
= htobe32(new_path_length
);
1223 strcpy(msg
->paths
, old_path
);
1224 strcpy(msg
->paths
+ old_path_length
, new_path
);
1227 ret
= send_command(rsock
, RELAYD_ROTATE_RENAME
, (const void *) msg
,
1233 /* Receive response */
1234 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1239 reply
.ret_code
= be32toh(reply
.ret_code
);
1241 /* Return session id or negative ret code. */
1242 if (reply
.ret_code
!= LTTNG_OK
) {
1244 ERR("Relayd rotate rename replied error %d", reply
.ret_code
);
1250 DBG("Relayd rotate rename completed successfully");
1257 int relayd_rotate_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t chunk_id
)
1260 struct lttcomm_relayd_rotate_pending msg
;
1261 struct lttcomm_relayd_rotate_pending_reply reply
;
1263 /* Code flow error. Safety net. */
1266 DBG("Querying relayd for rotate pending with chunk_id %" PRIu64
,
1269 memset(&msg
, 0, sizeof(msg
));
1270 msg
.chunk_id
= htobe64(chunk_id
);
1273 ret
= send_command(rsock
, RELAYD_ROTATE_PENDING
, (void *) &msg
,
1279 /* Receive response */
1280 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1285 reply
.generic
.ret_code
= be32toh(reply
.generic
.ret_code
);
1287 /* Return session id or negative ret code. */
1288 if (reply
.generic
.ret_code
!= LTTNG_OK
) {
1289 ret
= -reply
.generic
.ret_code
;
1290 ERR("Relayd rotate pending replied with error %d", ret
);
1293 /* No error, just rotate pending state */
1294 if (reply
.is_pending
== 0 || reply
.is_pending
== 1) {
1295 ret
= reply
.is_pending
;
1296 DBG("Relayd rotate pending command completed successfully with result \"%s\"",
1297 ret
? "rotation pending" : "rotation NOT pending");
1299 ret
= -LTTNG_ERR_UNK
;
1307 int relayd_mkdir(struct lttcomm_relayd_sock
*rsock
, const char *path
)
1310 struct lttcomm_relayd_mkdir
*msg
;
1311 struct lttcomm_relayd_generic_reply reply
;
1314 /* Code flow error. Safety net. */
1317 DBG("Relayd mkdir path %s", path
);
1319 len
= strlen(path
) + 1;
1320 msg
= zmalloc(sizeof(msg
->length
) + len
);
1322 PERROR("Alloc mkdir msg");
1326 msg
->length
= htobe32((uint32_t) len
);
1328 if (lttng_strncpy(msg
->path
, path
, len
)) {
1334 ret
= send_command(rsock
, RELAYD_MKDIR
, (void *) msg
,
1335 sizeof(msg
->length
) + len
, 0);
1340 /* Receive response */
1341 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
1346 reply
.ret_code
= be32toh(reply
.ret_code
);
1348 /* Return session id or negative ret code. */
1349 if (reply
.ret_code
!= LTTNG_OK
) {
1351 ERR("Relayd mkdir replied error %d", reply
.ret_code
);
1357 DBG("Relayd mkdir completed successfully");