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 reply
;
168 /* Code flow error. Safety net. */
171 DBG("Relayd version check for major.minor %u.%u", major
, minor
);
174 ret
= send_command(sock
, RELAYD_VERSION
, NULL
, 0, 0);
179 /* Recevie response */
180 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
185 /* Set back to host bytes order */
186 reply
.major
= be32toh(reply
.major
);
187 reply
.minor
= be32toh(reply
.minor
);
189 /* Validate version */
190 if (reply
.major
<= major
) {
191 if (reply
.minor
<= minor
) {
194 DBG2("Relayd version is compatible");
199 /* Version number not compatible */
200 DBG2("Relayd version is NOT compatible %u.%u > %u.%u", reply
.major
,
201 reply
.minor
, major
, minor
);
209 * Add stream on the relayd and assign stream handle to the stream_id argument.
211 * On success return 0 else return ret_code negative value.
213 int relayd_send_metadata(struct lttcomm_sock
*sock
, size_t len
)
217 /* Code flow error. Safety net. */
220 DBG("Relayd sending metadata of size %zu", len
);
223 ret
= send_command(sock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
228 DBG2("Relayd metadata added successfully");
231 * After that call, the metadata data MUST be sent to the relayd so the
232 * receive size on the other end matches the len of the metadata packet
233 * header. This is why we don't wait for a reply here.
241 * Connect to relay daemon with an allocated lttcomm_sock.
243 int relayd_connect(struct lttcomm_sock
*sock
)
245 /* Code flow error. Safety net. */
248 DBG3("Relayd connect ...");
250 return sock
->ops
->connect(sock
);
254 * Close relayd socket with an allocated lttcomm_sock.
256 int relayd_close(struct lttcomm_sock
*sock
)
258 /* Code flow error. Safety net. */
261 DBG3("Relayd closing socket %d", sock
->fd
);
263 return sock
->ops
->close(sock
);
267 * Send data header structure to the relayd.
269 int relayd_send_data_hdr(struct lttcomm_sock
*sock
,
270 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
274 /* Code flow error. Safety net. */
278 DBG3("Relayd sending data header of size %ld", size
);
280 /* Again, safety net */
282 size
= sizeof(struct lttcomm_relayd_data_hdr
);
285 /* Only send data header. */
286 ret
= sock
->ops
->sendmsg(sock
, hdr
, size
, 0);
293 * The data MUST be sent right after that command for the receive on the
294 * other end to match the size in the header.
302 * Send close stream command to the relayd.
304 int relayd_send_close_stream(struct lttcomm_sock
*sock
, uint64_t stream_id
,
305 uint64_t last_net_seq_num
)
308 struct lttcomm_relayd_close_stream msg
;
309 struct lttcomm_relayd_generic_reply reply
;
311 /* Code flow error. Safety net. */
314 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
316 msg
.stream_id
= htobe64(stream_id
);
317 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
320 ret
= send_command(sock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
325 /* Recevie response */
326 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
331 reply
.ret_code
= be32toh(reply
.ret_code
);
333 /* Return session id or negative ret code. */
334 if (reply
.ret_code
!= LTTNG_OK
) {
335 ret
= -reply
.ret_code
;
336 ERR("Relayd close stream replied error %d", ret
);
342 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);
349 * Check for data availability for a given stream id.
351 * Return 0 if NOT available, 1 if so and a negative value on error.
353 int relayd_data_available(struct lttcomm_sock
*sock
, uint64_t stream_id
,
354 uint64_t last_net_seq_num
)
357 struct lttcomm_relayd_data_available msg
;
358 struct lttcomm_relayd_generic_reply reply
;
360 /* Code flow error. Safety net. */
363 DBG("Relayd data available for stream id %" PRIu64
, stream_id
);
365 msg
.stream_id
= htobe64(stream_id
);
366 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
369 ret
= send_command(sock
, RELAYD_DATA_AVAILABLE
, (void *) &msg
,
375 /* Recevie response */
376 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
381 reply
.ret_code
= be32toh(reply
.ret_code
);
383 /* Return session id or negative ret code. */
384 if (reply
.ret_code
>= LTTNG_OK
) {
385 ret
= -reply
.ret_code
;
386 ERR("Relayd data available replied error %d", ret
);
389 /* At this point, the ret code is either 1 or 0 */
390 ret
= reply
.ret_code
;
392 DBG("Relayd data is %s available for stream id %" PRIu64
,
393 ret
== 1 ? "" : "NOT", stream_id
);
400 * Check on the relayd side for a quiescent state on the control socket.
402 int relayd_quiescent_control(struct lttcomm_sock
*sock
)
405 struct lttcomm_relayd_generic_reply reply
;
407 /* Code flow error. Safety net. */
410 DBG("Relayd checking quiescent control state");
413 ret
= send_command(sock
, RELAYD_QUIESCENT_CONTROL
, NULL
, 0, 0);
418 /* Recevie response */
419 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
424 reply
.ret_code
= be32toh(reply
.ret_code
);
426 /* Return session id or negative ret code. */
427 if (reply
.ret_code
!= LTTNG_OK
) {
428 ret
= -reply
.ret_code
;
429 ERR("Relayd quiescent control replied error %d", ret
);
433 /* Control socket is quiescent */