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/lttng-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 header
.cmd
= htobe32(cmd
);
61 header
.data_size
= htobe64(size
);
63 /* Zeroed for now since not used. */
64 header
.cmd_version
= 0;
65 header
.circuit_id
= 0;
67 /* Prepare buffer to send. */
68 memcpy(buf
, &header
, sizeof(header
));
70 memcpy(buf
+ sizeof(header
), data
, size
);
73 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
79 DBG3("Relayd sending command %d of size %" PRIu64
, cmd
, buf_size
);
88 * Receive reply data on socket. This MUST be call after send_command or else
89 * could result in unexpected behavior(s).
91 static int recv_reply(struct lttcomm_relayd_sock
*rsock
, void *data
, size_t size
)
95 if (rsock
->sock
.fd
< 0) {
99 DBG3("Relayd waiting for reply of size %zu", size
);
101 ret
= rsock
->sock
.ops
->recvmsg(&rsock
->sock
, data
, size
, 0);
102 if (ret
<= 0 || ret
!= size
) {
104 /* Orderly shutdown. */
105 DBG("Socket %d has performed an orderly shutdown", rsock
->sock
.fd
);
107 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
108 rsock
->sock
.fd
, size
, ret
);
110 /* Always return -1 here and the caller can use errno. */
120 * Starting at 2.4, RELAYD_CREATE_SESSION takes additional parameters to
121 * support the live reading capability.
123 static int relayd_create_session_2_4(struct lttcomm_relayd_sock
*rsock
,
124 uint64_t *session_id
, char *session_name
, char *hostname
,
125 int session_live_timer
, unsigned int snapshot
)
128 struct lttcomm_relayd_create_session_2_4 msg
;
130 strncpy(msg
.session_name
, session_name
, sizeof(msg
.session_name
));
131 strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
));
132 msg
.live_timer
= htobe32(session_live_timer
);
133 msg
.snapshot
= htobe32(snapshot
);
136 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, &msg
, sizeof(msg
), 0);
146 * RELAYD_CREATE_SESSION from 2.1 to 2.3.
148 static int relayd_create_session_2_1(struct lttcomm_relayd_sock
*rsock
,
149 uint64_t *session_id
)
154 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
164 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
165 * set session_id of the relayd if we have a successful reply from the relayd.
167 * On success, return 0 else a negative value which is either an errno error or
168 * a lttng error code from the relayd.
170 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
, uint64_t *session_id
,
171 char *session_name
, char *hostname
, int session_live_timer
,
172 unsigned int snapshot
)
175 struct lttcomm_relayd_status_session reply
;
180 DBG("Relayd create session");
182 switch(rsock
->minor
) {
186 ret
= relayd_create_session_2_1(rsock
, session_id
);
189 ret
= relayd_create_session_2_4(rsock
, session_id
, session_name
,
190 hostname
, session_live_timer
, snapshot
);
197 /* Receive response */
198 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
203 reply
.session_id
= be64toh(reply
.session_id
);
204 reply
.ret_code
= be32toh(reply
.ret_code
);
206 /* Return session id or negative ret code. */
207 if (reply
.ret_code
!= LTTNG_OK
) {
209 ERR("Relayd create session replied error %d", reply
.ret_code
);
213 *session_id
= reply
.session_id
;
216 DBG("Relayd session created with id %" PRIu64
, reply
.session_id
);
223 * Add stream on the relayd and assign stream handle to the stream_id argument.
225 * On success return 0 else return ret_code negative value.
227 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
228 const char *pathname
, uint64_t *stream_id
,
229 uint64_t tracefile_size
, uint64_t tracefile_count
)
232 struct lttcomm_relayd_add_stream msg
;
233 struct lttcomm_relayd_add_stream_2_2 msg_2_2
;
234 struct lttcomm_relayd_status_stream reply
;
236 /* Code flow error. Safety net. */
238 assert(channel_name
);
241 DBG("Relayd adding stream for channel name %s", channel_name
);
243 /* Compat with relayd 2.1 */
244 if (rsock
->minor
== 1) {
245 strncpy(msg
.channel_name
, channel_name
, sizeof(msg
.channel_name
));
246 strncpy(msg
.pathname
, pathname
, sizeof(msg
.pathname
));
249 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
254 /* Compat with relayd 2.2+ */
255 strncpy(msg_2_2
.channel_name
, channel_name
, sizeof(msg_2_2
.channel_name
));
256 strncpy(msg_2_2
.pathname
, pathname
, sizeof(msg_2_2
.pathname
));
257 msg_2_2
.tracefile_size
= htobe64(tracefile_size
);
258 msg_2_2
.tracefile_count
= htobe64(tracefile_count
);
261 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg_2_2
, sizeof(msg_2_2
), 0);
267 /* Waiting for reply */
268 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
273 /* Back to host bytes order. */
274 reply
.handle
= be64toh(reply
.handle
);
275 reply
.ret_code
= be32toh(reply
.ret_code
);
277 /* Return session id or negative ret code. */
278 if (reply
.ret_code
!= LTTNG_OK
) {
280 ERR("Relayd add stream replied error %d", reply
.ret_code
);
284 *stream_id
= reply
.handle
;
287 DBG("Relayd stream added successfully with handle %" PRIu64
,
295 * Check version numbers on the relayd.
296 * If major versions are compatible, we assign minor_to_use to the
297 * minor version of the procotol we are going to use for this session.
299 * Return 0 if compatible else negative value.
301 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
304 struct lttcomm_relayd_version msg
;
306 /* Code flow error. Safety net. */
309 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
312 /* Prepare network byte order before transmission. */
313 msg
.major
= htobe32(rsock
->major
);
314 msg
.minor
= htobe32(rsock
->minor
);
317 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
322 /* Receive response */
323 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
328 /* Set back to host bytes order */
329 msg
.major
= be32toh(msg
.major
);
330 msg
.minor
= be32toh(msg
.minor
);
333 * Only validate the major version. If the other side is higher,
334 * communication is not possible. Only major version equal can talk to each
335 * other. If the minor version differs, the lowest version is used by both
338 if (msg
.major
!= rsock
->major
) {
341 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
342 msg
.major
, rsock
->major
);
347 * If the relayd's minor version is higher, it will adapt to our version so
348 * we can continue to use the latest relayd communication data structure.
349 * If the received minor version is higher, the relayd should adapt to us.
351 if (rsock
->minor
> msg
.minor
) {
352 rsock
->minor
= msg
.minor
;
355 /* Version number compatible */
356 DBG2("Relayd version is compatible, using protocol version %u.%u",
357 rsock
->major
, rsock
->minor
);
365 * Add stream on the relayd and assign stream handle to the stream_id argument.
367 * On success return 0 else return ret_code negative value.
369 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
373 /* Code flow error. Safety net. */
376 DBG("Relayd sending metadata of size %zu", len
);
379 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
384 DBG2("Relayd metadata added successfully");
387 * After that call, the metadata data MUST be sent to the relayd so the
388 * receive size on the other end matches the len of the metadata packet
389 * header. This is why we don't wait for a reply here.
397 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
399 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
401 /* Code flow error. Safety net. */
404 if (!rsock
->sock
.ops
) {
406 * Attempting a connect on a non-initialized socket.
411 DBG3("Relayd connect ...");
413 return rsock
->sock
.ops
->connect(&rsock
->sock
);
417 * Close relayd socket with an allocated lttcomm_relayd_sock.
419 * If no socket operations are found, simply return 0 meaning that everything
420 * is fine. Without operations, the socket can not possibly be opened or used.
421 * This is possible if the socket was allocated but not created. However, the
422 * caller could simply use it to store a valid file descriptor for instance
423 * passed over a Unix socket and call this to cleanup but still without a valid
426 * Return the close returned value. On error, a negative value is usually
427 * returned back from close(2).
429 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
433 /* Code flow error. Safety net. */
436 /* An invalid fd is fine, return success. */
437 if (rsock
->sock
.fd
< 0) {
442 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
444 if (rsock
->sock
.ops
) {
445 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
447 /* Default call if no specific ops found. */
448 ret
= close(rsock
->sock
.fd
);
450 PERROR("relayd_close default close");
460 * Send data header structure to the relayd.
462 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
463 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
467 /* Code flow error. Safety net. */
471 if (rsock
->sock
.fd
< 0) {
475 DBG3("Relayd sending data header of size %zu", size
);
477 /* Again, safety net */
479 size
= sizeof(struct lttcomm_relayd_data_hdr
);
482 /* Only send data header. */
483 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
490 * The data MUST be sent right after that command for the receive on the
491 * other end to match the size in the header.
499 * Send close stream command to the relayd.
501 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
502 uint64_t last_net_seq_num
)
505 struct lttcomm_relayd_close_stream msg
;
506 struct lttcomm_relayd_generic_reply reply
;
508 /* Code flow error. Safety net. */
511 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
513 msg
.stream_id
= htobe64(stream_id
);
514 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
517 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
522 /* Receive response */
523 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
528 reply
.ret_code
= be32toh(reply
.ret_code
);
530 /* Return session id or negative ret code. */
531 if (reply
.ret_code
!= LTTNG_OK
) {
533 ERR("Relayd close stream replied error %d", reply
.ret_code
);
539 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
546 * Check for data availability for a given stream id.
548 * Return 0 if NOT pending, 1 if so and a negative value on error.
550 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
551 uint64_t last_net_seq_num
)
554 struct lttcomm_relayd_data_pending msg
;
555 struct lttcomm_relayd_generic_reply reply
;
557 /* Code flow error. Safety net. */
560 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
562 msg
.stream_id
= htobe64(stream_id
);
563 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
566 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
572 /* Receive response */
573 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
578 reply
.ret_code
= be32toh(reply
.ret_code
);
580 /* Return session id or negative ret code. */
581 if (reply
.ret_code
>= LTTNG_OK
) {
582 ERR("Relayd data pending replied error %d", reply
.ret_code
);
585 /* At this point, the ret code is either 1 or 0 */
586 ret
= reply
.ret_code
;
588 DBG("Relayd data is %s pending for stream id %" PRIu64
,
589 ret
== 1 ? "" : "NOT", stream_id
);
596 * Check on the relayd side for a quiescent state on the control socket.
598 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
599 uint64_t metadata_stream_id
)
602 struct lttcomm_relayd_quiescent_control msg
;
603 struct lttcomm_relayd_generic_reply reply
;
605 /* Code flow error. Safety net. */
608 DBG("Relayd checking quiescent control state");
610 msg
.stream_id
= htobe64(metadata_stream_id
);
613 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
618 /* Receive response */
619 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
624 reply
.ret_code
= be32toh(reply
.ret_code
);
626 /* Return session id or negative ret code. */
627 if (reply
.ret_code
!= LTTNG_OK
) {
629 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
633 /* Control socket is quiescent */
641 * Begin a data pending command for a specific session id.
643 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
646 struct lttcomm_relayd_begin_data_pending msg
;
647 struct lttcomm_relayd_generic_reply reply
;
649 /* Code flow error. Safety net. */
652 DBG("Relayd begin data pending");
654 msg
.session_id
= htobe64(id
);
657 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
662 /* Receive response */
663 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
668 reply
.ret_code
= be32toh(reply
.ret_code
);
670 /* Return session id or negative ret code. */
671 if (reply
.ret_code
!= LTTNG_OK
) {
673 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
684 * End a data pending command for a specific session id.
686 * Return 0 on success and set is_data_inflight to 0 if no data is being
687 * streamed or 1 if it is the case.
689 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
690 unsigned int *is_data_inflight
)
693 struct lttcomm_relayd_end_data_pending msg
;
694 struct lttcomm_relayd_generic_reply reply
;
696 /* Code flow error. Safety net. */
699 DBG("Relayd end data pending");
701 msg
.session_id
= htobe64(id
);
704 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
709 /* Receive response */
710 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
715 reply
.ret_code
= be32toh(reply
.ret_code
);
716 if (reply
.ret_code
< 0) {
717 ret
= reply
.ret_code
;
721 *is_data_inflight
= reply
.ret_code
;
723 DBG("Relayd end data pending is data inflight: %d", reply
.ret_code
);
732 * Send index to the relayd.
734 int relayd_send_index(struct lttcomm_relayd_sock
*rsock
,
735 struct lttng_packet_index
*index
, uint64_t relay_stream_id
,
736 uint64_t net_seq_num
)
739 struct lttcomm_relayd_index msg
;
740 struct lttcomm_relayd_generic_reply reply
;
742 /* Code flow error. Safety net. */
745 if (rsock
->minor
< 4) {
746 DBG("Not sending indexes before protocol 2.4");
751 DBG("Relayd sending index for stream ID %" PRIu64
, relay_stream_id
);
753 msg
.relay_stream_id
= htobe64(relay_stream_id
);
754 msg
.net_seq_num
= htobe64(net_seq_num
);
756 /* The index is already in big endian. */
757 msg
.packet_size
= index
->packet_size
;
758 msg
.content_size
= index
->content_size
;
759 msg
.timestamp_begin
= index
->timestamp_begin
;
760 msg
.timestamp_end
= index
->timestamp_end
;
761 msg
.events_discarded
= index
->events_discarded
;
762 msg
.stream_id
= index
->stream_id
;
765 ret
= send_command(rsock
, RELAYD_SEND_INDEX
, &msg
, sizeof(msg
), 0);
770 /* Receive response */
771 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
776 reply
.ret_code
= be32toh(reply
.ret_code
);
778 /* Return session id or negative ret code. */
779 if (reply
.ret_code
!= LTTNG_OK
) {
781 ERR("Relayd send index replied error %d", reply
.ret_code
);