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_sock
*sock
,
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
= sock
->ops
->sendmsg(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_sock
*sock
, void *data
, size_t size
)
90 DBG3("Relayd waiting for reply of size %ld", size
);
92 ret
= sock
->ops
->recvmsg(sock
, data
, size
, 0);
103 * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
104 * set session_id of the relayd if we have a successful reply from the relayd.
106 * On success, return 0 else a negative value being a lttng_error_code returned
109 int relayd_create_session(struct lttcomm_sock
*sock
, uint64_t *session_id
)
112 struct lttcomm_relayd_status_session reply
;
117 DBG("Relayd create session");
120 ret
= send_command(sock
, RELAYD_CREATE_SESSION
, NULL
, 0, 0);
125 /* Recevie response */
126 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
131 reply
.session_id
= be64toh(reply
.session_id
);
132 reply
.ret_code
= be32toh(reply
.ret_code
);
134 /* Return session id or negative ret code. */
135 if (reply
.ret_code
!= LTTNG_OK
) {
136 ret
= -reply
.ret_code
;
137 ERR("Relayd create session replied error %d", ret
);
141 *session_id
= reply
.session_id
;
144 DBG("Relayd session created with id %" PRIu64
, reply
.session_id
);
151 * Add stream on the relayd and assign stream handle to the stream_id argument.
153 * On success return 0 else return ret_code negative value.
155 int relayd_add_stream(struct lttcomm_sock
*sock
, const char *channel_name
,
156 const char *pathname
, uint64_t *stream_id
)
159 struct lttcomm_relayd_add_stream msg
;
160 struct lttcomm_relayd_status_stream reply
;
162 /* Code flow error. Safety net. */
164 assert(channel_name
);
167 DBG("Relayd adding stream for channel name %s", channel_name
);
169 strncpy(msg
.channel_name
, channel_name
, sizeof(msg
.channel_name
));
170 strncpy(msg
.pathname
, pathname
, sizeof(msg
.pathname
));
173 ret
= send_command(sock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
178 /* Waiting for reply */
179 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
184 /* Back to host bytes order. */
185 reply
.handle
= be64toh(reply
.handle
);
186 reply
.ret_code
= be32toh(reply
.ret_code
);
188 /* Return session id or negative ret code. */
189 if (reply
.ret_code
!= LTTNG_OK
) {
190 ret
= -reply
.ret_code
;
191 ERR("Relayd add stream replied error %d", ret
);
195 *stream_id
= reply
.handle
;
198 DBG("Relayd stream added successfully with handle %" PRIu64
,
206 * Check version numbers on the relayd.
208 * Return 0 if compatible else negative value.
210 int relayd_version_check(struct lttcomm_sock
*sock
, uint32_t major
,
214 struct lttcomm_relayd_version msg
;
216 /* Code flow error. Safety net. */
219 DBG("Relayd version check for major.minor %u.%u", major
, minor
);
221 /* Prepare network byte order before transmission. */
222 msg
.major
= htobe32(major
);
223 msg
.minor
= htobe32(minor
);
226 ret
= send_command(sock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
231 /* Recevie response */
232 ret
= recv_reply(sock
, (void *) &msg
, sizeof(msg
));
237 /* Set back to host bytes order */
238 msg
.major
= be32toh(msg
.major
);
239 msg
.minor
= be32toh(msg
.minor
);
242 * Only validate the major version. If the other side is higher,
243 * communication is not possible. Only major version equal can talk to each
244 * other. If the minor version differs, the lowest version is used by both
247 * For now, before 2.1.0 stable release, we don't have to check the minor
248 * because this new mechanism with the relayd will only be available with
251 if (msg
.major
== major
) {
254 DBG2("Relayd version is compatible");
259 * After 2.1.0 release, for the 2.2 release, at this point will have to
260 * check the minor version in order for the session daemon to know which
261 * structure to use to communicate with the relayd. If the relayd's minor
262 * version is higher, it will adapt to our version so we can continue to
263 * use the latest relayd communication data structure.
266 /* Version number not compatible */
267 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
276 * Add stream on the relayd and assign stream handle to the stream_id argument.
278 * On success return 0 else return ret_code negative value.
280 int relayd_send_metadata(struct lttcomm_sock
*sock
, size_t len
)
284 /* Code flow error. Safety net. */
287 DBG("Relayd sending metadata of size %zu", len
);
290 ret
= send_command(sock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
295 DBG2("Relayd metadata added successfully");
298 * After that call, the metadata data MUST be sent to the relayd so the
299 * receive size on the other end matches the len of the metadata packet
300 * header. This is why we don't wait for a reply here.
308 * Connect to relay daemon with an allocated lttcomm_sock.
310 int relayd_connect(struct lttcomm_sock
*sock
)
312 /* Code flow error. Safety net. */
315 DBG3("Relayd connect ...");
317 return sock
->ops
->connect(sock
);
321 * Close relayd socket with an allocated lttcomm_sock.
323 int relayd_close(struct lttcomm_sock
*sock
)
325 /* Code flow error. Safety net. */
328 DBG3("Relayd closing socket %d", sock
->fd
);
330 return sock
->ops
->close(sock
);
334 * Send data header structure to the relayd.
336 int relayd_send_data_hdr(struct lttcomm_sock
*sock
,
337 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
341 /* Code flow error. Safety net. */
345 DBG3("Relayd sending data header of size %ld", size
);
347 /* Again, safety net */
349 size
= sizeof(struct lttcomm_relayd_data_hdr
);
352 /* Only send data header. */
353 ret
= sock
->ops
->sendmsg(sock
, hdr
, size
, 0);
360 * The data MUST be sent right after that command for the receive on the
361 * other end to match the size in the header.
369 * Send close stream command to the relayd.
371 int relayd_send_close_stream(struct lttcomm_sock
*sock
, uint64_t stream_id
,
372 uint64_t last_net_seq_num
)
375 struct lttcomm_relayd_close_stream msg
;
376 struct lttcomm_relayd_generic_reply reply
;
378 /* Code flow error. Safety net. */
381 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
383 msg
.stream_id
= htobe64(stream_id
);
384 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
387 ret
= send_command(sock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
392 /* Recevie response */
393 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
398 reply
.ret_code
= be32toh(reply
.ret_code
);
400 /* Return session id or negative ret code. */
401 if (reply
.ret_code
!= LTTNG_OK
) {
402 ret
= -reply
.ret_code
;
403 ERR("Relayd close stream replied error %d", ret
);
409 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
416 * Check for data availability for a given stream id.
418 * Return 0 if NOT pending, 1 if so and a negative value on error.
420 int relayd_data_pending(struct lttcomm_sock
*sock
, uint64_t stream_id
,
421 uint64_t last_net_seq_num
)
424 struct lttcomm_relayd_data_pending msg
;
425 struct lttcomm_relayd_generic_reply reply
;
427 /* Code flow error. Safety net. */
430 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
432 msg
.stream_id
= htobe64(stream_id
);
433 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
436 ret
= send_command(sock
, RELAYD_DATA_PENDING
, (void *) &msg
,
442 /* Recevie response */
443 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
448 reply
.ret_code
= be32toh(reply
.ret_code
);
450 /* Return session id or negative ret code. */
451 if (reply
.ret_code
>= LTTNG_OK
) {
452 ret
= -reply
.ret_code
;
453 ERR("Relayd data pending replied error %d", ret
);
456 /* At this point, the ret code is either 1 or 0 */
457 ret
= reply
.ret_code
;
459 DBG("Relayd data is %s pending for stream id %" PRIu64
,
460 ret
== 1 ? "NOT" : "", stream_id
);
467 * Check on the relayd side for a quiescent state on the control socket.
469 int relayd_quiescent_control(struct lttcomm_sock
*sock
)
472 struct lttcomm_relayd_generic_reply reply
;
474 /* Code flow error. Safety net. */
477 DBG("Relayd checking quiescent control state");
480 ret
= send_command(sock
, RELAYD_QUIESCENT_CONTROL
, NULL
, 0, 0);
485 /* Recevie response */
486 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
491 reply
.ret_code
= be32toh(reply
.ret_code
);
493 /* Return session id or negative ret code. */
494 if (reply
.ret_code
!= LTTNG_OK
) {
495 ret
= -reply
.ret_code
;
496 ERR("Relayd quiescent control replied error %d", ret
);
500 /* Control socket is quiescent */
508 * Begin a data pending command for a specific session id.
510 int relayd_begin_data_pending(struct lttcomm_sock
*sock
, uint64_t id
)
513 struct lttcomm_relayd_begin_data_pending msg
;
514 struct lttcomm_relayd_generic_reply reply
;
516 /* Code flow error. Safety net. */
519 DBG("Relayd begin data pending");
521 msg
.session_id
= htobe64(id
);
524 ret
= send_command(sock
, RELAYD_BEGIN_DATA_PENDING
, &msg
, sizeof(msg
), 0);
529 /* Recevie response */
530 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
535 reply
.ret_code
= be32toh(reply
.ret_code
);
537 /* Return session id or negative ret code. */
538 if (reply
.ret_code
!= LTTNG_OK
) {
539 ret
= -reply
.ret_code
;
540 ERR("Relayd begin data pending replied error %d", ret
);
551 * End a data pending command for a specific session id.
553 * Return 0 on success and set is_data_inflight to 0 if no data is being
554 * streamed or 1 if it is the case.
556 int relayd_end_data_pending(struct lttcomm_sock
*sock
, uint64_t id
,
557 unsigned int *is_data_inflight
)
560 struct lttcomm_relayd_end_data_pending msg
;
561 struct lttcomm_relayd_generic_reply reply
;
563 /* Code flow error. Safety net. */
566 DBG("Relayd end data pending");
568 msg
.session_id
= htobe64(id
);
571 ret
= send_command(sock
, RELAYD_END_DATA_PENDING
, &msg
, sizeof(msg
), 0);
576 /* Recevie response */
577 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
582 reply
.ret_code
= be32toh(reply
.ret_code
);
583 if (reply
.ret_code
< 0) {
584 ret
= reply
.ret_code
;
588 *is_data_inflight
= reply
.ret_code
;
590 DBG("Relayd end data pending is data inflight: %d", reply
.ret_code
);