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_sessiond_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 * Add stream on the relayd and assign stream handle to the stream_id argument.
105 * On success return 0 else return ret_code negative value.
107 int relayd_add_stream(struct lttcomm_sock
*sock
, const char *channel_name
,
108 const char *pathname
, uint64_t *stream_id
)
111 struct lttcomm_relayd_add_stream msg
;
112 struct lttcomm_relayd_status_stream reply
;
114 /* Code flow error. Safety net. */
116 assert(channel_name
);
119 DBG("Relayd adding stream for channel name %s", channel_name
);
121 strncpy(msg
.channel_name
, channel_name
, sizeof(msg
.channel_name
));
122 strncpy(msg
.pathname
, pathname
, sizeof(msg
.pathname
));
125 ret
= send_command(sock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
130 /* Waiting for reply */
131 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
136 /* Back to host bytes order. */
137 reply
.handle
= be64toh(reply
.handle
);
138 reply
.ret_code
= be32toh(reply
.ret_code
);
140 /* Return session id or negative ret code. */
141 if (reply
.ret_code
!= LTTNG_OK
) {
142 ret
= -reply
.ret_code
;
143 ERR("Relayd add stream replied error %d", ret
);
147 *stream_id
= reply
.handle
;
150 DBG("Relayd stream added successfully with handle %" PRIu64
,
158 * Check version numbers on the relayd.
160 * Return 0 if compatible else negative value.
162 int relayd_version_check(struct lttcomm_sock
*sock
, uint32_t major
,
166 struct lttcomm_relayd_version msg
;
168 /* Code flow error. Safety net. */
171 DBG("Relayd version check for major.minor %u.%u", major
, minor
);
173 /* Prepare network byte order before transmission. */
174 msg
.major
= htobe32(major
);
175 msg
.minor
= htobe32(minor
);
178 ret
= send_command(sock
, RELAYD_VERSION
, (void *) &msg
, sizeof(msg
), 0);
183 /* Recevie response */
184 ret
= recv_reply(sock
, (void *) &msg
, sizeof(msg
));
189 /* Set back to host bytes order */
190 msg
.major
= be32toh(msg
.major
);
191 msg
.minor
= be32toh(msg
.minor
);
194 * Only validate the major version. If the other side is higher,
195 * communication is not possible. Only major version equal can talk to each
196 * other. If the minor version differs, the lowest version is used by both
199 * For now, before 2.1.0 stable release, we don't have to check the minor
200 * because this new mechanism with the relayd will only be available with
203 if (msg
.major
== major
) {
206 DBG2("Relayd version is compatible");
211 * After 2.1.0 release, for the 2.2 release, at this point will have to
212 * check the minor version in order for the session daemon to know which
213 * structure to use to communicate with the relayd. If the relayd's minor
214 * version is higher, it will adapt to our version so we can continue to
215 * use the latest relayd communication data structure.
218 /* Version number not compatible */
219 DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
228 * Add stream on the relayd and assign stream handle to the stream_id argument.
230 * On success return 0 else return ret_code negative value.
232 int relayd_send_metadata(struct lttcomm_sock
*sock
, size_t len
)
236 /* Code flow error. Safety net. */
239 DBG("Relayd sending metadata of size %zu", len
);
242 ret
= send_command(sock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
247 DBG2("Relayd metadata added successfully");
250 * After that call, the metadata data MUST be sent to the relayd so the
251 * receive size on the other end matches the len of the metadata packet
252 * header. This is why we don't wait for a reply here.
260 * Connect to relay daemon with an allocated lttcomm_sock.
262 int relayd_connect(struct lttcomm_sock
*sock
)
264 /* Code flow error. Safety net. */
267 DBG3("Relayd connect ...");
269 return sock
->ops
->connect(sock
);
273 * Close relayd socket with an allocated lttcomm_sock.
275 int relayd_close(struct lttcomm_sock
*sock
)
277 /* Code flow error. Safety net. */
280 DBG3("Relayd closing socket %d", sock
->fd
);
282 return sock
->ops
->close(sock
);
286 * Send data header structure to the relayd.
288 int relayd_send_data_hdr(struct lttcomm_sock
*sock
,
289 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
293 /* Code flow error. Safety net. */
297 DBG3("Relayd sending data header of size %ld", size
);
299 /* Again, safety net */
301 size
= sizeof(struct lttcomm_relayd_data_hdr
);
304 /* Only send data header. */
305 ret
= sock
->ops
->sendmsg(sock
, hdr
, size
, 0);
312 * The data MUST be sent right after that command for the receive on the
313 * other end to match the size in the header.
321 * Send close stream command to the relayd.
323 int relayd_send_close_stream(struct lttcomm_sock
*sock
, uint64_t stream_id
,
324 uint64_t last_net_seq_num
)
327 struct lttcomm_relayd_close_stream msg
;
328 struct lttcomm_relayd_generic_reply reply
;
330 /* Code flow error. Safety net. */
333 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
335 msg
.stream_id
= htobe64(stream_id
);
336 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
339 ret
= send_command(sock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
344 /* Recevie response */
345 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
350 reply
.ret_code
= be32toh(reply
.ret_code
);
352 /* Return session id or negative ret code. */
353 if (reply
.ret_code
!= LTTNG_OK
) {
354 ret
= -reply
.ret_code
;
355 ERR("Relayd close stream replied error %d", ret
);
361 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
368 * Check for data availability for a given stream id.
370 * Return 0 if NOT pending, 1 if so and a negative value on error.
372 int relayd_data_pending(struct lttcomm_sock
*sock
, uint64_t stream_id
,
373 uint64_t last_net_seq_num
)
376 struct lttcomm_relayd_data_pending msg
;
377 struct lttcomm_relayd_generic_reply reply
;
379 /* Code flow error. Safety net. */
382 DBG("Relayd data pending for stream id %" PRIu64
, stream_id
);
384 msg
.stream_id
= htobe64(stream_id
);
385 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
388 ret
= send_command(sock
, RELAYD_DATA_PENDING
, (void *) &msg
,
394 /* Recevie response */
395 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
400 reply
.ret_code
= be32toh(reply
.ret_code
);
402 /* Return session id or negative ret code. */
403 if (reply
.ret_code
>= LTTNG_OK
) {
404 ret
= -reply
.ret_code
;
405 ERR("Relayd data pending replied error %d", ret
);
408 /* At this point, the ret code is either 1 or 0 */
409 ret
= reply
.ret_code
;
411 DBG("Relayd data is %s pending for stream id %" PRIu64
,
412 ret
== 1 ? "NOT" : "", stream_id
);
419 * Check on the relayd side for a quiescent state on the control socket.
421 int relayd_quiescent_control(struct lttcomm_sock
*sock
)
424 struct lttcomm_relayd_generic_reply reply
;
426 /* Code flow error. Safety net. */
429 DBG("Relayd checking quiescent control state");
432 ret
= send_command(sock
, RELAYD_QUIESCENT_CONTROL
, NULL
, 0, 0);
437 /* Recevie response */
438 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
443 reply
.ret_code
= be32toh(reply
.ret_code
);
445 /* Return session id or negative ret code. */
446 if (reply
.ret_code
!= LTTNG_OK
) {
447 ret
= -reply
.ret_code
;
448 ERR("Relayd quiescent control replied error %d", ret
);
452 /* Control socket is quiescent */