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);
101 * Add stream on the relayd and assign stream handle to the stream_id argument.
103 * On success return 0 else return ret_code negative value.
105 int relayd_add_stream(struct lttcomm_sock
*sock
, const char *channel_name
,
106 const char *pathname
, uint64_t *stream_id
)
109 struct lttcomm_relayd_add_stream msg
;
110 struct lttcomm_relayd_status_stream reply
;
112 /* Code flow error. Safety net. */
114 assert(channel_name
);
117 DBG("Relayd adding stream for channel name %s", channel_name
);
119 strncpy(msg
.channel_name
, channel_name
, sizeof(msg
.channel_name
));
120 strncpy(msg
.pathname
, pathname
, sizeof(msg
.pathname
));
123 ret
= send_command(sock
, RELAYD_ADD_STREAM
, (void *) &msg
, sizeof(msg
), 0);
128 /* Recevie response */
129 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
134 /* Back to host bytes order. */
135 reply
.handle
= be64toh(reply
.handle
);
136 reply
.ret_code
= be32toh(reply
.ret_code
);
138 /* Return session id or negative ret code. */
139 if (reply
.ret_code
!= LTTCOMM_OK
) {
140 ret
= -reply
.ret_code
;
141 ERR("Relayd add stream replied error %d", ret
);
145 *stream_id
= reply
.handle
;
148 DBG("Relayd stream added successfully with handle %" PRIu64
,
156 * Check version numbers on the relayd.
158 * Return 0 if compatible else negative value.
160 int relayd_version_check(struct lttcomm_sock
*sock
, uint32_t major
,
164 struct lttcomm_relayd_version reply
;
166 /* Code flow error. Safety net. */
169 DBG("Relayd version check for major.minor %u.%u", major
, minor
);
172 ret
= send_command(sock
, RELAYD_VERSION
, NULL
, 0, 0);
177 /* Recevie response */
178 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
183 /* Set back to host bytes order */
184 reply
.major
= be32toh(reply
.major
);
185 reply
.minor
= be32toh(reply
.minor
);
187 /* Validate version */
188 if (reply
.major
<= major
) {
189 if (reply
.minor
<= minor
) {
192 DBG2("Relayd version is compatible");
197 /* Version number not compatible */
198 DBG2("Relayd version is NOT compatible %u.%u > %u.%u", reply
.major
,
199 reply
.minor
, major
, minor
);
207 * Add stream on the relayd and assign stream handle to the stream_id argument.
209 * On success return 0 else return ret_code negative value.
211 int relayd_send_metadata(struct lttcomm_sock
*sock
, size_t len
)
215 /* Code flow error. Safety net. */
218 DBG("Relayd sending metadata of size %zu", len
);
221 ret
= send_command(sock
, RELAYD_SEND_METADATA
, NULL
, len
, 0);
226 DBG2("Relayd metadata added successfully");
229 * After that call, the metadata data MUST be sent to the relayd so the
230 * receive size on the other end matches the len of the metadata packet
239 * Connect to relay daemon with an allocated lttcomm_sock.
241 int relayd_connect(struct lttcomm_sock
*sock
)
243 /* Code flow error. Safety net. */
246 DBG3("Relayd connect ...");
248 return sock
->ops
->connect(sock
);
252 * Close relayd socket with an allocated lttcomm_sock.
254 int relayd_close(struct lttcomm_sock
*sock
)
256 /* Code flow error. Safety net. */
259 DBG3("Relayd closing socket %d", sock
->fd
);
261 return sock
->ops
->close(sock
);
265 * Send data header structure to the relayd.
267 int relayd_send_data_hdr(struct lttcomm_sock
*sock
,
268 struct lttcomm_relayd_data_hdr
*hdr
, size_t size
)
272 /* Code flow error. Safety net. */
276 DBG3("Relayd sending data header...");
278 /* Again, safety net */
280 size
= sizeof(struct lttcomm_relayd_data_hdr
);
283 /* Only send data header. */
284 ret
= sock
->ops
->sendmsg(sock
, hdr
, size
, 0);
290 * The data MUST be sent right after that command for the receive on the
291 * other end to match the size in the header.
299 * Send close stream command to the relayd.
301 int relayd_send_close_stream(struct lttcomm_sock
*sock
, uint64_t stream_id
,
302 uint64_t last_net_seq_num
)
305 struct lttcomm_relayd_close_stream msg
;
306 struct lttcomm_relayd_generic_reply reply
;
308 /* Code flow error. Safety net. */
311 DBG("Relayd closing stream id %" PRIu64
, stream_id
);
313 msg
.stream_id
= htobe64(stream_id
);
314 msg
.last_net_seq_num
= htobe64(last_net_seq_num
);
317 ret
= send_command(sock
, RELAYD_CLOSE_STREAM
, (void *) &msg
, sizeof(msg
), 0);
322 /* Recevie response */
323 ret
= recv_reply(sock
, (void *) &reply
, sizeof(reply
));
328 reply
.ret_code
= be32toh(reply
.ret_code
);
330 /* Return session id or negative ret code. */
331 if (reply
.ret_code
!= LTTCOMM_OK
) {
332 ret
= -reply
.ret_code
;
333 ERR("Relayd close stream replied error %d", ret
);
339 DBG("Relayd close stream id %" PRIu64
" successfully", stream_id
);