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.
25 #include <common/common.h>
26 #include <common/defaults.h>
27 #include <common/sessiond-comm/relayd.h>
32 * Send command. Fill up the header and append the data.
34 static int send_command(struct lttcomm_sock
*sock
,
35 enum lttcomm_sessiond_command cmd
, void *data
, size_t size
,
39 struct lttcomm_relayd_hdr header
;
41 uint64_t buf_size
= sizeof(header
);
47 buf
= zmalloc(buf_size
);
49 PERROR("zmalloc relayd send command buf");
54 header
.cmd
= htobe32(cmd
);
55 header
.data_size
= htobe64(size
);
57 /* Zeroed for now since not used. */
58 header
.cmd_version
= 0;
59 header
.circuit_id
= 0;
61 /* Prepare buffer to send. */
62 memcpy(buf
, &header
, sizeof(header
));
64 memcpy(buf
+ sizeof(header
), data
, size
);
67 ret
= sock
->ops
->sendmsg(sock
, buf
, buf_size
, flags
);
72 DBG3("Relayd sending command %d", cmd
);
81 * Receive reply data on socket. This MUST be call after send_command or else
82 * could result in unexpected behavior(s).
84 static int recv_reply(struct lttcomm_sock
*sock
, void *data
, size_t size
)
88 DBG3("Relayd waiting for reply...");
90 ret
= sock
->ops
->recvmsg(sock
, data
, size
, 0);
101 * Create session on the relayd.
103 * On error, return ret_code negative value else return 0.
105 int relayd_create_session(struct lttcomm_sock
*sock
, const char *hostname
,
106 const char *session_name
)
109 struct lttcomm_relayd_create_session msg
;
110 struct lttcomm_relayd_generic_reply reply
;
112 /* Code flow error. Safety net. */
115 assert(session_name
);
117 DBG("Relayd creating session for hostname %s and session name %s",
118 hostname
, session_name
);
120 strncpy(msg
.hostname
, hostname
, sizeof(msg
.hostname
));
121 strncpy(msg
.session_name
, session_name
, sizeof(msg
.session_name
));
124 ret
= send_command(sock
, RELAYD_CREATE_SESSION
, (void *) &msg
,
130 /* Recevie response */
131 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
136 /* Return session id or negative ret code. */
137 if (reply
.ret_code
!= LTTCOMM_OK
) {
138 ret
= -reply
.ret_code
;
144 DBG2("Relayd created session for %s", session_name
);
152 * Add stream on the relayd and assign stream handle to the stream_id argument.
154 * On success return 0 else return ret_code negative value.
156 int relayd_add_stream(struct lttcomm_sock
*sock
, const char *channel_name
,
157 const char *pathname
, uint64_t *stream_id
)
160 struct lttcomm_relayd_add_stream msg
;
161 struct lttcomm_relayd_status_stream reply
;
163 /* Code flow error. Safety net. */
165 assert(channel_name
);
168 DBG("Relayd adding stream for channel name %s", channel_name
);
170 strncpy(msg
.channel_name
, channel_name
, sizeof(msg
.channel_name
));
171 strncpy(msg
.pathname
, pathname
, sizeof(msg
.pathname
));
174 ret
= send_command(sock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
179 /* Recevie response */
180 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
185 /* Back to host bytes order. */
186 reply
.handle
= be64toh(reply
.handle
);
187 reply
.ret_code
= be32toh(reply
.ret_code
);
189 /* Return session id or negative ret code. */
190 if (reply
.ret_code
!= LTTCOMM_OK
) {
191 ret
= -reply
.ret_code
;
192 ERR("Relayd add stream replied error %d", ret
);
196 *stream_id
= reply
.handle
;
199 DBG("Relayd stream added successfully with handle %zu", reply
.handle
);
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 reply
;
216 /* Code flow error. Safety net. */
219 DBG("Relayd version check for major.minor %u.%u", major
, minor
);
222 ret
= send_command(sock
, RELAYD_VERSION
, NULL
, 0, 0);
227 /* Recevie response */
228 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
233 /* Set back to host bytes order */
234 reply
.major
= be32toh(reply
.major
);
235 reply
.minor
= be32toh(reply
.minor
);
237 /* Validate version */
238 if (reply
.major
<= major
) {
239 if (reply
.minor
<= minor
) {
242 DBG2("Relayd version is compatible");
247 /* Version number not compatible */
248 DBG2("Relayd version is NOT compatible %u.%u > %u.%u", reply
.major
,
249 reply
.minor
, major
, minor
);
258 * Start data command on the relayd.
260 * On success return 0 else return ret_code negative value.
262 int relayd_start_data(struct lttcomm_sock
*sock
)
265 struct lttcomm_relayd_generic_reply reply
;
267 /* Code flow error. Safety net. */
270 DBG("Relayd start data command");
273 ret
= send_command(sock
, RELAYD_START_DATA
, NULL
, 0, 0);
278 /* Recevie response */
279 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
284 /* Return session id or negative ret code. */
285 if (reply
.ret_code
!= LTTCOMM_OK
) {
286 ret
= -reply
.ret_code
;
298 * Add stream on the relayd and assign stream handle to the stream_id argument.
300 * On success return 0 else return ret_code negative value.
302 int relayd_send_metadata(struct lttcomm_sock
*sock
, size_t len
)
306 /* Code flow error. Safety net. */
309 DBG("Relayd sending metadata of size %lu", len
);
312 ret
= send_command(sock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
317 DBG2("Relayd metadata added successfully");
320 * After that call, the metadata data MUST be sent to the relayd so the
321 * receive size on the other end matches the len of the metadata packet
330 * Connect to relay daemon with an allocated lttcomm_sock.
332 int relayd_connect(struct lttcomm_sock
*sock
)
334 /* Code flow error. Safety net. */
337 DBG3("Relayd connect ...");
339 return sock
->ops
->connect(sock
);
343 * Close relayd socket with an allocated lttcomm_sock.
345 int relayd_close(struct lttcomm_sock
*sock
)
347 /* Code flow error. Safety net. */
350 DBG3("Relayd closing socket %d", sock
->fd
);
352 return sock
->ops
->close(sock
);
356 * Send data header structure to the relayd.
358 int relayd_send_data_hdr(struct lttcomm_sock
*sock
,
359 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
363 /* Code flow error. Safety net. */
367 DBG3("Relayd sending data header...");
369 /* Again, safety net */
371 size
= sizeof(struct lttcomm_relayd_data_hdr
);
374 /* Only send data header. */
375 ret
= sock
->ops
->sendmsg(sock
, hdr
, size
, 0);
381 * The data MUST be sent right after that command for the receive on the
382 * other end to match the size in the header.