Commit | Line | Data |
---|---|---|
00e2e675 DG |
1 | /* |
2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
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. | |
7 | * | |
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 | |
11 | * more details. | |
12 | * | |
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. | |
16 | */ | |
17 | ||
18 | #define _GNU_SOURCE | |
6c1c0768 | 19 | #define _LGPL_SOURCE |
00e2e675 DG |
20 | #include <assert.h> |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <sys/stat.h> | |
77c7c900 | 25 | #include <inttypes.h> |
00e2e675 DG |
26 | |
27 | #include <common/common.h> | |
28 | #include <common/defaults.h> | |
f263b7fd | 29 | #include <common/compat/endian.h> |
00e2e675 | 30 | #include <common/sessiond-comm/relayd.h> |
50adc264 | 31 | #include <common/index/ctf-index.h> |
00e2e675 DG |
32 | |
33 | #include "relayd.h" | |
34 | ||
35 | /* | |
36 | * Send command. Fill up the header and append the data. | |
37 | */ | |
6151a90f | 38 | static int send_command(struct lttcomm_relayd_sock *rsock, |
7c9534d6 | 39 | enum lttcomm_relayd_command cmd, void *data, size_t size, |
00e2e675 DG |
40 | int flags) |
41 | { | |
42 | int ret; | |
43 | struct lttcomm_relayd_hdr header; | |
44 | char *buf; | |
45 | uint64_t buf_size = sizeof(header); | |
46 | ||
f96e4545 MD |
47 | if (rsock->sock.fd < 0) { |
48 | return -ECONNRESET; | |
49 | } | |
50 | ||
00e2e675 DG |
51 | if (data) { |
52 | buf_size += size; | |
53 | } | |
54 | ||
55 | buf = zmalloc(buf_size); | |
56 | if (buf == NULL) { | |
57 | PERROR("zmalloc relayd send command buf"); | |
58 | ret = -1; | |
59 | goto alloc_error; | |
60 | } | |
61 | ||
53efb85a | 62 | memset(&header, 0, sizeof(header)); |
00e2e675 DG |
63 | header.cmd = htobe32(cmd); |
64 | header.data_size = htobe64(size); | |
65 | ||
66 | /* Zeroed for now since not used. */ | |
67 | header.cmd_version = 0; | |
68 | header.circuit_id = 0; | |
69 | ||
70 | /* Prepare buffer to send. */ | |
71 | memcpy(buf, &header, sizeof(header)); | |
72 | if (data) { | |
73 | memcpy(buf + sizeof(header), data, size); | |
74 | } | |
75 | ||
6151a90f | 76 | ret = rsock->sock.ops->sendmsg(&rsock->sock, buf, buf_size, flags); |
00e2e675 | 77 | if (ret < 0) { |
8994307f | 78 | ret = -errno; |
00e2e675 DG |
79 | goto error; |
80 | } | |
81 | ||
633d0084 | 82 | DBG3("Relayd sending command %d of size %" PRIu64, cmd, buf_size); |
00e2e675 DG |
83 | |
84 | error: | |
85 | free(buf); | |
86 | alloc_error: | |
87 | return ret; | |
88 | } | |
89 | ||
90 | /* | |
91 | * Receive reply data on socket. This MUST be call after send_command or else | |
92 | * could result in unexpected behavior(s). | |
93 | */ | |
6151a90f | 94 | static int recv_reply(struct lttcomm_relayd_sock *rsock, void *data, size_t size) |
00e2e675 DG |
95 | { |
96 | int ret; | |
97 | ||
f96e4545 MD |
98 | if (rsock->sock.fd < 0) { |
99 | return -ECONNRESET; | |
100 | } | |
101 | ||
8fd623e0 | 102 | DBG3("Relayd waiting for reply of size %zu", size); |
00e2e675 | 103 | |
6151a90f | 104 | ret = rsock->sock.ops->recvmsg(&rsock->sock, data, size, 0); |
20275fe8 DG |
105 | if (ret <= 0 || ret != size) { |
106 | if (ret == 0) { | |
107 | /* Orderly shutdown. */ | |
6151a90f | 108 | DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd); |
20275fe8 | 109 | } else { |
8fd623e0 | 110 | DBG("Receiving reply failed on sock %d for size %zu with ret %d", |
6151a90f | 111 | rsock->sock.fd, size, ret); |
20275fe8 DG |
112 | } |
113 | /* Always return -1 here and the caller can use errno. */ | |
114 | ret = -1; | |
00e2e675 DG |
115 | goto error; |
116 | } | |
117 | ||
118 | error: | |
119 | return ret; | |
120 | } | |
121 | ||
d3e2ba59 JD |
122 | /* |
123 | * Starting at 2.4, RELAYD_CREATE_SESSION takes additional parameters to | |
124 | * support the live reading capability. | |
125 | */ | |
126 | static int relayd_create_session_2_4(struct lttcomm_relayd_sock *rsock, | |
127 | uint64_t *session_id, char *session_name, char *hostname, | |
7d2f7452 | 128 | int session_live_timer, unsigned int snapshot) |
d3e2ba59 JD |
129 | { |
130 | int ret; | |
131 | struct lttcomm_relayd_create_session_2_4 msg; | |
132 | ||
133 | strncpy(msg.session_name, session_name, sizeof(msg.session_name)); | |
134 | strncpy(msg.hostname, hostname, sizeof(msg.hostname)); | |
135 | msg.live_timer = htobe32(session_live_timer); | |
7d2f7452 | 136 | msg.snapshot = htobe32(snapshot); |
d3e2ba59 JD |
137 | |
138 | /* Send command */ | |
139 | ret = send_command(rsock, RELAYD_CREATE_SESSION, &msg, sizeof(msg), 0); | |
140 | if (ret < 0) { | |
141 | goto error; | |
142 | } | |
143 | ||
144 | error: | |
145 | return ret; | |
146 | } | |
147 | ||
148 | /* | |
149 | * RELAYD_CREATE_SESSION from 2.1 to 2.3. | |
150 | */ | |
151 | static int relayd_create_session_2_1(struct lttcomm_relayd_sock *rsock, | |
152 | uint64_t *session_id) | |
153 | { | |
154 | int ret; | |
155 | ||
156 | /* Send command */ | |
157 | ret = send_command(rsock, RELAYD_CREATE_SESSION, NULL, 0, 0); | |
158 | if (ret < 0) { | |
159 | goto error; | |
160 | } | |
161 | ||
162 | error: | |
163 | return ret; | |
164 | } | |
165 | ||
c5b6f4f0 DG |
166 | /* |
167 | * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and | |
168 | * set session_id of the relayd if we have a successful reply from the relayd. | |
169 | * | |
20275fe8 DG |
170 | * On success, return 0 else a negative value which is either an errno error or |
171 | * a lttng error code from the relayd. | |
c5b6f4f0 | 172 | */ |
d3e2ba59 | 173 | int relayd_create_session(struct lttcomm_relayd_sock *rsock, uint64_t *session_id, |
7d2f7452 DG |
174 | char *session_name, char *hostname, int session_live_timer, |
175 | unsigned int snapshot) | |
c5b6f4f0 DG |
176 | { |
177 | int ret; | |
178 | struct lttcomm_relayd_status_session reply; | |
179 | ||
6151a90f | 180 | assert(rsock); |
c5b6f4f0 DG |
181 | assert(session_id); |
182 | ||
183 | DBG("Relayd create session"); | |
184 | ||
d3e2ba59 JD |
185 | switch(rsock->minor) { |
186 | case 1: | |
187 | case 2: | |
188 | case 3: | |
189 | ret = relayd_create_session_2_1(rsock, session_id); | |
31d74dc3 | 190 | break; |
d3e2ba59 JD |
191 | case 4: |
192 | default: | |
7d2f7452 DG |
193 | ret = relayd_create_session_2_4(rsock, session_id, session_name, |
194 | hostname, session_live_timer, snapshot); | |
31d74dc3 | 195 | break; |
d3e2ba59 JD |
196 | } |
197 | ||
c5b6f4f0 DG |
198 | if (ret < 0) { |
199 | goto error; | |
200 | } | |
201 | ||
20275fe8 | 202 | /* Receive response */ |
6151a90f | 203 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
c5b6f4f0 DG |
204 | if (ret < 0) { |
205 | goto error; | |
206 | } | |
207 | ||
208 | reply.session_id = be64toh(reply.session_id); | |
209 | reply.ret_code = be32toh(reply.ret_code); | |
210 | ||
211 | /* Return session id or negative ret code. */ | |
212 | if (reply.ret_code != LTTNG_OK) { | |
bb63afd9 DG |
213 | ret = -1; |
214 | ERR("Relayd create session replied error %d", reply.ret_code); | |
c5b6f4f0 DG |
215 | goto error; |
216 | } else { | |
217 | ret = 0; | |
218 | *session_id = reply.session_id; | |
219 | } | |
220 | ||
221 | DBG("Relayd session created with id %" PRIu64, reply.session_id); | |
222 | ||
223 | error: | |
224 | return ret; | |
225 | } | |
226 | ||
00e2e675 DG |
227 | /* |
228 | * Add stream on the relayd and assign stream handle to the stream_id argument. | |
229 | * | |
230 | * On success return 0 else return ret_code negative value. | |
231 | */ | |
6151a90f | 232 | int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name, |
0f907de1 JD |
233 | const char *pathname, uint64_t *stream_id, |
234 | uint64_t tracefile_size, uint64_t tracefile_count) | |
00e2e675 DG |
235 | { |
236 | int ret; | |
237 | struct lttcomm_relayd_add_stream msg; | |
0f907de1 | 238 | struct lttcomm_relayd_add_stream_2_2 msg_2_2; |
00e2e675 DG |
239 | struct lttcomm_relayd_status_stream reply; |
240 | ||
241 | /* Code flow error. Safety net. */ | |
6151a90f | 242 | assert(rsock); |
00e2e675 DG |
243 | assert(channel_name); |
244 | assert(pathname); | |
245 | ||
246 | DBG("Relayd adding stream for channel name %s", channel_name); | |
247 | ||
0f907de1 JD |
248 | /* Compat with relayd 2.1 */ |
249 | if (rsock->minor == 1) { | |
53efb85a | 250 | memset(&msg, 0, sizeof(msg)); |
0f907de1 JD |
251 | strncpy(msg.channel_name, channel_name, sizeof(msg.channel_name)); |
252 | strncpy(msg.pathname, pathname, sizeof(msg.pathname)); | |
00e2e675 | 253 | |
0f907de1 JD |
254 | /* Send command */ |
255 | ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0); | |
256 | if (ret < 0) { | |
257 | goto error; | |
258 | } | |
259 | } else { | |
53efb85a | 260 | memset(&msg_2_2, 0, sizeof(msg_2_2)); |
0f907de1 JD |
261 | /* Compat with relayd 2.2+ */ |
262 | strncpy(msg_2_2.channel_name, channel_name, sizeof(msg_2_2.channel_name)); | |
263 | strncpy(msg_2_2.pathname, pathname, sizeof(msg_2_2.pathname)); | |
264 | msg_2_2.tracefile_size = htobe64(tracefile_size); | |
265 | msg_2_2.tracefile_count = htobe64(tracefile_count); | |
266 | ||
267 | /* Send command */ | |
268 | ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg_2_2, sizeof(msg_2_2), 0); | |
269 | if (ret < 0) { | |
270 | goto error; | |
271 | } | |
00e2e675 DG |
272 | } |
273 | ||
633d0084 | 274 | /* Waiting for reply */ |
6151a90f | 275 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
00e2e675 DG |
276 | if (ret < 0) { |
277 | goto error; | |
278 | } | |
279 | ||
280 | /* Back to host bytes order. */ | |
281 | reply.handle = be64toh(reply.handle); | |
282 | reply.ret_code = be32toh(reply.ret_code); | |
283 | ||
284 | /* Return session id or negative ret code. */ | |
f73fabfd | 285 | if (reply.ret_code != LTTNG_OK) { |
bb63afd9 DG |
286 | ret = -1; |
287 | ERR("Relayd add stream replied error %d", reply.ret_code); | |
00e2e675 DG |
288 | } else { |
289 | /* Success */ | |
290 | ret = 0; | |
291 | *stream_id = reply.handle; | |
292 | } | |
293 | ||
77c7c900 MD |
294 | DBG("Relayd stream added successfully with handle %" PRIu64, |
295 | reply.handle); | |
00e2e675 DG |
296 | |
297 | error: | |
298 | return ret; | |
299 | } | |
300 | ||
a4baae1b JD |
301 | /* |
302 | * Inform the relay that all the streams for the current channel has been sent. | |
303 | * | |
304 | * On success return 0 else return ret_code negative value. | |
305 | */ | |
306 | int relayd_streams_sent(struct lttcomm_relayd_sock *rsock) | |
307 | { | |
308 | int ret; | |
309 | struct lttcomm_relayd_generic_reply reply; | |
310 | ||
311 | /* Code flow error. Safety net. */ | |
312 | assert(rsock); | |
313 | ||
314 | DBG("Relayd sending streams sent."); | |
315 | ||
316 | /* This feature was introduced in 2.4, ignore it for earlier versions. */ | |
317 | if (rsock->minor < 4) { | |
318 | ret = 0; | |
319 | goto end; | |
320 | } | |
321 | ||
322 | /* Send command */ | |
323 | ret = send_command(rsock, RELAYD_STREAMS_SENT, NULL, 0, 0); | |
324 | if (ret < 0) { | |
325 | goto error; | |
326 | } | |
327 | ||
328 | /* Waiting for reply */ | |
329 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); | |
330 | if (ret < 0) { | |
331 | goto error; | |
332 | } | |
333 | ||
334 | /* Back to host bytes order. */ | |
335 | reply.ret_code = be32toh(reply.ret_code); | |
336 | ||
337 | /* Return session id or negative ret code. */ | |
338 | if (reply.ret_code != LTTNG_OK) { | |
339 | ret = -1; | |
340 | ERR("Relayd streams sent replied error %d", reply.ret_code); | |
341 | goto error; | |
342 | } else { | |
343 | /* Success */ | |
344 | ret = 0; | |
345 | } | |
346 | ||
347 | DBG("Relayd streams sent success"); | |
348 | ||
349 | error: | |
350 | end: | |
351 | return ret; | |
352 | } | |
353 | ||
00e2e675 DG |
354 | /* |
355 | * Check version numbers on the relayd. | |
d4519fa3 JD |
356 | * If major versions are compatible, we assign minor_to_use to the |
357 | * minor version of the procotol we are going to use for this session. | |
00e2e675 DG |
358 | * |
359 | * Return 0 if compatible else negative value. | |
360 | */ | |
6151a90f | 361 | int relayd_version_check(struct lttcomm_relayd_sock *rsock) |
00e2e675 DG |
362 | { |
363 | int ret; | |
092b6259 | 364 | struct lttcomm_relayd_version msg; |
00e2e675 DG |
365 | |
366 | /* Code flow error. Safety net. */ | |
6151a90f | 367 | assert(rsock); |
00e2e675 | 368 | |
6151a90f JD |
369 | DBG("Relayd version check for major.minor %u.%u", rsock->major, |
370 | rsock->minor); | |
00e2e675 | 371 | |
53efb85a | 372 | memset(&msg, 0, sizeof(msg)); |
092b6259 | 373 | /* Prepare network byte order before transmission. */ |
6151a90f JD |
374 | msg.major = htobe32(rsock->major); |
375 | msg.minor = htobe32(rsock->minor); | |
092b6259 | 376 | |
00e2e675 | 377 | /* Send command */ |
6151a90f | 378 | ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0); |
00e2e675 DG |
379 | if (ret < 0) { |
380 | goto error; | |
381 | } | |
382 | ||
20275fe8 | 383 | /* Receive response */ |
6151a90f | 384 | ret = recv_reply(rsock, (void *) &msg, sizeof(msg)); |
00e2e675 DG |
385 | if (ret < 0) { |
386 | goto error; | |
387 | } | |
388 | ||
389 | /* Set back to host bytes order */ | |
092b6259 DG |
390 | msg.major = be32toh(msg.major); |
391 | msg.minor = be32toh(msg.minor); | |
392 | ||
393 | /* | |
394 | * Only validate the major version. If the other side is higher, | |
395 | * communication is not possible. Only major version equal can talk to each | |
396 | * other. If the minor version differs, the lowest version is used by both | |
397 | * sides. | |
092b6259 | 398 | */ |
6151a90f | 399 | if (msg.major != rsock->major) { |
d4519fa3 JD |
400 | /* Not compatible */ |
401 | ret = -1; | |
402 | DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)", | |
6151a90f | 403 | msg.major, rsock->major); |
092b6259 | 404 | goto error; |
00e2e675 DG |
405 | } |
406 | ||
092b6259 | 407 | /* |
6151a90f JD |
408 | * If the relayd's minor version is higher, it will adapt to our version so |
409 | * we can continue to use the latest relayd communication data structure. | |
410 | * If the received minor version is higher, the relayd should adapt to us. | |
092b6259 | 411 | */ |
6151a90f JD |
412 | if (rsock->minor > msg.minor) { |
413 | rsock->minor = msg.minor; | |
d4519fa3 | 414 | } |
092b6259 | 415 | |
d4519fa3 JD |
416 | /* Version number compatible */ |
417 | DBG2("Relayd version is compatible, using protocol version %u.%u", | |
6151a90f | 418 | rsock->major, rsock->minor); |
d4519fa3 | 419 | ret = 0; |
00e2e675 DG |
420 | |
421 | error: | |
422 | return ret; | |
423 | } | |
424 | ||
00e2e675 DG |
425 | /* |
426 | * Add stream on the relayd and assign stream handle to the stream_id argument. | |
427 | * | |
428 | * On success return 0 else return ret_code negative value. | |
429 | */ | |
6151a90f | 430 | int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len) |
00e2e675 DG |
431 | { |
432 | int ret; | |
433 | ||
434 | /* Code flow error. Safety net. */ | |
6151a90f | 435 | assert(rsock); |
00e2e675 | 436 | |
77c7c900 | 437 | DBG("Relayd sending metadata of size %zu", len); |
00e2e675 DG |
438 | |
439 | /* Send command */ | |
6151a90f | 440 | ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0); |
00e2e675 DG |
441 | if (ret < 0) { |
442 | goto error; | |
443 | } | |
444 | ||
445 | DBG2("Relayd metadata added successfully"); | |
446 | ||
447 | /* | |
448 | * After that call, the metadata data MUST be sent to the relayd so the | |
449 | * receive size on the other end matches the len of the metadata packet | |
633d0084 | 450 | * header. This is why we don't wait for a reply here. |
00e2e675 DG |
451 | */ |
452 | ||
453 | error: | |
454 | return ret; | |
455 | } | |
456 | ||
457 | /* | |
6151a90f | 458 | * Connect to relay daemon with an allocated lttcomm_relayd_sock. |
00e2e675 | 459 | */ |
6151a90f | 460 | int relayd_connect(struct lttcomm_relayd_sock *rsock) |
00e2e675 DG |
461 | { |
462 | /* Code flow error. Safety net. */ | |
6151a90f | 463 | assert(rsock); |
00e2e675 | 464 | |
f96e4545 MD |
465 | if (!rsock->sock.ops) { |
466 | /* | |
467 | * Attempting a connect on a non-initialized socket. | |
468 | */ | |
469 | return -ECONNRESET; | |
470 | } | |
471 | ||
00e2e675 DG |
472 | DBG3("Relayd connect ..."); |
473 | ||
6151a90f | 474 | return rsock->sock.ops->connect(&rsock->sock); |
00e2e675 DG |
475 | } |
476 | ||
477 | /* | |
6151a90f | 478 | * Close relayd socket with an allocated lttcomm_relayd_sock. |
ffe60014 DG |
479 | * |
480 | * If no socket operations are found, simply return 0 meaning that everything | |
481 | * is fine. Without operations, the socket can not possibly be opened or used. | |
482 | * This is possible if the socket was allocated but not created. However, the | |
483 | * caller could simply use it to store a valid file descriptor for instance | |
484 | * passed over a Unix socket and call this to cleanup but still without a valid | |
485 | * ops pointer. | |
486 | * | |
487 | * Return the close returned value. On error, a negative value is usually | |
488 | * returned back from close(2). | |
00e2e675 | 489 | */ |
6151a90f | 490 | int relayd_close(struct lttcomm_relayd_sock *rsock) |
00e2e675 | 491 | { |
ffe60014 DG |
492 | int ret; |
493 | ||
00e2e675 | 494 | /* Code flow error. Safety net. */ |
6151a90f | 495 | assert(rsock); |
00e2e675 | 496 | |
ffe60014 | 497 | /* An invalid fd is fine, return success. */ |
6151a90f | 498 | if (rsock->sock.fd < 0) { |
ffe60014 DG |
499 | ret = 0; |
500 | goto end; | |
501 | } | |
502 | ||
6151a90f | 503 | DBG3("Relayd closing socket %d", rsock->sock.fd); |
00e2e675 | 504 | |
6151a90f JD |
505 | if (rsock->sock.ops) { |
506 | ret = rsock->sock.ops->close(&rsock->sock); | |
ffe60014 DG |
507 | } else { |
508 | /* Default call if no specific ops found. */ | |
6151a90f | 509 | ret = close(rsock->sock.fd); |
ffe60014 DG |
510 | if (ret < 0) { |
511 | PERROR("relayd_close default close"); | |
512 | } | |
513 | } | |
f96e4545 | 514 | rsock->sock.fd = -1; |
ffe60014 DG |
515 | |
516 | end: | |
517 | return ret; | |
00e2e675 DG |
518 | } |
519 | ||
520 | /* | |
521 | * Send data header structure to the relayd. | |
522 | */ | |
6151a90f | 523 | int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock, |
00e2e675 DG |
524 | struct lttcomm_relayd_data_hdr *hdr, size_t size) |
525 | { | |
526 | int ret; | |
527 | ||
528 | /* Code flow error. Safety net. */ | |
6151a90f | 529 | assert(rsock); |
00e2e675 DG |
530 | assert(hdr); |
531 | ||
f96e4545 MD |
532 | if (rsock->sock.fd < 0) { |
533 | return -ECONNRESET; | |
534 | } | |
535 | ||
8fd623e0 | 536 | DBG3("Relayd sending data header of size %zu", size); |
00e2e675 DG |
537 | |
538 | /* Again, safety net */ | |
539 | if (size == 0) { | |
540 | size = sizeof(struct lttcomm_relayd_data_hdr); | |
541 | } | |
542 | ||
543 | /* Only send data header. */ | |
6151a90f | 544 | ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0); |
00e2e675 | 545 | if (ret < 0) { |
8994307f | 546 | ret = -errno; |
00e2e675 DG |
547 | goto error; |
548 | } | |
549 | ||
550 | /* | |
551 | * The data MUST be sent right after that command for the receive on the | |
552 | * other end to match the size in the header. | |
553 | */ | |
554 | ||
555 | error: | |
556 | return ret; | |
557 | } | |
173af62f DG |
558 | |
559 | /* | |
560 | * Send close stream command to the relayd. | |
561 | */ | |
6151a90f | 562 | int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id, |
173af62f DG |
563 | uint64_t last_net_seq_num) |
564 | { | |
565 | int ret; | |
566 | struct lttcomm_relayd_close_stream msg; | |
567 | struct lttcomm_relayd_generic_reply reply; | |
568 | ||
569 | /* Code flow error. Safety net. */ | |
6151a90f | 570 | assert(rsock); |
173af62f | 571 | |
77c7c900 | 572 | DBG("Relayd closing stream id %" PRIu64, stream_id); |
173af62f | 573 | |
53efb85a | 574 | memset(&msg, 0, sizeof(msg)); |
173af62f DG |
575 | msg.stream_id = htobe64(stream_id); |
576 | msg.last_net_seq_num = htobe64(last_net_seq_num); | |
577 | ||
578 | /* Send command */ | |
6151a90f | 579 | ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0); |
173af62f DG |
580 | if (ret < 0) { |
581 | goto error; | |
582 | } | |
583 | ||
20275fe8 | 584 | /* Receive response */ |
6151a90f | 585 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
173af62f DG |
586 | if (ret < 0) { |
587 | goto error; | |
588 | } | |
589 | ||
590 | reply.ret_code = be32toh(reply.ret_code); | |
591 | ||
592 | /* Return session id or negative ret code. */ | |
f73fabfd | 593 | if (reply.ret_code != LTTNG_OK) { |
bb63afd9 DG |
594 | ret = -1; |
595 | ERR("Relayd close stream replied error %d", reply.ret_code); | |
173af62f DG |
596 | } else { |
597 | /* Success */ | |
598 | ret = 0; | |
599 | } | |
600 | ||
77c7c900 | 601 | DBG("Relayd close stream id %" PRIu64 " successfully", stream_id); |
173af62f DG |
602 | |
603 | error: | |
604 | return ret; | |
605 | } | |
c8f59ee5 DG |
606 | |
607 | /* | |
608 | * Check for data availability for a given stream id. | |
609 | * | |
6d805429 | 610 | * Return 0 if NOT pending, 1 if so and a negative value on error. |
c8f59ee5 | 611 | */ |
6151a90f | 612 | int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id, |
c8f59ee5 DG |
613 | uint64_t last_net_seq_num) |
614 | { | |
615 | int ret; | |
6d805429 | 616 | struct lttcomm_relayd_data_pending msg; |
c8f59ee5 DG |
617 | struct lttcomm_relayd_generic_reply reply; |
618 | ||
619 | /* Code flow error. Safety net. */ | |
6151a90f | 620 | assert(rsock); |
c8f59ee5 | 621 | |
6d805429 | 622 | DBG("Relayd data pending for stream id %" PRIu64, stream_id); |
c8f59ee5 | 623 | |
53efb85a | 624 | memset(&msg, 0, sizeof(msg)); |
c8f59ee5 DG |
625 | msg.stream_id = htobe64(stream_id); |
626 | msg.last_net_seq_num = htobe64(last_net_seq_num); | |
627 | ||
628 | /* Send command */ | |
6151a90f | 629 | ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg, |
c8f59ee5 DG |
630 | sizeof(msg), 0); |
631 | if (ret < 0) { | |
632 | goto error; | |
633 | } | |
634 | ||
20275fe8 | 635 | /* Receive response */ |
6151a90f | 636 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
c8f59ee5 DG |
637 | if (ret < 0) { |
638 | goto error; | |
639 | } | |
640 | ||
641 | reply.ret_code = be32toh(reply.ret_code); | |
642 | ||
643 | /* Return session id or negative ret code. */ | |
644 | if (reply.ret_code >= LTTNG_OK) { | |
bb63afd9 | 645 | ERR("Relayd data pending replied error %d", reply.ret_code); |
c8f59ee5 DG |
646 | } |
647 | ||
648 | /* At this point, the ret code is either 1 or 0 */ | |
649 | ret = reply.ret_code; | |
650 | ||
6d805429 | 651 | DBG("Relayd data is %s pending for stream id %" PRIu64, |
9dd26bb9 | 652 | ret == 1 ? "" : "NOT", stream_id); |
c8f59ee5 DG |
653 | |
654 | error: | |
655 | return ret; | |
656 | } | |
657 | ||
658 | /* | |
659 | * Check on the relayd side for a quiescent state on the control socket. | |
660 | */ | |
6151a90f | 661 | int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock, |
ad7051c0 | 662 | uint64_t metadata_stream_id) |
c8f59ee5 DG |
663 | { |
664 | int ret; | |
ad7051c0 | 665 | struct lttcomm_relayd_quiescent_control msg; |
c8f59ee5 DG |
666 | struct lttcomm_relayd_generic_reply reply; |
667 | ||
668 | /* Code flow error. Safety net. */ | |
6151a90f | 669 | assert(rsock); |
c8f59ee5 DG |
670 | |
671 | DBG("Relayd checking quiescent control state"); | |
672 | ||
53efb85a | 673 | memset(&msg, 0, sizeof(msg)); |
ad7051c0 DG |
674 | msg.stream_id = htobe64(metadata_stream_id); |
675 | ||
c8f59ee5 | 676 | /* Send command */ |
6151a90f | 677 | ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0); |
c8f59ee5 DG |
678 | if (ret < 0) { |
679 | goto error; | |
680 | } | |
681 | ||
20275fe8 | 682 | /* Receive response */ |
6151a90f | 683 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
c8f59ee5 DG |
684 | if (ret < 0) { |
685 | goto error; | |
686 | } | |
687 | ||
688 | reply.ret_code = be32toh(reply.ret_code); | |
689 | ||
690 | /* Return session id or negative ret code. */ | |
691 | if (reply.ret_code != LTTNG_OK) { | |
bb63afd9 DG |
692 | ret = -1; |
693 | ERR("Relayd quiescent control replied error %d", reply.ret_code); | |
c8f59ee5 DG |
694 | goto error; |
695 | } | |
696 | ||
697 | /* Control socket is quiescent */ | |
6d805429 | 698 | return 0; |
c8f59ee5 DG |
699 | |
700 | error: | |
701 | return ret; | |
702 | } | |
f7079f67 DG |
703 | |
704 | /* | |
705 | * Begin a data pending command for a specific session id. | |
706 | */ | |
6151a90f | 707 | int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id) |
f7079f67 DG |
708 | { |
709 | int ret; | |
710 | struct lttcomm_relayd_begin_data_pending msg; | |
711 | struct lttcomm_relayd_generic_reply reply; | |
712 | ||
713 | /* Code flow error. Safety net. */ | |
6151a90f | 714 | assert(rsock); |
f7079f67 DG |
715 | |
716 | DBG("Relayd begin data pending"); | |
717 | ||
53efb85a | 718 | memset(&msg, 0, sizeof(msg)); |
f7079f67 DG |
719 | msg.session_id = htobe64(id); |
720 | ||
721 | /* Send command */ | |
6151a90f | 722 | ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0); |
f7079f67 DG |
723 | if (ret < 0) { |
724 | goto error; | |
725 | } | |
726 | ||
20275fe8 | 727 | /* Receive response */ |
6151a90f | 728 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
f7079f67 DG |
729 | if (ret < 0) { |
730 | goto error; | |
731 | } | |
732 | ||
733 | reply.ret_code = be32toh(reply.ret_code); | |
734 | ||
735 | /* Return session id or negative ret code. */ | |
736 | if (reply.ret_code != LTTNG_OK) { | |
bb63afd9 DG |
737 | ret = -1; |
738 | ERR("Relayd begin data pending replied error %d", reply.ret_code); | |
f7079f67 DG |
739 | goto error; |
740 | } | |
741 | ||
742 | return 0; | |
743 | ||
744 | error: | |
745 | return ret; | |
746 | } | |
747 | ||
748 | /* | |
749 | * End a data pending command for a specific session id. | |
750 | * | |
751 | * Return 0 on success and set is_data_inflight to 0 if no data is being | |
752 | * streamed or 1 if it is the case. | |
753 | */ | |
6151a90f | 754 | int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id, |
f7079f67 DG |
755 | unsigned int *is_data_inflight) |
756 | { | |
af6c30b5 | 757 | int ret, recv_ret; |
f7079f67 DG |
758 | struct lttcomm_relayd_end_data_pending msg; |
759 | struct lttcomm_relayd_generic_reply reply; | |
760 | ||
761 | /* Code flow error. Safety net. */ | |
6151a90f | 762 | assert(rsock); |
f7079f67 DG |
763 | |
764 | DBG("Relayd end data pending"); | |
765 | ||
53efb85a | 766 | memset(&msg, 0, sizeof(msg)); |
f7079f67 DG |
767 | msg.session_id = htobe64(id); |
768 | ||
769 | /* Send command */ | |
6151a90f | 770 | ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0); |
f7079f67 DG |
771 | if (ret < 0) { |
772 | goto error; | |
773 | } | |
774 | ||
20275fe8 | 775 | /* Receive response */ |
6151a90f | 776 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
f7079f67 DG |
777 | if (ret < 0) { |
778 | goto error; | |
779 | } | |
780 | ||
af6c30b5 DG |
781 | recv_ret = be32toh(reply.ret_code); |
782 | if (recv_ret < 0) { | |
783 | ret = recv_ret; | |
f7079f67 DG |
784 | goto error; |
785 | } | |
786 | ||
af6c30b5 | 787 | *is_data_inflight = recv_ret; |
f7079f67 | 788 | |
af6c30b5 | 789 | DBG("Relayd end data pending is data inflight: %d", recv_ret); |
f7079f67 DG |
790 | |
791 | return 0; | |
792 | ||
793 | error: | |
794 | return ret; | |
795 | } | |
1c20f0e2 JD |
796 | |
797 | /* | |
798 | * Send index to the relayd. | |
799 | */ | |
800 | int relayd_send_index(struct lttcomm_relayd_sock *rsock, | |
50adc264 | 801 | struct ctf_packet_index *index, uint64_t relay_stream_id, |
1c20f0e2 JD |
802 | uint64_t net_seq_num) |
803 | { | |
804 | int ret; | |
805 | struct lttcomm_relayd_index msg; | |
806 | struct lttcomm_relayd_generic_reply reply; | |
807 | ||
808 | /* Code flow error. Safety net. */ | |
809 | assert(rsock); | |
810 | ||
811 | if (rsock->minor < 4) { | |
812 | DBG("Not sending indexes before protocol 2.4"); | |
813 | ret = 0; | |
814 | goto error; | |
815 | } | |
816 | ||
817 | DBG("Relayd sending index for stream ID %" PRIu64, relay_stream_id); | |
818 | ||
53efb85a | 819 | memset(&msg, 0, sizeof(msg)); |
1c20f0e2 JD |
820 | msg.relay_stream_id = htobe64(relay_stream_id); |
821 | msg.net_seq_num = htobe64(net_seq_num); | |
822 | ||
823 | /* The index is already in big endian. */ | |
824 | msg.packet_size = index->packet_size; | |
825 | msg.content_size = index->content_size; | |
826 | msg.timestamp_begin = index->timestamp_begin; | |
827 | msg.timestamp_end = index->timestamp_end; | |
828 | msg.events_discarded = index->events_discarded; | |
829 | msg.stream_id = index->stream_id; | |
830 | ||
831 | /* Send command */ | |
832 | ret = send_command(rsock, RELAYD_SEND_INDEX, &msg, sizeof(msg), 0); | |
833 | if (ret < 0) { | |
834 | goto error; | |
835 | } | |
836 | ||
837 | /* Receive response */ | |
838 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); | |
839 | if (ret < 0) { | |
840 | goto error; | |
841 | } | |
842 | ||
843 | reply.ret_code = be32toh(reply.ret_code); | |
844 | ||
845 | /* Return session id or negative ret code. */ | |
846 | if (reply.ret_code != LTTNG_OK) { | |
847 | ret = -1; | |
848 | ERR("Relayd send index replied error %d", reply.ret_code); | |
849 | } else { | |
850 | /* Success */ | |
851 | ret = 0; | |
852 | } | |
853 | ||
854 | error: | |
855 | return ret; | |
856 | } |