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/sessiond-comm/relayd.h>
30 #include <common/index/ctf-index.h>
35 * Send command. Fill up the header and append the data.
37 static int send_command(struct lttcomm_relayd_sock
*rsock
,
38 enum lttcomm_relayd_command cmd
, void *data
, size_t size
,
42 struct lttcomm_relayd_hdr header
;
44 uint64_t buf_size
= sizeof(header
);
46 if (rsock
->sock
.fd
< 0) {
54 buf
= zmalloc(buf_size
);
56 PERROR("zmalloc relayd send command buf");
61 memset(&header
, 0, sizeof(header
));
62 header
.cmd
= htobe32(cmd
);
63 header
.data_size
= htobe64(size
);
65 /* Zeroed for now since not used. */
66 header
.cmd_version
= 0;
67 header
.circuit_id
= 0;
69 /* Prepare buffer to send. */
70 memcpy(buf
, &header
, sizeof(header
));
72 memcpy(buf
+ sizeof(header
), data
, size
);
75 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
81 DBG3("Relayd sending command %d of size %" PRIu64
, cmd
, buf_size
);
90 * Receive reply data on socket. This MUST be call after send_command or else
91 * could result in unexpected behavior(s).
93 static int recv_reply(struct lttcomm_relayd_sock
*rsock
, void *data
, size_t size
)
97 if (rsock
->sock
.fd
< 0) {
101 DBG3("Relayd waiting for reply of size %zu", size
);
103 ret
= rsock
->sock
.ops
->recvmsg(&rsock
->sock
, data
, size
, 0);
104 if (ret
<= 0 || ret
!= size
) {
106 /* Orderly shutdown. */
107 DBG("Socket %d has performed an orderly shutdown", rsock
->sock
.fd
);
109 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
110 rsock
->sock
.fd
, size
, ret
);
112 /* Always return -1 here and the caller can use errno. */
122 * Starting at 2.4, RELAYD_CREATE_SESSION takes additional parameters to
123 * support the live reading capability.
125 static int relayd_create_session_2_4(struct lttcomm_relayd_sock
*rsock
,
126 uint64_t *session_id
, char *session_name
, char *hostname
,
127 int session_live_timer
, unsigned int snapshot
)
130 struct lttcomm_relayd_create_session_2_4 msg
;
132 strncpy(msg
.session_name
, session_name
, sizeof(msg
.session_name
));
133 strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
));
134 msg
.live_timer
= htobe32(session_live_timer
);
135 msg
.snapshot
= htobe32(snapshot
);
138 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, &msg
, sizeof(msg
), 0);
148 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
150 static int relayd_create_session_2_1(struct lttcomm_relayd_sock
*rsock
,
151 uint64_t *session_id
)
156 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
166 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
167 * set session_id of the relayd if we have a successful reply from the relayd.
169 * On success, return 0 else a negative value which is either an errno error or
170 * a lttng error code from the relayd.
172 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
, uint64_t *session_id
,
173 char *session_name
, char *hostname
, int session_live_timer
,
174 unsigned int snapshot
)
177 struct lttcomm_relayd_status_session reply
;
182 DBG("Relayd create session");
184 switch(rsock
->minor
) {
188 ret
= relayd_create_session_2_1(rsock
, session_id
);
192 ret
= relayd_create_session_2_4(rsock
, session_id
, session_name
,
193 hostname
, session_live_timer
, snapshot
);
201 /* Receive response */
202 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
207 reply
.session_id
= be64toh(reply
.session_id
);
208 reply
.ret_code
= be32toh(reply
.ret_code
);
210 /* Return session id or negative ret code. */
211 if (reply
.ret_code
!= LTTNG_OK
) {
213 ERR("Relayd create session replied error %d", reply
.ret_code
);
217 *session_id
= reply
.session_id
;
220 DBG("Relayd session created with id %" PRIu64
, reply
.session_id
);
227 * Add stream on the relayd and assign stream handle to the stream_id argument.
229 * On success return 0 else return ret_code negative value.
231 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
232 const char *pathname
, uint64_t *stream_id
,
233 uint64_t tracefile_size
, uint64_t tracefile_count
)
236 struct lttcomm_relayd_add_stream msg
;
237 struct lttcomm_relayd_add_stream_2_2 msg_2_2
;
238 struct lttcomm_relayd_status_stream reply
;
240 /* Code flow error. Safety net. */
242 assert(channel_name
);
245 DBG("Relayd adding stream for channel name %s", channel_name
);
247 /* Compat with relayd 2.1 */
248 if (rsock
->minor
== 1) {
249 memset(&msg
, 0, sizeof(msg
));
250 strncpy(msg
.channel_name
, channel_name
, sizeof(msg
.channel_name
));
251 strncpy(msg
.pathname
, pathname
, sizeof(msg
.pathname
));
254 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
259 memset(&msg_2_2
, 0, sizeof(msg_2_2
));
260 /* Compat with relayd 2.2+ */
261 strncpy(msg_2_2
.channel_name
, channel_name
, sizeof(msg_2_2
.channel_name
));
262 strncpy(msg_2_2
.pathname
, pathname
, sizeof(msg_2_2
.pathname
));
263 msg_2_2
.tracefile_size
= htobe64(tracefile_size
);
264 msg_2_2
.tracefile_count
= htobe64(tracefile_count
);
267 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg_2_2
, sizeof(msg_2_2
), 0);
273 /* Waiting for reply */
274 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
279 /* Back to host bytes order. */
280 reply
.handle
= be64toh(reply
.handle
);
281 reply
.ret_code
= be32toh(reply
.ret_code
);
283 /* Return session id or negative ret code. */
284 if (reply
.ret_code
!= LTTNG_OK
) {
286 ERR("Relayd add stream replied error %d", reply
.ret_code
);
290 *stream_id
= reply
.handle
;
293 DBG("Relayd stream added successfully with handle %" PRIu64
,
301 * Inform the relay that all the streams for the current channel has been sent.
303 * On success return 0 else return ret_code negative value.
305 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
308 struct lttcomm_relayd_generic_reply reply
;
310 /* Code flow error. Safety net. */
313 DBG("Relayd sending streams sent.");
315 /* This feature was introduced in 2.4, ignore it for earlier versions. */
316 if (rsock
->minor
< 4) {
322 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, NULL
, 0, 0);
327 /* Waiting for reply */
328 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
333 /* Back to host bytes order. */
334 reply
.ret_code
= be32toh(reply
.ret_code
);
336 /* Return session id or negative ret code. */
337 if (reply
.ret_code
!= LTTNG_OK
) {
339 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
346 DBG("Relayd streams sent success");
354 * Check version numbers on the relayd.
355 * If major versions are compatible, we assign minor_to_use to the
356 * minor version of the procotol we are going to use for this session.
358 * Return 0 if compatible else negative value.
360 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
363 struct lttcomm_relayd_version msg
;
365 /* Code flow error. Safety net. */
368 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
371 memset(&msg
, 0, sizeof(msg
));
372 /* Prepare network byte order before transmission. */
373 msg
.major
= htobe32(rsock
->major
);
374 msg
.minor
= htobe32(rsock
->minor
);
377 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
382 /* Receive response */
383 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
388 /* Set back to host bytes order */
389 msg
.major
= be32toh(msg
.major
);
390 msg
.minor
= be32toh(msg
.minor
);
393 * Only validate the major version. If the other side is higher,
394 * communication is not possible. Only major version equal can talk to each
395 * other. If the minor version differs, the lowest version is used by both
398 if (msg
.major
!= rsock
->major
) {
401 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
402 msg
.major
, rsock
->major
);
407 * If the relayd's minor version is higher, it will adapt to our version so
408 * we can continue to use the latest relayd communication data structure.
409 * If the received minor version is higher, the relayd should adapt to us.
411 if (rsock
->minor
> msg
.minor
) {
412 rsock
->minor
= msg
.minor
;
415 /* Version number compatible */
416 DBG2("Relayd version is compatible, using protocol version %u.%u",
417 rsock
->major
, rsock
->minor
);
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_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
433 /* Code flow error. Safety net. */
436 DBG("Relayd sending metadata of size %zu", len
);
439 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
444 DBG2("Relayd metadata added successfully");
447 * After that call, the metadata data MUST be sent to the relayd so the
448 * receive size on the other end matches the len of the metadata packet
449 * header. This is why we don't wait for a reply here.
457 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
459 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
461 /* Code flow error. Safety net. */
464 if (!rsock
->sock
.ops
) {
466 * Attempting a connect on a non-initialized socket.
471 DBG3("Relayd connect ...");
473 return rsock
->sock
.ops
->connect(&rsock
->sock
);
477 * Close relayd socket with an allocated lttcomm_relayd_sock.
479 * If no socket operations are found, simply return 0 meaning that everything
480 * is fine. Without operations, the socket can not possibly be opened or used.
481 * This is possible if the socket was allocated but not created. However, the
482 * caller could simply use it to store a valid file descriptor for instance
483 * passed over a Unix socket and call this to cleanup but still without a valid
486 * Return the close returned value. On error, a negative value is usually
487 * returned back from close(2).
489 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
493 /* Code flow error. Safety net. */
496 /* An invalid fd is fine, return success. */
497 if (rsock
->sock
.fd
< 0) {
502 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
504 if (rsock
->sock
.ops
) {
505 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
507 /* Default call if no specific ops found. */
508 ret
= close(rsock
->sock
.fd
);
510 PERROR("relayd_close default close");
520 * Send data header structure to the relayd.
522 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
523 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
527 /* Code flow error. Safety net. */
531 if (rsock
->sock
.fd
< 0) {
535 DBG3("Relayd sending data header of size %zu", size
);
537 /* Again, safety net */
539 size
= sizeof(struct lttcomm_relayd_data_hdr
);
542 /* Only send data header. */
543 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
550 * The data MUST be sent right after that command for the receive on the
551 * other end to match the size in the header.
559 * Send close stream command to the relayd.
561 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
562 uint64_t last_net_seq_num
)
565 struct lttcomm_relayd_close_stream msg
;
566 struct lttcomm_relayd_generic_reply reply
;
568 /* Code flow error. Safety net. */
571 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
573 memset(&msg
, 0, sizeof(msg
));
574 msg
.stream_id
= htobe64(stream_id
);
575 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
578 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
583 /* Receive response */
584 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
589 reply
.ret_code
= be32toh(reply
.ret_code
);
591 /* Return session id or negative ret code. */
592 if (reply
.ret_code
!= LTTNG_OK
) {
594 ERR("Relayd close stream replied error %d", reply
.ret_code
);
600 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
607 * Check for data availability for a given stream id.
609 * Return 0 if NOT pending, 1 if so and a negative value on error.
611 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
612 uint64_t last_net_seq_num
)
615 struct lttcomm_relayd_data_pending msg
;
616 struct lttcomm_relayd_generic_reply reply
;
618 /* Code flow error. Safety net. */
621 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
623 memset(&msg
, 0, sizeof(msg
));
624 msg
.stream_id
= htobe64(stream_id
);
625 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
628 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
634 /* Receive response */
635 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
640 reply
.ret_code
= be32toh(reply
.ret_code
);
642 /* Return session id or negative ret code. */
643 if (reply
.ret_code
>= LTTNG_OK
) {
644 ERR("Relayd data pending replied error %d", reply
.ret_code
);
647 /* At this point, the ret code is either 1 or 0 */
648 ret
= reply
.ret_code
;
650 DBG("Relayd data is %s pending for stream id %" PRIu64
,
651 ret
== 1 ? "" : "NOT", stream_id
);
658 * Check on the relayd side for a quiescent state on the control socket.
660 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
661 uint64_t metadata_stream_id
)
664 struct lttcomm_relayd_quiescent_control msg
;
665 struct lttcomm_relayd_generic_reply reply
;
667 /* Code flow error. Safety net. */
670 DBG("Relayd checking quiescent control state");
672 memset(&msg
, 0, sizeof(msg
));
673 msg
.stream_id
= htobe64(metadata_stream_id
);
676 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
681 /* Receive response */
682 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
687 reply
.ret_code
= be32toh(reply
.ret_code
);
689 /* Return session id or negative ret code. */
690 if (reply
.ret_code
!= LTTNG_OK
) {
692 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
696 /* Control socket is quiescent */
704 * Begin a data pending command for a specific session id.
706 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
709 struct lttcomm_relayd_begin_data_pending msg
;
710 struct lttcomm_relayd_generic_reply reply
;
712 /* Code flow error. Safety net. */
715 DBG("Relayd begin data pending");
717 memset(&msg
, 0, sizeof(msg
));
718 msg
.session_id
= htobe64(id
);
721 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
726 /* Receive response */
727 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
732 reply
.ret_code
= be32toh(reply
.ret_code
);
734 /* Return session id or negative ret code. */
735 if (reply
.ret_code
!= LTTNG_OK
) {
737 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
748 * End a data pending command for a specific session id.
750 * Return 0 on success and set is_data_inflight to 0 if no data is being
751 * streamed or 1 if it is the case.
753 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
754 unsigned int *is_data_inflight
)
757 struct lttcomm_relayd_end_data_pending msg
;
758 struct lttcomm_relayd_generic_reply reply
;
760 /* Code flow error. Safety net. */
763 DBG("Relayd end data pending");
765 memset(&msg
, 0, sizeof(msg
));
766 msg
.session_id
= htobe64(id
);
769 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
774 /* Receive response */
775 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
780 recv_ret
= be32toh(reply
.ret_code
);
786 *is_data_inflight
= recv_ret
;
788 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
797 * Send index to the relayd.
799 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
800 struct ctf_packet_index
*index
, uint64_t relay_stream_id
,
801 uint64_t net_seq_num
)
804 struct lttcomm_relayd_index msg
;
805 struct lttcomm_relayd_generic_reply reply
;
807 /* Code flow error. Safety net. */
810 if (rsock
->minor
< 4) {
811 DBG("Not sending indexes before protocol 2.4");
816 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
818 memset(&msg
, 0, sizeof(msg
));
819 msg
.relay_stream_id
= htobe64(relay_stream_id
);
820 msg
.net_seq_num
= htobe64(net_seq_num
);
822 /* The index is already in big endian. */
823 msg
.packet_size
= index
->packet_size
;
824 msg
.content_size
= index
->content_size
;
825 msg
.timestamp_begin
= index
->timestamp_begin
;
826 msg
.timestamp_end
= index
->timestamp_end
;
827 msg
.events_discarded
= index
->events_discarded
;
828 msg
.stream_id
= index
->stream_id
;
831 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
, sizeof(msg
), 0);
836 /* Receive response */
837 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
842 reply
.ret_code
= be32toh(reply
.ret_code
);
844 /* Return session id or negative ret code. */
845 if (reply
.ret_code
!= LTTNG_OK
) {
847 ERR("Relayd send index replied error %d", reply
.ret_code
);