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.
27 #include <common/common.h>
28 #include <common/defaults.h>
29 #include <common/compat/endian.h>
30 #include <common/sessiond-comm/relayd.h>
31 #include <common/index/ctf-index.h>
36 * Send command. Fill up the header and append the data.
38 static int send_command(struct lttcomm_relayd_sock
*rsock
,
39 enum lttcomm_relayd_command cmd
, void *data
, size_t size
,
43 struct lttcomm_relayd_hdr header
;
45 uint64_t buf_size
= sizeof(header
);
47 if (rsock
->sock
.fd
< 0) {
55 buf
= zmalloc(buf_size
);
57 PERROR("zmalloc relayd send command buf");
62 memset(&header
, 0, sizeof(header
));
63 header
.cmd
= htobe32(cmd
);
64 header
.data_size
= htobe64(size
);
66 /* Zeroed for now since not used. */
67 header
.cmd_version
= 0;
68 header
.circuit_id
= 0;
70 /* Prepare buffer to send. */
71 memcpy(buf
, &header
, sizeof(header
));
73 memcpy(buf
+ sizeof(header
), data
, size
);
76 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
82 DBG3("Relayd sending command %d of size %" PRIu64
, cmd
, buf_size
);
91 * Receive reply data on socket. This MUST be call after send_command or else
92 * could result in unexpected behavior(s).
94 static int recv_reply(struct lttcomm_relayd_sock
*rsock
, void *data
, size_t size
)
98 if (rsock
->sock
.fd
< 0) {
102 DBG3("Relayd waiting for reply of size %zu", size
);
104 ret
= rsock
->sock
.ops
->recvmsg(&rsock
->sock
, data
, size
, 0);
105 if (ret
<= 0 || ret
!= size
) {
107 /* Orderly shutdown. */
108 DBG("Socket %d has performed an orderly shutdown", rsock
->sock
.fd
);
110 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
111 rsock
->sock
.fd
, size
, ret
);
113 /* Always return -1 here and the caller can use errno. */
123 * Starting at 2.4, RELAYD_CREATE_SESSION takes additional parameters to
124 * support the live reading capability.
126 static int relayd_create_session_2_4(struct lttcomm_relayd_sock
*rsock
,
127 uint64_t *session_id
, char *session_name
, char *hostname
,
128 int session_live_timer
, unsigned int snapshot
)
131 struct lttcomm_relayd_create_session_2_4 msg
;
133 if (lttng_strncpy(msg
.session_name
, session_name
,
134 sizeof(msg
.session_name
))) {
138 if (lttng_strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
))) {
142 msg
.live_timer
= htobe32(session_live_timer
);
143 msg
.snapshot
= htobe32(snapshot
);
146 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, &msg
, sizeof(msg
), 0);
156 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
158 static int relayd_create_session_2_1(struct lttcomm_relayd_sock
*rsock
,
159 uint64_t *session_id
)
164 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
174 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
175 * set session_id of the relayd if we have a successful reply from the relayd.
177 * On success, return 0 else a negative value which is either an errno error or
178 * a lttng error code from the relayd.
180 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
, uint64_t *session_id
,
181 char *session_name
, char *hostname
, int session_live_timer
,
182 unsigned int snapshot
)
185 struct lttcomm_relayd_status_session reply
;
190 DBG("Relayd create session");
192 switch(rsock
->minor
) {
196 ret
= relayd_create_session_2_1(rsock
, session_id
);
200 ret
= relayd_create_session_2_4(rsock
, session_id
, session_name
,
201 hostname
, session_live_timer
, snapshot
);
209 /* Receive response */
210 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
215 reply
.session_id
= be64toh(reply
.session_id
);
216 reply
.ret_code
= be32toh(reply
.ret_code
);
218 /* Return session id or negative ret code. */
219 if (reply
.ret_code
!= LTTNG_OK
) {
221 ERR("Relayd create session replied error %d", reply
.ret_code
);
225 *session_id
= reply
.session_id
;
228 DBG("Relayd session created with id %" PRIu64
, reply
.session_id
);
235 * Add stream on the relayd and assign stream handle to the stream_id argument.
237 * On success return 0 else return ret_code negative value.
239 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
240 const char *pathname
, uint64_t *stream_id
,
241 uint64_t tracefile_size
, uint64_t tracefile_count
)
244 struct lttcomm_relayd_add_stream msg
;
245 struct lttcomm_relayd_add_stream_2_2 msg_2_2
;
246 struct lttcomm_relayd_status_stream reply
;
248 /* Code flow error. Safety net. */
250 assert(channel_name
);
253 DBG("Relayd adding stream for channel name %s", channel_name
);
255 /* Compat with relayd 2.1 */
256 if (rsock
->minor
== 1) {
257 memset(&msg
, 0, sizeof(msg
));
258 if (lttng_strncpy(msg
.channel_name
, channel_name
,
259 sizeof(msg
.channel_name
))) {
263 if (lttng_strncpy(msg
.pathname
, pathname
,
264 sizeof(msg
.pathname
))) {
270 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
275 memset(&msg_2_2
, 0, sizeof(msg_2_2
));
276 /* Compat with relayd 2.2+ */
277 if (lttng_strncpy(msg_2_2
.channel_name
, channel_name
,
278 sizeof(msg_2_2
.channel_name
))) {
282 if (lttng_strncpy(msg_2_2
.pathname
, pathname
,
283 sizeof(msg_2_2
.pathname
))) {
287 msg_2_2
.tracefile_size
= htobe64(tracefile_size
);
288 msg_2_2
.tracefile_count
= htobe64(tracefile_count
);
291 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg_2_2
, sizeof(msg_2_2
), 0);
297 /* Waiting for reply */
298 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
303 /* Back to host bytes order. */
304 reply
.handle
= be64toh(reply
.handle
);
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 add stream replied error %d", reply
.ret_code
);
314 *stream_id
= reply
.handle
;
317 DBG("Relayd stream added successfully with handle %" PRIu64
,
325 * Inform the relay that all the streams for the current channel has been sent.
327 * On success return 0 else return ret_code negative value.
329 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
332 struct lttcomm_relayd_generic_reply reply
;
334 /* Code flow error. Safety net. */
337 DBG("Relayd sending streams sent.");
339 /* This feature was introduced in 2.4, ignore it for earlier versions. */
340 if (rsock
->minor
< 4) {
346 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, NULL
, 0, 0);
351 /* Waiting for reply */
352 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
357 /* Back to host bytes order. */
358 reply
.ret_code
= be32toh(reply
.ret_code
);
360 /* Return session id or negative ret code. */
361 if (reply
.ret_code
!= LTTNG_OK
) {
363 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
370 DBG("Relayd streams sent success");
378 * Check version numbers on the relayd.
379 * If major versions are compatible, we assign minor_to_use to the
380 * minor version of the procotol we are going to use for this session.
382 * Return 0 if compatible else negative value.
384 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
387 struct lttcomm_relayd_version msg
;
389 /* Code flow error. Safety net. */
392 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
395 memset(&msg
, 0, sizeof(msg
));
396 /* Prepare network byte order before transmission. */
397 msg
.major
= htobe32(rsock
->major
);
398 msg
.minor
= htobe32(rsock
->minor
);
401 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
406 /* Receive response */
407 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
412 /* Set back to host bytes order */
413 msg
.major
= be32toh(msg
.major
);
414 msg
.minor
= be32toh(msg
.minor
);
417 * Only validate the major version. If the other side is higher,
418 * communication is not possible. Only major version equal can talk to each
419 * other. If the minor version differs, the lowest version is used by both
422 if (msg
.major
!= rsock
->major
) {
425 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
426 msg
.major
, rsock
->major
);
431 * If the relayd's minor version is higher, it will adapt to our version so
432 * we can continue to use the latest relayd communication data structure.
433 * If the received minor version is higher, the relayd should adapt to us.
435 if (rsock
->minor
> msg
.minor
) {
436 rsock
->minor
= msg
.minor
;
439 /* Version number compatible */
440 DBG2("Relayd version is compatible, using protocol version %u.%u",
441 rsock
->major
, rsock
->minor
);
449 * Add stream on the relayd and assign stream handle to the stream_id argument.
451 * On success return 0 else return ret_code negative value.
453 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
457 /* Code flow error. Safety net. */
460 DBG("Relayd sending metadata of size %zu", len
);
463 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
468 DBG2("Relayd metadata added successfully");
471 * After that call, the metadata data MUST be sent to the relayd so the
472 * receive size on the other end matches the len of the metadata packet
473 * header. This is why we don't wait for a reply here.
481 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
483 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
485 /* Code flow error. Safety net. */
488 if (!rsock
->sock
.ops
) {
490 * Attempting a connect on a non-initialized socket.
495 DBG3("Relayd connect ...");
497 return rsock
->sock
.ops
->connect(&rsock
->sock
);
501 * Close relayd socket with an allocated lttcomm_relayd_sock.
503 * If no socket operations are found, simply return 0 meaning that everything
504 * is fine. Without operations, the socket can not possibly be opened or used.
505 * This is possible if the socket was allocated but not created. However, the
506 * caller could simply use it to store a valid file descriptor for instance
507 * passed over a Unix socket and call this to cleanup but still without a valid
510 * Return the close returned value. On error, a negative value is usually
511 * returned back from close(2).
513 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
517 /* Code flow error. Safety net. */
520 /* An invalid fd is fine, return success. */
521 if (rsock
->sock
.fd
< 0) {
526 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
528 if (rsock
->sock
.ops
) {
529 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
531 /* Default call if no specific ops found. */
532 ret
= close(rsock
->sock
.fd
);
534 PERROR("relayd_close default close");
544 * Send data header structure to the relayd.
546 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
547 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
551 /* Code flow error. Safety net. */
555 if (rsock
->sock
.fd
< 0) {
559 DBG3("Relayd sending data header of size %zu", size
);
561 /* Again, safety net */
563 size
= sizeof(struct lttcomm_relayd_data_hdr
);
566 /* Only send data header. */
567 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
574 * The data MUST be sent right after that command for the receive on the
575 * other end to match the size in the header.
583 * Send close stream command to the relayd.
585 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
586 uint64_t last_net_seq_num
)
589 struct lttcomm_relayd_close_stream msg
;
590 struct lttcomm_relayd_generic_reply reply
;
592 /* Code flow error. Safety net. */
595 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
597 memset(&msg
, 0, sizeof(msg
));
598 msg
.stream_id
= htobe64(stream_id
);
599 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
602 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
607 /* Receive response */
608 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
613 reply
.ret_code
= be32toh(reply
.ret_code
);
615 /* Return session id or negative ret code. */
616 if (reply
.ret_code
!= LTTNG_OK
) {
618 ERR("Relayd close stream replied error %d", reply
.ret_code
);
624 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
631 * Check for data availability for a given stream id.
633 * Return 0 if NOT pending, 1 if so and a negative value on error.
635 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
636 uint64_t last_net_seq_num
)
639 struct lttcomm_relayd_data_pending msg
;
640 struct lttcomm_relayd_generic_reply reply
;
642 /* Code flow error. Safety net. */
645 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
647 memset(&msg
, 0, sizeof(msg
));
648 msg
.stream_id
= htobe64(stream_id
);
649 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
652 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
658 /* Receive response */
659 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
664 reply
.ret_code
= be32toh(reply
.ret_code
);
666 /* Return session id or negative ret code. */
667 if (reply
.ret_code
>= LTTNG_OK
) {
668 ERR("Relayd data pending replied error %d", reply
.ret_code
);
671 /* At this point, the ret code is either 1 or 0 */
672 ret
= reply
.ret_code
;
674 DBG("Relayd data is %s pending for stream id %" PRIu64
,
675 ret
== 1 ? "" : "NOT", stream_id
);
682 * Check on the relayd side for a quiescent state on the control socket.
684 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
685 uint64_t metadata_stream_id
)
688 struct lttcomm_relayd_quiescent_control msg
;
689 struct lttcomm_relayd_generic_reply reply
;
691 /* Code flow error. Safety net. */
694 DBG("Relayd checking quiescent control state");
696 memset(&msg
, 0, sizeof(msg
));
697 msg
.stream_id
= htobe64(metadata_stream_id
);
700 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
705 /* Receive response */
706 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
711 reply
.ret_code
= be32toh(reply
.ret_code
);
713 /* Return session id or negative ret code. */
714 if (reply
.ret_code
!= LTTNG_OK
) {
716 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
720 /* Control socket is quiescent */
728 * Begin a data pending command for a specific session id.
730 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
733 struct lttcomm_relayd_begin_data_pending msg
;
734 struct lttcomm_relayd_generic_reply reply
;
736 /* Code flow error. Safety net. */
739 DBG("Relayd begin data pending");
741 memset(&msg
, 0, sizeof(msg
));
742 msg
.session_id
= htobe64(id
);
745 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
750 /* Receive response */
751 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
756 reply
.ret_code
= be32toh(reply
.ret_code
);
758 /* Return session id or negative ret code. */
759 if (reply
.ret_code
!= LTTNG_OK
) {
761 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
772 * End a data pending command for a specific session id.
774 * Return 0 on success and set is_data_inflight to 0 if no data is being
775 * streamed or 1 if it is the case.
777 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
778 unsigned int *is_data_inflight
)
781 struct lttcomm_relayd_end_data_pending msg
;
782 struct lttcomm_relayd_generic_reply reply
;
784 /* Code flow error. Safety net. */
787 DBG("Relayd end data pending");
789 memset(&msg
, 0, sizeof(msg
));
790 msg
.session_id
= htobe64(id
);
793 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
798 /* Receive response */
799 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
804 recv_ret
= be32toh(reply
.ret_code
);
810 *is_data_inflight
= recv_ret
;
812 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
821 * Send index to the relayd.
823 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
824 struct ctf_packet_index
*index
, uint64_t relay_stream_id
,
825 uint64_t net_seq_num
)
828 struct lttcomm_relayd_index msg
;
829 struct lttcomm_relayd_generic_reply reply
;
831 /* Code flow error. Safety net. */
834 if (rsock
->minor
< 4) {
835 DBG("Not sending indexes before protocol 2.4");
840 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
842 memset(&msg
, 0, sizeof(msg
));
843 msg
.relay_stream_id
= htobe64(relay_stream_id
);
844 msg
.net_seq_num
= htobe64(net_seq_num
);
846 /* The index is already in big endian. */
847 msg
.packet_size
= index
->packet_size
;
848 msg
.content_size
= index
->content_size
;
849 msg
.timestamp_begin
= index
->timestamp_begin
;
850 msg
.timestamp_end
= index
->timestamp_end
;
851 msg
.events_discarded
= index
->events_discarded
;
852 msg
.stream_id
= index
->stream_id
;
855 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
, sizeof(msg
), 0);
860 /* Receive response */
861 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
866 reply
.ret_code
= be32toh(reply
.ret_code
);
868 /* Return session id or negative ret code. */
869 if (reply
.ret_code
!= LTTNG_OK
) {
871 ERR("Relayd send index replied error %d", reply
.ret_code
);