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>
33 * Send command. Fill up the header and append the data.
35 static int send_command(struct lttcomm_relayd_sock
*rsock
,
36 enum lttcomm_relayd_command cmd
, void *data
, size_t size
,
40 struct lttcomm_relayd_hdr header
;
42 uint64_t buf_size
= sizeof(header
);
44 if (rsock
->sock
.fd
< 0) {
52 buf
= zmalloc(buf_size
);
54 PERROR("zmalloc relayd send command buf");
59 header
.cmd
= htobe32(cmd
);
60 header
.data_size
= htobe64(size
);
62 /* Zeroed for now since not used. */
63 header
.cmd_version
= 0;
64 header
.circuit_id
= 0;
66 /* Prepare buffer to send. */
67 memcpy(buf
, &header
, sizeof(header
));
69 memcpy(buf
+ sizeof(header
), data
, size
);
72 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
78 DBG3("Relayd sending command %d of size %" PRIu64
, cmd
, buf_size
);
87 * Receive reply data on socket. This MUST be call after send_command or else
88 * could result in unexpected behavior(s).
90 static int recv_reply(struct lttcomm_relayd_sock
*rsock
, void *data
, size_t size
)
94 if (rsock
->sock
.fd
< 0) {
98 DBG3("Relayd waiting for reply of size %zu", size
);
100 ret
= rsock
->sock
.ops
->recvmsg(&rsock
->sock
, data
, size
, 0);
101 if (ret
<= 0 || ret
!= size
) {
103 /* Orderly shutdown. */
104 DBG("Socket %d has performed an orderly shutdown", rsock
->sock
.fd
);
106 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
107 rsock
->sock
.fd
, size
, ret
);
109 /* Always return -1 here and the caller can use errno. */
119 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
120 * set session_id of the relayd if we have a successful reply from the relayd.
122 * On success, return 0 else a negative value which is either an errno error or
123 * a lttng error code from the relayd.
125 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
, uint64_t *session_id
)
128 struct lttcomm_relayd_status_session reply
;
133 DBG("Relayd create session");
136 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
141 /* Receive response */
142 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
147 reply
.session_id
= be64toh(reply
.session_id
);
148 reply
.ret_code
= be32toh(reply
.ret_code
);
150 /* Return session id or negative ret code. */
151 if (reply
.ret_code
!= LTTNG_OK
) {
153 ERR("Relayd create session replied error %d", reply
.ret_code
);
157 *session_id
= reply
.session_id
;
160 DBG("Relayd session created with id %" PRIu64
, reply
.session_id
);
167 * Add stream on the relayd and assign stream handle to the stream_id argument.
169 * On success return 0 else return ret_code negative value.
171 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
172 const char *pathname
, uint64_t *stream_id
,
173 uint64_t tracefile_size
, uint64_t tracefile_count
)
176 struct lttcomm_relayd_add_stream msg
;
177 struct lttcomm_relayd_add_stream_2_2 msg_2_2
;
178 struct lttcomm_relayd_status_stream reply
;
180 /* Code flow error. Safety net. */
182 assert(channel_name
);
185 DBG("Relayd adding stream for channel name %s", channel_name
);
187 /* Compat with relayd 2.1 */
188 if (rsock
->minor
== 1) {
189 strncpy(msg
.channel_name
, channel_name
, sizeof(msg
.channel_name
));
190 strncpy(msg
.pathname
, pathname
, sizeof(msg
.pathname
));
193 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
198 /* Compat with relayd 2.2+ */
199 strncpy(msg_2_2
.channel_name
, channel_name
, sizeof(msg_2_2
.channel_name
));
200 strncpy(msg_2_2
.pathname
, pathname
, sizeof(msg_2_2
.pathname
));
201 msg_2_2
.tracefile_size
= htobe64(tracefile_size
);
202 msg_2_2
.tracefile_count
= htobe64(tracefile_count
);
205 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg_2_2
, sizeof(msg_2_2
), 0);
211 /* Waiting for reply */
212 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
217 /* Back to host bytes order. */
218 reply
.handle
= be64toh(reply
.handle
);
219 reply
.ret_code
= be32toh(reply
.ret_code
);
221 /* Return session id or negative ret code. */
222 if (reply
.ret_code
!= LTTNG_OK
) {
224 ERR("Relayd add stream replied error %d", reply
.ret_code
);
228 *stream_id
= reply
.handle
;
231 DBG("Relayd stream added successfully with handle %" PRIu64
,
239 * Check version numbers on the relayd.
240 * If major versions are compatible, we assign minor_to_use to the
241 * minor version of the procotol we are going to use for this session.
243 * Return 0 if compatible else negative value.
245 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
248 struct lttcomm_relayd_version msg
;
250 /* Code flow error. Safety net. */
253 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
256 /* Prepare network byte order before transmission. */
257 msg
.major
= htobe32(rsock
->major
);
258 msg
.minor
= htobe32(rsock
->minor
);
261 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
266 /* Receive response */
267 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
272 /* Set back to host bytes order */
273 msg
.major
= be32toh(msg
.major
);
274 msg
.minor
= be32toh(msg
.minor
);
277 * Only validate the major version. If the other side is higher,
278 * communication is not possible. Only major version equal can talk to each
279 * other. If the minor version differs, the lowest version is used by both
282 if (msg
.major
!= rsock
->major
) {
285 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
286 msg
.major
, rsock
->major
);
291 * If the relayd's minor version is higher, it will adapt to our version so
292 * we can continue to use the latest relayd communication data structure.
293 * If the received minor version is higher, the relayd should adapt to us.
295 if (rsock
->minor
> msg
.minor
) {
296 rsock
->minor
= msg
.minor
;
299 /* Version number compatible */
300 DBG2("Relayd version is compatible, using protocol version %u.%u",
301 rsock
->major
, rsock
->minor
);
309 * Add stream on the relayd and assign stream handle to the stream_id argument.
311 * On success return 0 else return ret_code negative value.
313 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
317 /* Code flow error. Safety net. */
320 DBG("Relayd sending metadata of size %zu", len
);
323 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
328 DBG2("Relayd metadata added successfully");
331 * After that call, the metadata data MUST be sent to the relayd so the
332 * receive size on the other end matches the len of the metadata packet
333 * header. This is why we don't wait for a reply here.
341 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
343 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
345 /* Code flow error. Safety net. */
348 if (!rsock
->sock
.ops
) {
350 * Attempting a connect on a non-initialized socket.
355 DBG3("Relayd connect ...");
357 return rsock
->sock
.ops
->connect(&rsock
->sock
);
361 * Close relayd socket with an allocated lttcomm_relayd_sock.
363 * If no socket operations are found, simply return 0 meaning that everything
364 * is fine. Without operations, the socket can not possibly be opened or used.
365 * This is possible if the socket was allocated but not created. However, the
366 * caller could simply use it to store a valid file descriptor for instance
367 * passed over a Unix socket and call this to cleanup but still without a valid
370 * Return the close returned value. On error, a negative value is usually
371 * returned back from close(2).
373 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
377 /* Code flow error. Safety net. */
380 /* An invalid fd is fine, return success. */
381 if (rsock
->sock
.fd
< 0) {
386 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
388 if (rsock
->sock
.ops
) {
389 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
391 /* Default call if no specific ops found. */
392 ret
= close(rsock
->sock
.fd
);
394 PERROR("relayd_close default close");
404 * Send data header structure to the relayd.
406 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
407 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
411 /* Code flow error. Safety net. */
415 if (rsock
->sock
.fd
< 0) {
419 DBG3("Relayd sending data header of size %zu", size
);
421 /* Again, safety net */
423 size
= sizeof(struct lttcomm_relayd_data_hdr
);
426 /* Only send data header. */
427 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
434 * The data MUST be sent right after that command for the receive on the
435 * other end to match the size in the header.
443 * Send close stream command to the relayd.
445 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
446 uint64_t last_net_seq_num
)
449 struct lttcomm_relayd_close_stream msg
;
450 struct lttcomm_relayd_generic_reply reply
;
452 /* Code flow error. Safety net. */
455 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
457 msg
.stream_id
= htobe64(stream_id
);
458 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
461 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
466 /* Receive response */
467 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
472 reply
.ret_code
= be32toh(reply
.ret_code
);
474 /* Return session id or negative ret code. */
475 if (reply
.ret_code
!= LTTNG_OK
) {
477 ERR("Relayd close stream replied error %d", reply
.ret_code
);
483 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
490 * Check for data availability for a given stream id.
492 * Return 0 if NOT pending, 1 if so and a negative value on error.
494 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
495 uint64_t last_net_seq_num
)
498 struct lttcomm_relayd_data_pending msg
;
499 struct lttcomm_relayd_generic_reply reply
;
501 /* Code flow error. Safety net. */
504 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
506 msg
.stream_id
= htobe64(stream_id
);
507 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
510 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
516 /* Receive response */
517 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
522 reply
.ret_code
= be32toh(reply
.ret_code
);
524 /* Return session id or negative ret code. */
525 if (reply
.ret_code
>= LTTNG_OK
) {
526 ERR("Relayd data pending replied error %d", reply
.ret_code
);
529 /* At this point, the ret code is either 1 or 0 */
530 ret
= reply
.ret_code
;
532 DBG("Relayd data is %s pending for stream id %" PRIu64
,
533 ret
== 1 ? "" : "NOT", stream_id
);
540 * Check on the relayd side for a quiescent state on the control socket.
542 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
543 uint64_t metadata_stream_id
)
546 struct lttcomm_relayd_quiescent_control msg
;
547 struct lttcomm_relayd_generic_reply reply
;
549 /* Code flow error. Safety net. */
552 DBG("Relayd checking quiescent control state");
554 msg
.stream_id
= htobe64(metadata_stream_id
);
557 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
562 /* Receive response */
563 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
568 reply
.ret_code
= be32toh(reply
.ret_code
);
570 /* Return session id or negative ret code. */
571 if (reply
.ret_code
!= LTTNG_OK
) {
573 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
577 /* Control socket is quiescent */
585 * Begin a data pending command for a specific session id.
587 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
590 struct lttcomm_relayd_begin_data_pending msg
;
591 struct lttcomm_relayd_generic_reply reply
;
593 /* Code flow error. Safety net. */
596 DBG("Relayd begin data pending");
598 msg
.session_id
= htobe64(id
);
601 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
606 /* Receive response */
607 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
612 reply
.ret_code
= be32toh(reply
.ret_code
);
614 /* Return session id or negative ret code. */
615 if (reply
.ret_code
!= LTTNG_OK
) {
617 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
628 * End a data pending command for a specific session id.
630 * Return 0 on success and set is_data_inflight to 0 if no data is being
631 * streamed or 1 if it is the case.
633 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
634 unsigned int *is_data_inflight
)
637 struct lttcomm_relayd_end_data_pending msg
;
638 struct lttcomm_relayd_generic_reply reply
;
640 /* Code flow error. Safety net. */
643 DBG("Relayd end data pending");
645 msg
.session_id
= htobe64(id
);
648 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
653 /* Receive response */
654 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
659 recv_ret
= be32toh(reply
.ret_code
);
665 *is_data_inflight
= recv_ret
;
667 DBG("Relayd end data pending is data inflight: %d", recv_ret
);