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
);
48 buf
= zmalloc(buf_size
);
50 PERROR("zmalloc relayd send command buf");
55 header
.cmd
= htobe32(cmd
);
56 header
.data_size
= htobe64(size
);
58 /* Zeroed for now since not used. */
59 header
.cmd_version
= 0;
60 header
.circuit_id
= 0;
62 /* Prepare buffer to send. */
63 memcpy(buf
, &header
, sizeof(header
));
65 memcpy(buf
+ sizeof(header
), data
, size
);
68 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, buf
, buf_size
, flags
);
74 DBG3("Relayd sending command %d of size %" PRIu64
, cmd
, buf_size
);
83 * Receive reply data on socket. This MUST be call after send_command or else
84 * could result in unexpected behavior(s).
86 static int recv_reply(struct lttcomm_relayd_sock
*rsock
, void *data
, size_t size
)
90 DBG3("Relayd waiting for reply of size %zu", size
);
92 ret
= rsock
->sock
.ops
->recvmsg(&rsock
->sock
, data
, size
, 0);
93 if (ret
<= 0 || ret
!= size
) {
95 /* Orderly shutdown. */
96 DBG("Socket %d has performed an orderly shutdown", rsock
->sock
.fd
);
98 DBG("Receiving reply failed on sock %d for size %zu with ret %d",
99 rsock
->sock
.fd
, size
, ret
);
101 /* Always return -1 here and the caller can use errno. */
111 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
112 * set session_id of the relayd if we have a successful reply from the relayd.
114 * On success, return 0 else a negative value which is either an errno error or
115 * a lttng error code from the relayd.
117 int relayd_create_session(struct lttcomm_relayd_sock
*rsock
, uint64_t *session_id
)
120 struct lttcomm_relayd_status_session reply
;
125 DBG("Relayd create session");
128 ret
= send_command(rsock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
133 /* Receive response */
134 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
139 reply
.session_id
= be64toh(reply
.session_id
);
140 reply
.ret_code
= be32toh(reply
.ret_code
);
142 /* Return session id or negative ret code. */
143 if (reply
.ret_code
!= LTTNG_OK
) {
145 ERR("Relayd create session replied error %d", reply
.ret_code
);
149 *session_id
= reply
.session_id
;
152 DBG("Relayd session created with id %" PRIu64
, reply
.session_id
);
159 * Add stream on the relayd and assign stream handle to the stream_id argument.
161 * On success return 0 else return ret_code negative value.
163 int relayd_add_stream(struct lttcomm_relayd_sock
*rsock
, const char *channel_name
,
164 const char *pathname
, uint64_t *stream_id
,
165 uint64_t tracefile_size
, uint64_t tracefile_count
)
168 struct lttcomm_relayd_add_stream msg
;
169 struct lttcomm_relayd_add_stream_2_2 msg_2_2
;
170 struct lttcomm_relayd_status_stream reply
;
172 /* Code flow error. Safety net. */
174 assert(channel_name
);
177 DBG("Relayd adding stream for channel name %s", channel_name
);
179 /* Compat with relayd 2.1 */
180 if (rsock
->minor
== 1) {
181 strncpy(msg
.channel_name
, channel_name
, sizeof(msg
.channel_name
));
182 strncpy(msg
.pathname
, pathname
, sizeof(msg
.pathname
));
185 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
190 /* Compat with relayd 2.2+ */
191 strncpy(msg_2_2
.channel_name
, channel_name
, sizeof(msg_2_2
.channel_name
));
192 strncpy(msg_2_2
.pathname
, pathname
, sizeof(msg_2_2
.pathname
));
193 msg_2_2
.tracefile_size
= htobe64(tracefile_size
);
194 msg_2_2
.tracefile_count
= htobe64(tracefile_count
);
197 ret
= send_command(rsock
, RELAYD_ADD_STREAM
, (void *) &msg_2_2
, sizeof(msg_2_2
), 0);
203 /* Waiting for reply */
204 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
209 /* Back to host bytes order. */
210 reply
.handle
= be64toh(reply
.handle
);
211 reply
.ret_code
= be32toh(reply
.ret_code
);
213 /* Return session id or negative ret code. */
214 if (reply
.ret_code
!= LTTNG_OK
) {
216 ERR("Relayd add stream replied error %d", reply
.ret_code
);
220 *stream_id
= reply
.handle
;
223 DBG("Relayd stream added successfully with handle %" PRIu64
,
231 * Check version numbers on the relayd.
232 * If major versions are compatible, we assign minor_to_use to the
233 * minor version of the procotol we are going to use for this session.
235 * Return 0 if compatible else negative value.
237 int relayd_version_check(struct lttcomm_relayd_sock
*rsock
)
240 struct lttcomm_relayd_version msg
;
242 /* Code flow error. Safety net. */
245 DBG("Relayd version check for major.minor %u.%u", rsock
->major
,
248 /* Prepare network byte order before transmission. */
249 msg
.major
= htobe32(rsock
->major
);
250 msg
.minor
= htobe32(rsock
->minor
);
253 ret
= send_command(rsock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
258 /* Receive response */
259 ret
= recv_reply(rsock
, (void *) &msg
, sizeof(msg
));
264 /* Set back to host bytes order */
265 msg
.major
= be32toh(msg
.major
);
266 msg
.minor
= be32toh(msg
.minor
);
269 * Only validate the major version. If the other side is higher,
270 * communication is not possible. Only major version equal can talk to each
271 * other. If the minor version differs, the lowest version is used by both
274 if (msg
.major
!= rsock
->major
) {
277 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
278 msg
.major
, rsock
->major
);
283 * If the relayd's minor version is higher, it will adapt to our version so
284 * we can continue to use the latest relayd communication data structure.
285 * If the received minor version is higher, the relayd should adapt to us.
287 if (rsock
->minor
> msg
.minor
) {
288 rsock
->minor
= msg
.minor
;
291 /* Version number compatible */
292 DBG2("Relayd version is compatible, using protocol version %u.%u",
293 rsock
->major
, rsock
->minor
);
301 * Add stream on the relayd and assign stream handle to the stream_id argument.
303 * On success return 0 else return ret_code negative value.
305 int relayd_send_metadata(struct lttcomm_relayd_sock
*rsock
, size_t len
)
309 /* Code flow error. Safety net. */
312 DBG("Relayd sending metadata of size %zu", len
);
315 ret
= send_command(rsock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
320 DBG2("Relayd metadata added successfully");
323 * After that call, the metadata data MUST be sent to the relayd so the
324 * receive size on the other end matches the len of the metadata packet
325 * header. This is why we don't wait for a reply here.
333 * Connect to relay daemon with an allocated lttcomm_relayd_sock.
335 int relayd_connect(struct lttcomm_relayd_sock
*rsock
)
337 /* Code flow error. Safety net. */
340 DBG3("Relayd connect ...");
342 return rsock
->sock
.ops
->connect(&rsock
->sock
);
346 * Close relayd socket with an allocated lttcomm_relayd_sock.
348 * If no socket operations are found, simply return 0 meaning that everything
349 * is fine. Without operations, the socket can not possibly be opened or used.
350 * This is possible if the socket was allocated but not created. However, the
351 * caller could simply use it to store a valid file descriptor for instance
352 * passed over a Unix socket and call this to cleanup but still without a valid
355 * Return the close returned value. On error, a negative value is usually
356 * returned back from close(2).
358 int relayd_close(struct lttcomm_relayd_sock
*rsock
)
362 /* Code flow error. Safety net. */
365 /* An invalid fd is fine, return success. */
366 if (rsock
->sock
.fd
< 0) {
371 DBG3("Relayd closing socket %d", rsock
->sock
.fd
);
373 if (rsock
->sock
.ops
) {
374 ret
= rsock
->sock
.ops
->close(&rsock
->sock
);
376 /* Default call if no specific ops found. */
377 ret
= close(rsock
->sock
.fd
);
379 PERROR("relayd_close default close");
388 * Send data header structure to the relayd.
390 int relayd_send_data_hdr(struct lttcomm_relayd_sock
*rsock
,
391 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
395 /* Code flow error. Safety net. */
399 DBG3("Relayd sending data header of size %zu", size
);
401 /* Again, safety net */
403 size
= sizeof(struct lttcomm_relayd_data_hdr
);
406 /* Only send data header. */
407 ret
= rsock
->sock
.ops
->sendmsg(&rsock
->sock
, hdr
, size
, 0);
414 * The data MUST be sent right after that command for the receive on the
415 * other end to match the size in the header.
423 * Send close stream command to the relayd.
425 int relayd_send_close_stream(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
426 uint64_t last_net_seq_num
)
429 struct lttcomm_relayd_close_stream msg
;
430 struct lttcomm_relayd_generic_reply reply
;
432 /* Code flow error. Safety net. */
435 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
437 msg
.stream_id
= htobe64(stream_id
);
438 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
441 ret
= send_command(rsock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
446 /* Receive response */
447 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
452 reply
.ret_code
= be32toh(reply
.ret_code
);
454 /* Return session id or negative ret code. */
455 if (reply
.ret_code
!= LTTNG_OK
) {
457 ERR("Relayd close stream replied error %d", reply
.ret_code
);
463 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
470 * Check for data availability for a given stream id.
472 * Return 0 if NOT pending, 1 if so and a negative value on error.
474 int relayd_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t stream_id
,
475 uint64_t last_net_seq_num
)
478 struct lttcomm_relayd_data_pending msg
;
479 struct lttcomm_relayd_generic_reply reply
;
481 /* Code flow error. Safety net. */
484 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
486 msg
.stream_id
= htobe64(stream_id
);
487 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
490 ret
= send_command(rsock
, RELAYD_DATA_PENDING
, (void *) &msg
,
496 /* Receive response */
497 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
502 reply
.ret_code
= be32toh(reply
.ret_code
);
504 /* Return session id or negative ret code. */
505 if (reply
.ret_code
>= LTTNG_OK
) {
506 ERR("Relayd data pending replied error %d", reply
.ret_code
);
509 /* At this point, the ret code is either 1 or 0 */
510 ret
= reply
.ret_code
;
512 DBG("Relayd data is %s pending for stream id %" PRIu64
,
513 ret
== 1 ? "" : "NOT", stream_id
);
520 * Check on the relayd side for a quiescent state on the control socket.
522 int relayd_quiescent_control(struct lttcomm_relayd_sock
*rsock
,
523 uint64_t metadata_stream_id
)
526 struct lttcomm_relayd_quiescent_control msg
;
527 struct lttcomm_relayd_generic_reply reply
;
529 /* Code flow error. Safety net. */
532 DBG("Relayd checking quiescent control state");
534 msg
.stream_id
= htobe64(metadata_stream_id
);
537 ret
= send_command(rsock
, RELAYD_QUIESCENT_CONTROL
, &msg
, sizeof(msg
), 0);
542 /* Receive response */
543 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
548 reply
.ret_code
= be32toh(reply
.ret_code
);
550 /* Return session id or negative ret code. */
551 if (reply
.ret_code
!= LTTNG_OK
) {
553 ERR("Relayd quiescent control replied error %d", reply
.ret_code
);
557 /* Control socket is quiescent */
565 * Begin a data pending command for a specific session id.
567 int relayd_begin_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
)
570 struct lttcomm_relayd_begin_data_pending msg
;
571 struct lttcomm_relayd_generic_reply reply
;
573 /* Code flow error. Safety net. */
576 DBG("Relayd begin data pending");
578 msg
.session_id
= htobe64(id
);
581 ret
= send_command(rsock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
586 /* Receive response */
587 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
592 reply
.ret_code
= be32toh(reply
.ret_code
);
594 /* Return session id or negative ret code. */
595 if (reply
.ret_code
!= LTTNG_OK
) {
597 ERR("Relayd begin data pending replied error %d", reply
.ret_code
);
608 * End a data pending command for a specific session id.
610 * Return 0 on success and set is_data_inflight to 0 if no data is being
611 * streamed or 1 if it is the case.
613 int relayd_end_data_pending(struct lttcomm_relayd_sock
*rsock
, uint64_t id
,
614 unsigned int *is_data_inflight
)
617 struct lttcomm_relayd_end_data_pending msg
;
618 struct lttcomm_relayd_generic_reply reply
;
620 /* Code flow error. Safety net. */
623 DBG("Relayd end data pending");
625 msg
.session_id
= htobe64(id
);
628 ret
= send_command(rsock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
633 /* Receive response */
634 ret
= recv_reply(rsock
, (void *) &reply
, sizeof(reply
));
639 reply
.ret_code
= be32toh(reply
.ret_code
);
640 if (reply
.ret_code
< 0) {
641 ret
= reply
.ret_code
;
645 *is_data_inflight
= reply
.ret_code
;
647 DBG("Relayd end data pending is data inflight: %d", reply
.ret_code
);