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