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
);
73 DBG3("Relayd sending command %d", cmd
);
82 * Receive reply data on socket. This MUST be call after send_command or else
83 * could result in unexpected behavior(s).
85 static int recv_reply(struct lttcomm_sock
*sock
, void *data
, size_t size
)
89 DBG3("Relayd waiting for reply...");
91 ret
= sock
->ops
->recvmsg(sock
, data
, size
, 0);
102 * Create session on the relayd.
104 * On error, return ret_code negative value else return 0.
106 int relayd_create_session(struct lttcomm_sock
*sock
, const char *hostname
,
107 const char *session_name
)
110 struct lttcomm_relayd_create_session msg
;
111 struct lttcomm_relayd_generic_reply reply
;
113 /* Code flow error. Safety net. */
116 assert(session_name
);
118 DBG("Relayd creating session for hostname %s and session name %s",
119 hostname
, session_name
);
121 strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
));
122 strncpy(msg
.session_name
, session_name
, sizeof(msg
.session_name
));
125 ret
= send_command(sock
, RELAYD_CREATE_SESSION
, (void *) &msg
,
131 /* Recevie response */
132 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
137 /* Return session id or negative ret code. */
138 if (reply
.ret_code
!= LTTCOMM_OK
) {
139 ret
= -reply
.ret_code
;
145 DBG2("Relayd created session for %s", session_name
);
153 * Add stream on the relayd and assign stream handle to the stream_id argument.
155 * On success return 0 else return ret_code negative value.
157 int relayd_add_stream(struct lttcomm_sock
*sock
, const char *channel_name
,
158 const char *pathname
, uint64_t *stream_id
)
161 struct lttcomm_relayd_add_stream msg
;
162 struct lttcomm_relayd_status_stream reply
;
164 /* Code flow error. Safety net. */
166 assert(channel_name
);
169 DBG("Relayd adding stream for channel name %s", channel_name
);
171 strncpy(msg
.channel_name
, channel_name
, sizeof(msg
.channel_name
));
172 strncpy(msg
.pathname
, pathname
, sizeof(msg
.pathname
));
175 ret
= send_command(sock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
180 /* Recevie response */
181 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
186 /* Back to host bytes order. */
187 reply
.handle
= be64toh(reply
.handle
);
188 reply
.ret_code
= be32toh(reply
.ret_code
);
190 /* Return session id or negative ret code. */
191 if (reply
.ret_code
!= LTTCOMM_OK
) {
192 ret
= -reply
.ret_code
;
193 ERR("Relayd add stream replied error %d", ret
);
197 *stream_id
= reply
.handle
;
200 DBG("Relayd stream added successfully with handle %" PRIu64
,
208 * Check version numbers on the relayd.
210 * Return 0 if compatible else negative value.
212 int relayd_version_check(struct lttcomm_sock
*sock
, uint32_t major
,
216 struct lttcomm_relayd_version reply
;
218 /* Code flow error. Safety net. */
221 DBG("Relayd version check for major.minor %u.%u", major
, minor
);
224 ret
= send_command(sock
, RELAYD_VERSION
, NULL
, 0, 0);
229 /* Recevie response */
230 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
235 /* Set back to host bytes order */
236 reply
.major
= be32toh(reply
.major
);
237 reply
.minor
= be32toh(reply
.minor
);
239 /* Validate version */
240 if (reply
.major
<= major
) {
241 if (reply
.minor
<= minor
) {
244 DBG2("Relayd version is compatible");
249 /* Version number not compatible */
250 DBG2("Relayd version is NOT compatible %u.%u > %u.%u", reply
.major
,
251 reply
.minor
, major
, minor
);
260 * Start data command on the relayd.
262 * On success return 0 else return ret_code negative value.
264 int relayd_start_data(struct lttcomm_sock
*sock
)
267 struct lttcomm_relayd_generic_reply reply
;
269 /* Code flow error. Safety net. */
272 DBG("Relayd start data command");
275 ret
= send_command(sock
, RELAYD_START_DATA
, NULL
, 0, 0);
280 /* Recevie response */
281 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
286 /* Return session id or negative ret code. */
287 if (reply
.ret_code
!= LTTCOMM_OK
) {
288 ret
= -reply
.ret_code
;
300 * Add stream on the relayd and assign stream handle to the stream_id argument.
302 * On success return 0 else return ret_code negative value.
304 int relayd_send_metadata(struct lttcomm_sock
*sock
, size_t len
)
308 /* Code flow error. Safety net. */
311 DBG("Relayd sending metadata of size %zu", len
);
314 ret
= send_command(sock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
319 DBG2("Relayd metadata added successfully");
322 * After that call, the metadata data MUST be sent to the relayd so the
323 * receive size on the other end matches the len of the metadata packet
332 * Connect to relay daemon with an allocated lttcomm_sock.
334 int relayd_connect(struct lttcomm_sock
*sock
)
336 /* Code flow error. Safety net. */
339 DBG3("Relayd connect ...");
341 return sock
->ops
->connect(sock
);
345 * Close relayd socket with an allocated lttcomm_sock.
347 int relayd_close(struct lttcomm_sock
*sock
)
349 /* Code flow error. Safety net. */
352 DBG3("Relayd closing socket %d", sock
->fd
);
354 return sock
->ops
->close(sock
);
358 * Send data header structure to the relayd.
360 int relayd_send_data_hdr(struct lttcomm_sock
*sock
,
361 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
365 /* Code flow error. Safety net. */
369 DBG3("Relayd sending data header...");
371 /* Again, safety net */
373 size
= sizeof(struct lttcomm_relayd_data_hdr
);
376 /* Only send data header. */
377 ret
= sock
->ops
->sendmsg(sock
, hdr
, size
, 0);
383 * The data MUST be sent right after that command for the receive on the
384 * other end to match the size in the header.
392 * Send close stream command to the relayd.
394 int relayd_send_close_stream(struct lttcomm_sock
*sock
, uint64_t stream_id
,
395 uint64_t last_net_seq_num
)
398 struct lttcomm_relayd_close_stream msg
;
399 struct lttcomm_relayd_generic_reply reply
;
401 /* Code flow error. Safety net. */
404 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
406 msg
.stream_id
= htobe64(stream_id
);
407 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
410 ret
= send_command(sock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
415 /* Recevie response */
416 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
421 reply
.ret_code
= be32toh(reply
.ret_code
);
423 /* Return session id or negative ret code. */
424 if (reply
.ret_code
!= LTTCOMM_OK
) {
425 ret
= -reply
.ret_code
;
426 ERR("Relayd close stream replied error %d", ret
);
432 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);