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/sessiond-comm/relayd.h>
29 #include <common/index/ctf-index.h>
34 * Send command. Fill up the header and append the data.
36 static int send_command(struct lttcomm_relayd_sock
*rsock
,
37 enum lttcomm_relayd_command cmd
, void *data
, size_t size
,
41 struct lttcomm_relayd_hdr header
;
43 uint64_t buf_size
= sizeof(header
);
45 if (rsock
->sock
.fd
< 0) {
53 buf
= zmalloc(buf_size
);
55 PERROR("zmalloc relayd send command buf");
60 memset(&header
, 0, sizeof(header
));
61 header
.cmd
= htobe32(cmd
);
62 header
.data_size
= htobe64(size
);
64 /* Zeroed for now since not used. */
65 header
.cmd_version
= 0;
66 header
.circuit_id
= 0;
68 /* Prepare buffer to send. */
69 memcpy(buf
, &header
, sizeof(header
));
71 memcpy(buf
+ sizeof(header
), data
, size
);
74 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
80 DBG3("Relayd sending command %d of size %" PRIu64
, cmd
, buf_size
);
89 * Receive reply data on socket. This MUST be call after send_command or else
90 * could result in unexpected behavior(s).
92 static int recv_reply(struct lttcomm_relayd_sock
*rsock
, void *data
, size_t size
)
96 if (rsock
->sock
.fd
< 0) {
100 DBG3("Relayd waiting for reply of size %zu", size
);
102 ret
= rsock
->sock
.ops
->recvmsg(&rsock
->sock
, data
, size
, 0);
103 if (ret
<= 0 || ret
!= size
) {
105 /* Orderly shutdown. */
106 DBG("Socket %d has performed an orderly shutdown", rsock
->sock
.fd
);
108 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
109 rsock
->sock
.fd
, size
, ret
);
111 /* Always return -1 here and the caller can use errno. */
121 * Starting at 2.4, RELAYD_CREATE_SESSION takes additional parameters to
122 * support the live reading capability.
124 static int relayd_create_session_2_4(struct lttcomm_relayd_sock
*rsock
,
125 uint64_t *session_id
, char *session_name
, char *hostname
,
126 int session_live_timer
, unsigned int snapshot
)
129 struct lttcomm_relayd_create_session_2_4 msg
;
131 strncpy(msg
.session_name
, session_name
, sizeof(msg
.session_name
));
132 strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
));
133 msg
.live_timer
= htobe32(session_live_timer
);
134 msg
.snapshot
= htobe32(snapshot
);
137 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, &msg
, sizeof(msg
), 0);
147 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
149 static int relayd_create_session_2_1(struct lttcomm_relayd_sock
*rsock
,
150 uint64_t *session_id
)
155 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
165 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
166 * set session_id of the relayd if we have a successful reply from the relayd.
168 * On success, return 0 else a negative value which is either an errno error or
169 * a lttng error code from the relayd.
171 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
, uint64_t *session_id
,
172 char *session_name
, char *hostname
, int session_live_timer
,
173 unsigned int snapshot
)
176 struct lttcomm_relayd_status_session reply
;
181 DBG("Relayd create session");
183 switch(rsock
->minor
) {
187 ret
= relayd_create_session_2_1(rsock
, session_id
);
191 ret
= relayd_create_session_2_4(rsock
, session_id
, session_name
,
192 hostname
, session_live_timer
, snapshot
);
200 /* Receive response */
201 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
206 reply
.session_id
= be64toh(reply
.session_id
);
207 reply
.ret_code
= be32toh(reply
.ret_code
);
209 /* Return session id or negative ret code. */
210 if (reply
.ret_code
!= LTTNG_OK
) {
212 ERR("Relayd create session replied error %d", reply
.ret_code
);
216 *session_id
= reply
.session_id
;
219 DBG("Relayd session created with id %" PRIu64
, reply
.session_id
);
226 * Add stream on the relayd and assign stream handle to the stream_id argument.
228 * On success return 0 else return ret_code negative value.
230 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
231 const char *pathname
, uint64_t *stream_id
,
232 uint64_t tracefile_size
, uint64_t tracefile_count
)
235 struct lttcomm_relayd_add_stream msg
;
236 struct lttcomm_relayd_add_stream_2_2 msg_2_2
;
237 struct lttcomm_relayd_status_stream reply
;
239 /* Code flow error. Safety net. */
241 assert(channel_name
);
244 DBG("Relayd adding stream for channel name %s", channel_name
);
246 /* Compat with relayd 2.1 */
247 if (rsock
->minor
== 1) {
248 memset(&msg
, 0, sizeof(msg
));
249 strncpy(msg
.channel_name
, channel_name
, sizeof(msg
.channel_name
));
250 strncpy(msg
.pathname
, pathname
, sizeof(msg
.pathname
));
253 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
258 memset(&msg_2_2
, 0, sizeof(msg_2_2
));
259 /* Compat with relayd 2.2+ */
260 strncpy(msg_2_2
.channel_name
, channel_name
, sizeof(msg_2_2
.channel_name
));
261 strncpy(msg_2_2
.pathname
, pathname
, sizeof(msg_2_2
.pathname
));
262 msg_2_2
.tracefile_size
= htobe64(tracefile_size
);
263 msg_2_2
.tracefile_count
= htobe64(tracefile_count
);
266 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg_2_2
, sizeof(msg_2_2
), 0);
272 /* Waiting for reply */
273 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
278 /* Back to host bytes order. */
279 reply
.handle
= be64toh(reply
.handle
);
280 reply
.ret_code
= be32toh(reply
.ret_code
);
282 /* Return session id or negative ret code. */
283 if (reply
.ret_code
!= LTTNG_OK
) {
285 ERR("Relayd add stream replied error %d", reply
.ret_code
);
289 *stream_id
= reply
.handle
;
292 DBG("Relayd stream added successfully with handle %" PRIu64
,
300 * Inform the relay that all the streams for the current channel has been sent.
302 * On success return 0 else return ret_code negative value.
304 int relayd_streams_sent(struct lttcomm_relayd_sock
*rsock
)
307 struct lttcomm_relayd_generic_reply reply
;
309 /* Code flow error. Safety net. */
312 DBG("Relayd sending streams sent.");
314 /* This feature was introduced in 2.4, ignore it for earlier versions. */
315 if (rsock
->minor
< 4) {
321 ret
= send_command(rsock
, RELAYD_STREAMS_SENT
, NULL
, 0, 0);
326 /* Waiting for reply */
327 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
332 /* Back to host bytes order. */
333 reply
.ret_code
= be32toh(reply
.ret_code
);
335 /* Return session id or negative ret code. */
336 if (reply
.ret_code
!= LTTNG_OK
) {
338 ERR("Relayd streams sent replied error %d", reply
.ret_code
);
345 DBG("Relayd streams sent success");
353 * Check version numbers on the relayd.
354 * If major versions are compatible, we assign minor_to_use to the
355 * minor version of the procotol we are going to use for this session.
357 * Return 0 if compatible else negative value.
359 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
362 struct lttcomm_relayd_version msg
;
364 /* Code flow error. Safety net. */
367 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
370 memset(&msg
, 0, sizeof(msg
));
371 /* Prepare network byte order before transmission. */
372 msg
.major
= htobe32(rsock
->major
);
373 msg
.minor
= htobe32(rsock
->minor
);
376 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
381 /* Receive response */
382 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
387 /* Set back to host bytes order */
388 msg
.major
= be32toh(msg
.major
);
389 msg
.minor
= be32toh(msg
.minor
);
392 * Only validate the major version. If the other side is higher,
393 * communication is not possible. Only major version equal can talk to each
394 * other. If the minor version differs, the lowest version is used by both
397 if (msg
.major
!= rsock
->major
) {
400 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
401 msg
.major
, rsock
->major
);
406 * If the relayd's minor version is higher, it will adapt to our version so
407 * we can continue to use the latest relayd communication data structure.
408 * If the received minor version is higher, the relayd should adapt to us.
410 if (rsock
->minor
> msg
.minor
) {
411 rsock
->minor
= msg
.minor
;
414 /* Version number compatible */
415 DBG2("Relayd version is compatible, using protocol version %u.%u",
416 rsock
->major
, rsock
->minor
);
424 * Add stream on the relayd and assign stream handle to the stream_id argument.
426 * On success return 0 else return ret_code negative value.
428 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
432 /* Code flow error. Safety net. */
435 DBG("Relayd sending metadata of size %zu", len
);
438 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
443 DBG2("Relayd metadata added successfully");
446 * After that call, the metadata data MUST be sent to the relayd so the
447 * receive size on the other end matches the len of the metadata packet
448 * header. This is why we don't wait for a reply here.
456 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
458 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
460 /* Code flow error. Safety net. */
463 if (!rsock
->sock
.ops
) {
465 * Attempting a connect on a non-initialized socket.
470 DBG3("Relayd connect ...");
472 return rsock
->sock
.ops
->connect(&rsock
->sock
);
476 * Close relayd socket with an allocated lttcomm_relayd_sock.
478 * If no socket operations are found, simply return 0 meaning that everything
479 * is fine. Without operations, the socket can not possibly be opened or used.
480 * This is possible if the socket was allocated but not created. However, the
481 * caller could simply use it to store a valid file descriptor for instance
482 * passed over a Unix socket and call this to cleanup but still without a valid
485 * Return the close returned value. On error, a negative value is usually
486 * returned back from close(2).
488 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
492 /* Code flow error. Safety net. */
495 /* An invalid fd is fine, return success. */
496 if (rsock
->sock
.fd
< 0) {
501 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
503 if (rsock
->sock
.ops
) {
504 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
506 /* Default call if no specific ops found. */
507 ret
= close(rsock
->sock
.fd
);
509 PERROR("relayd_close default close");
519 * Send data header structure to the relayd.
521 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
522 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
526 /* Code flow error. Safety net. */
530 if (rsock
->sock
.fd
< 0) {
534 DBG3("Relayd sending data header of size %zu", size
);
536 /* Again, safety net */
538 size
= sizeof(struct lttcomm_relayd_data_hdr
);
541 /* Only send data header. */
542 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
549 * The data MUST be sent right after that command for the receive on the
550 * other end to match the size in the header.
558 * Send close stream command to the relayd.
560 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
561 uint64_t last_net_seq_num
)
564 struct lttcomm_relayd_close_stream msg
;
565 struct lttcomm_relayd_generic_reply reply
;
567 /* Code flow error. Safety net. */
570 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
572 memset(&msg
, 0, sizeof(msg
));
573 msg
.stream_id
= htobe64(stream_id
);
574 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
577 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
582 /* Receive response */
583 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
588 reply
.ret_code
= be32toh(reply
.ret_code
);
590 /* Return session id or negative ret code. */
591 if (reply
.ret_code
!= LTTNG_OK
) {
593 ERR("Relayd close stream replied error %d", reply
.ret_code
);
599 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
606 * Check for data availability for a given stream id.
608 * Return 0 if NOT pending, 1 if so and a negative value on error.
610 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
611 uint64_t last_net_seq_num
)
614 struct lttcomm_relayd_data_pending msg
;
615 struct lttcomm_relayd_generic_reply reply
;
617 /* Code flow error. Safety net. */
620 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
622 memset(&msg
, 0, sizeof(msg
));
623 msg
.stream_id
= htobe64(stream_id
);
624 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
627 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
633 /* Receive response */
634 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
639 reply
.ret_code
= be32toh(reply
.ret_code
);
641 /* Return session id or negative ret code. */
642 if (reply
.ret_code
>= LTTNG_OK
) {
643 ERR("Relayd data pending replied error %d", reply
.ret_code
);
646 /* At this point, the ret code is either 1 or 0 */
647 ret
= reply
.ret_code
;
649 DBG("Relayd data is %s pending for stream id %" PRIu64
,
650 ret
== 1 ? "" : "NOT", stream_id
);
657 * Check on the relayd side for a quiescent state on the control socket.
659 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
660 uint64_t metadata_stream_id
)
663 struct lttcomm_relayd_quiescent_control msg
;
664 struct lttcomm_relayd_generic_reply reply
;
666 /* Code flow error. Safety net. */
669 DBG("Relayd checking quiescent control state");
671 memset(&msg
, 0, sizeof(msg
));
672 msg
.stream_id
= htobe64(metadata_stream_id
);
675 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
680 /* Receive response */
681 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
686 reply
.ret_code
= be32toh(reply
.ret_code
);
688 /* Return session id or negative ret code. */
689 if (reply
.ret_code
!= LTTNG_OK
) {
691 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
695 /* Control socket is quiescent */
703 * Begin a data pending command for a specific session id.
705 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
708 struct lttcomm_relayd_begin_data_pending msg
;
709 struct lttcomm_relayd_generic_reply reply
;
711 /* Code flow error. Safety net. */
714 DBG("Relayd begin data pending");
716 memset(&msg
, 0, sizeof(msg
));
717 msg
.session_id
= htobe64(id
);
720 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
725 /* Receive response */
726 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
731 reply
.ret_code
= be32toh(reply
.ret_code
);
733 /* Return session id or negative ret code. */
734 if (reply
.ret_code
!= LTTNG_OK
) {
736 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
747 * End a data pending command for a specific session id.
749 * Return 0 on success and set is_data_inflight to 0 if no data is being
750 * streamed or 1 if it is the case.
752 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
753 unsigned int *is_data_inflight
)
756 struct lttcomm_relayd_end_data_pending msg
;
757 struct lttcomm_relayd_generic_reply reply
;
759 /* Code flow error. Safety net. */
762 DBG("Relayd end data pending");
764 memset(&msg
, 0, sizeof(msg
));
765 msg
.session_id
= htobe64(id
);
768 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
773 /* Receive response */
774 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
779 recv_ret
= be32toh(reply
.ret_code
);
785 *is_data_inflight
= recv_ret
;
787 DBG("Relayd end data pending is data inflight: %d", recv_ret
);
796 * Send index to the relayd.
798 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
799 struct ctf_packet_index
*index
, uint64_t relay_stream_id
,
800 uint64_t net_seq_num
)
803 struct lttcomm_relayd_index msg
;
804 struct lttcomm_relayd_generic_reply reply
;
806 /* Code flow error. Safety net. */
809 if (rsock
->minor
< 4) {
810 DBG("Not sending indexes before protocol 2.4");
815 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
817 memset(&msg
, 0, sizeof(msg
));
818 msg
.relay_stream_id
= htobe64(relay_stream_id
);
819 msg
.net_seq_num
= htobe64(net_seq_num
);
821 /* The index is already in big endian. */
822 msg
.packet_size
= index
->packet_size
;
823 msg
.content_size
= index
->content_size
;
824 msg
.timestamp_begin
= index
->timestamp_begin
;
825 msg
.timestamp_end
= index
->timestamp_end
;
826 msg
.events_discarded
= index
->events_discarded
;
827 msg
.stream_id
= index
->stream_id
;
830 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
, sizeof(msg
), 0);
835 /* Receive response */
836 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
841 reply
.ret_code
= be32toh(reply
.ret_code
);
843 /* Return session id or negative ret code. */
844 if (reply
.ret_code
!= LTTNG_OK
) {
846 ERR("Relayd send index replied error %d", reply
.ret_code
);